Skip to content

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions src/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Usage: coderoad validate [path] [options]

Options:
--help (-h) display these help docs
--clean (-c) set to false to preserve .tmp folder. Helpful for debugging

More docs at https://github.com/coderoad/coderoad-cli`);
}
68 changes: 50 additions & 18 deletions src/utils/args.ts
Original file line number Diff line number Diff 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;
}
}
7 changes: 6 additions & 1 deletion src/utils/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,18 @@ export function createCherryPick(cwd: string) {
return async function cherryPick(commits: string[]): Promise<void> {
for (const commit of commits) {
try {
const { stdout } = await createExec(cwd)(
const { stdout, stderr } = await createExec(cwd)(
`git cherry-pick -X theirs ${commit}`
);
if (stderr) {
console.warn(stderr);
}
if (!stdout) {
console.warn(`No cherry-pick output for ${commit}`);
}
} catch (e) {
console.warn(`Cherry-pick failed for ${commit}`);
console.error(e.message);
}
}
};
Expand All @@ -50,6 +54,7 @@ export function createCommandRunner(cwd: string) {
}
const { stdout, stderr } = await createExec(cwdDir)(command);

console.log(stdout);
console.warn(stderr);
} catch (e) {
console.error(`Command failed: "${command}"`);
Expand Down
24 changes: 20 additions & 4 deletions src/validate.ts
Original file line number Diff line number Diff 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 Down Expand 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
69 changes: 69 additions & 0 deletions tests/args.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { getArg } from "../src/utils/args";

describe("args", () => {
it("should capture an arg name from text", () => {
const args = ["--name", "value"];
const result = getArg(args, { name: "name" });
expect(result).toBe("value");
});
it("should capture an arg alias from text", () => {
const args = ["-n", "value"];
const result = getArg(args, { name: "name", alias: "n" });
expect(result).toBe("value");
});
it("should capture an arg name from text when starting values", () => {
const args = ["dir", "--name", "value"];
const result = getArg(args, { name: "name" });
expect(result).toBe("value");
});
it("should capture an arg alias from text", () => {
const args = ["dir", "-n", "value"];
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, {
name: "someBool",
alias: "sb",
type: "bool",
});
expect(result).toBe(true);
});
it("should default value to true if next value is --name", () => {
const args = ["--someBool", "--someOtherBool"];
const result = getArg(args, {
name: "someBool",
alias: "sb",
type: "bool",
});
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);
});
});