Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add "clean" = false option#44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
ShMcK merged 4 commits intomasterfromfixes
Jun 20, 2020
Merged
Show file tree
Hide file tree
Changes from1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
PrevPrevious commit
NextNext commit
fix up arg validation
Signed-off-by: shmck <shawn.j.mckay@gmail.com>
  • Loading branch information
@ShMcK
ShMcK committedJun 20, 2020
commit3dbbd46ee1918ef21d49d0c3815888540b4aba48
68 changes: 50 additions & 18 deletionssrc/utils/args.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,64 @@
type ArgValueParams = { name: string; alias?: string; param?: boolean };
type ArgValueParams = {
name: string;
alias?: string;
type?: "string" | "bool" | "number";
};

const checkValue =(
function checkValue<T>(
args: string[],
string: string,
options: ArgValueParams
) => {
isBool: boolean
): string | null {
const nameIndex = args.indexOf(string);
if (nameIndex > -1) {
if (options.param) {
const value = args[nameIndex + 1];
if (!value) {
throw new Error(`Argument ${string} is missing a parameter value`);
if (nameIndex >= 0) {
const nextArg = args[nameIndex + 1];

if (nextArg !== undefined) {
const nextIsCommand = !!nextArg.match(/^\-/);
if (nextIsCommand) {
return isBool ? "true" : null;
}
return value;
return nextArg;
} else {
// no secondary set value
return isBool ? "true" : null;
}
}
return null;
};
}

export function getArg(args: string[], options: ArgValueParams): string | null {
let value: null | string = null;
export function getArg<T>(
args: string[],
options: ArgValueParams
): string | boolean | number | null {
let stringValue: null | string = null;
const isBool = options.type === "bool";

const aliasString = `-${options.alias}`;
value = checkValue(args, aliasString, options);
if (!value) {
if (options.alias) {
const aliasString = `-${options.alias}`;
stringValue = checkValue(args, aliasString, isBool);
}
if (!stringValue) {
const nameString = `--${options.name}`;
value = checkValue(args, nameString, options);
stringValue = checkValue(args, nameString, isBool);
}

if (stringValue === null) {
return null;
}

return value;
if (!options.type) {
options.type = "string";
}

// coerce type
switch (options.type) {
case "bool":
return (stringValue || "").toLowerCase() !== "false";
case "number":
return Number(stringValue);
case "string":
default:
return stringValue;
}
}
24 changes: 20 additions & 4 deletionssrc/validate.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -10,17 +10,30 @@ import {
} from "./utils/exec";
import { getCommits, CommitLogObject } from "./utils/commits";

interface Options {
yaml: string;
clean: boolean;
}

async function validate(args: string[]) {
// dir - default .
const dir = !args.length || args[0].match(/^-/) ? "." : args[0];
const localDir = path.join(process.cwd(), dir);

// -y --yaml - default coderoad-config.yml
const options = {
yaml: getArg(args, { name: "yaml", alias: "y" }) || "coderoad.yaml",
const options: Options = {
// @ts-ignore
yaml:
getArg(args, { name: "yaml", alias: "y", type: "string" }) ||
"coderoad.yaml",
// @ts-ignore
clean: getArg(args, { name: "clean", alias: "c", type: "bool" }),
};

const _yaml = await fs.readFile(path.join(localDir, options.yaml), "utf8");
const _yaml: string = await fs.readFile(
path.join(localDir, options.yaml),
"utf8"
);

// parse yaml config
let skeleton;
Expand DownExpand Up@@ -158,7 +171,10 @@ async function validate(args: string[]) {
console.error(e.message);
} finally {
// cleanup
await fs.emptyDir(tmpDir);
console.log("options.clean", options.clean);
if (options.clean) {
await fs.emptyDir(tmpDir);
}
}
}

Expand Down
29 changes: 28 additions & 1 deletiontests/args.test.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -21,6 +21,24 @@ describe("args", () => {
const result = getArg(args, { name: "name", alias: "n" });
expect(result).toBe("value");
});
it("should convert bool string to true", () => {
const args = ["--someBool", "true"];
const result = getArg(args, {
name: "someBool",
alias: "sb",
type: "bool",
});
expect(result).toBe(true);
});
it("should convert bool string to false", () => {
const args = ["--someBool", "false"];
const result = getArg(args, {
name: "someBool",
alias: "sb",
type: "bool",
});
expect(result).toBe(false);
});
it("should default value to true if no next value", () => {
const args = ["--someBool"];
const result = getArg(args, {
Expand All@@ -30,7 +48,7 @@ describe("args", () => {
});
expect(result).toBe(true);
});
it("should default value to true if next value isparam", () => {
it("should default value to true if next value is--name", () => {
const args = ["--someBool", "--someOtherBool"];
const result = getArg(args, {
name: "someBool",
Expand All@@ -39,4 +57,13 @@ describe("args", () => {
});
expect(result).toBe(true);
});
it("should default value to true if next value is -alias", () => {
const args = ["--someBool", "-a"];
const result = getArg(args, {
name: "someBool",
alias: "sb",
type: "bool",
});
expect(result).toBe(true);
});
});

[8]ページ先頭

©2009-2025 Movatter.jp