Skip to content

Commit 1b1f3e7

Browse files
authored
feat: support lock option (#171)
* feat: support lock option * add tests
1 parent 051d360 commit 1b1f3e7

6 files changed

+73
-2
lines changed

__snapshots__/writeConfigFile.test.ts.snap

+15
Original file line numberDiff line numberDiff line change
@@ -178,13 +178,20 @@ snapshot[`writeConfigFile 9`] = `
178178
' "exclude": []',
179179
" }",
180180
" },",
181+
' "lock": true,',
181182
' "tasks": {},',
182183
' "test": {',
183184
' "files": {',
184185
' "include": [],',
185186
' "exclude": []',
186187
" }",
187188
" },",
189+
' "bench": {',
190+
' "files": {',
191+
' "include": [],',
192+
' "exclude": []',
193+
" }",
194+
" },",
188195
' "compilerOptions": {}',
189196
"}",
190197
]
@@ -215,3 +222,11 @@ snapshot[`writeConfigFile 11`] = `
215222
"}",
216223
]
217224
`;
225+
226+
snapshot[`writeConfigFile 12`] = `
227+
[
228+
"{",
229+
' "lock": true',
230+
"}",
231+
]
232+
`;

init.ts

+4
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ await new Command()
4141
"-l, --lint [lint:boolean]",
4242
"Set up config for deno lint only.",
4343
)
44+
.option(
45+
"-o, --lock <lock:string>",
46+
"The name of the lock file.",
47+
)
4448
.option(
4549
"-k, --task [task:boolean]",
4650
"Set up config for deno tasks only. Requires Deno 1.20 or higher",

processOptions.test.ts

+24
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,30 @@ Deno.test("processOptions", async (context) => {
8989
},
9090
});
9191

92+
await test({
93+
name: "throw if --yes is used with --bench",
94+
fn: () => {
95+
testOpts.yes = true;
96+
testOpts.bench = true;
97+
98+
assertThrows(() => {
99+
process(testOpts);
100+
});
101+
},
102+
});
103+
104+
await test({
105+
name: "throw if --yes is used with --lock",
106+
fn: () => {
107+
testOpts.yes = true;
108+
testOpts.lock = true;
109+
110+
assertThrows(() => {
111+
process(testOpts);
112+
});
113+
},
114+
});
115+
92116
await test({
93117
name: "Returns object with map: true if --map option is true",
94118
fn: () => {

processOptions.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,16 @@ export function process(opts: Options): Options {
1616
if (opts.yes) {
1717
if (
1818
opts.tsconfig || opts.fmt || opts.lint || opts.task || opts.map ||
19-
opts.test
19+
opts.test || opts.bench || opts.lock
2020
) {
2121
throw new Error("--yes cannot be used together with other options.");
2222
}
2323
}
2424

2525
if (
2626
opts.yes || opts.fill || opts.fmt || opts.lint ||
27-
opts.tsconfig || opts.jsonc || opts.task || opts.map || opts.test
27+
opts.tsconfig || opts.jsonc || opts.task || opts.map || opts.test ||
28+
opts.bench || opts.lock
2829
) {
2930
return { ...defaultOpts, ...opts };
3031
} else {

writeConfigFile.test.ts

+18
Original file line numberDiff line numberDiff line change
@@ -258,4 +258,22 @@ Deno.test("writeConfigFile", async (context) => {
258258
await assertSnapshot(context, output.split("\n"));
259259
},
260260
});
261+
262+
await test({
263+
name: "create lock",
264+
fn: async () => {
265+
testOpts.lock = true;
266+
267+
const output = await inputHandler(testOpts);
268+
269+
const bytes = await Deno.readFile(
270+
`${testOpts.name}`,
271+
);
272+
273+
assert(bytes.length > 0);
274+
assertEquals(testOpts.name, "deno.json");
275+
276+
await assertSnapshot(context, output.split("\n"));
277+
},
278+
});
261279
});

writeConfigFile.ts

+9
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export interface Options {
88
fmt: boolean;
99
jsonc: boolean;
1010
lint: boolean;
11+
lock: boolean;
1112
map: boolean;
1213
name: string;
1314
task: boolean;
@@ -23,6 +24,7 @@ export const defaultOpts: Options = {
2324
fmt: false,
2425
jsonc: false,
2526
lint: false,
27+
lock: false,
2628
map: false,
2729
name: "deno.json",
2830
task: false,
@@ -65,6 +67,7 @@ export type ConfigFile = {
6567
};
6668
};
6769
importMap?: string;
70+
lock?: boolean;
6871
};
6972

7073
export async function inputHandler(opts: Options) {
@@ -91,6 +94,8 @@ export async function inputHandler(opts: Options) {
9194
opts.tsconfig = true;
9295
opts.task = true;
9396
opts.test = true;
97+
opts.bench = true;
98+
opts.lock = true;
9499
}
95100

96101
if (opts.fmt) {
@@ -120,6 +125,10 @@ export async function inputHandler(opts: Options) {
120125
configFile.importMap = "import_map.json";
121126
}
122127

128+
if (opts.lock) {
129+
configFile.lock = true;
130+
}
131+
123132
if (opts.task) {
124133
configFile.tasks = {};
125134
}

0 commit comments

Comments
 (0)