Skip to content

Commit

Permalink
fix(command): correct cli options type (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
2nthony authored Jul 15, 2022
1 parent 72aa2da commit 6081005
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,15 @@ export function parseCliOptionsToGitArgs(

return args;
}

export function correctCliOptionsType(
options: OptionalConfig & CAC["options"],
) {
return Object.entries(options).reduce((val, pair) => {
const [k, v] = pair;
if (v === "true" || v === "false") {
val[k] = JSON.parse(v);
}
return val;
}, options);
}
4 changes: 3 additions & 1 deletion src/commands/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
defaultConfig,
} from "../config";
import { PluginApi } from "../types";
import { correctCliOptionsType } from "../args";

export const config: PluginApi = {
extend(api) {
Expand All @@ -29,7 +30,8 @@ export const config: PluginApi = {
}

if (options.set) {
await writeUserConfig(options.set);
const correctTypedOptions = correctCliOptionsType(options.set);
await writeUserConfig(correctTypedOptions);
await printConfig(options.set);
return;
}
Expand Down
20 changes: 19 additions & 1 deletion test/args.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, test } from "vitest";
import { parseCliOptionsToGitArgs } from "../src/args";
import { correctCliOptionsType, parseCliOptionsToGitArgs } from "../src/args";
import { defaultConfig } from "../src/config";

describe("parse cli options to args", () => {
Expand All @@ -13,3 +13,21 @@ describe("parse cli options to args", () => {
expect(args).toEqual(["--depth", 1]);
});
});

describe("correct cli options type", () => {
test("boolean", () => {
expect(
correctCliOptionsType({
// @ts-ignore
shallow: "true",
}),
).toEqual({ shallow: true });

expect(
correctCliOptionsType({
// @ts-ignore
shallow: "false",
}),
).toEqual({ shallow: false });
});
});

0 comments on commit 6081005

Please sign in to comment.