-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwriteConfigFile.ts
336 lines (289 loc) · 7.05 KB
/
writeConfigFile.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import { generateJsonc } from "./schema.ts";
import {
ConfigFile,
FeatureVersions,
Options,
PromptAnswerOptions,
WriteFileSecOptions,
} from "./types.ts";
export const defaultOptions: Options = {
bench: false,
force: false,
fill: false,
fmt: false,
jsonc: false,
lint: false,
lock: false,
lockfile: "",
map: false,
name: "deno.json",
task: false,
test: false,
tsconfig: false,
yes: false,
};
export async function inputHandler(opts: Options) {
if (opts.fill || opts.jsonc) {
opts.name = opts.name.replace(".json", ".jsonc");
const file = generateJsonc(opts);
await writeFileSec(
opts.name,
new TextEncoder().encode(file),
{
force: opts.force,
},
);
return file;
}
const configFile: ConfigFile = {};
if (opts.yes) {
opts.fmt = true;
opts.lint = true;
opts.tsconfig = true;
opts.task = true;
opts.test = true;
opts.bench = true;
opts.lock = true;
}
if (opts.fmt) {
configFile.fmt = {
files: {
include: [],
exclude: [],
},
options: {},
};
}
if (opts.lint) {
configFile.lint = {
files: {
include: [],
exclude: [],
},
rules: {
include: [],
exclude: [],
},
};
}
if (opts.map) {
configFile.importMap = "import_map.json";
}
if (opts.lock) {
configFile.lock = true;
} else if (opts.lockfile) {
configFile.lock = opts.lockfile;
}
if (opts.task) {
configFile.tasks = {};
}
if (opts.test) {
configFile.test = {
files: {
include: [],
exclude: [],
},
};
}
if (opts.bench) {
configFile.bench = {
files: {
include: [],
exclude: [],
},
};
}
if (opts.tsconfig) {
configFile.compilerOptions = {};
}
await writeConfigFile(configFile, opts);
return JSON.stringify(configFile, null, 2);
}
export async function writeConfigFile(
configFile: ConfigFile,
opts: Options,
) {
if (opts.name && !/\.jsonc?$/.test(opts.name)) {
throw new Error(
`Chosen filename has an unsupported file extension: ${opts.name}`,
);
}
const denoJson = new TextEncoder()
.encode(JSON.stringify(configFile, null, 2));
await writeFileSec(
`./${opts.name}`,
denoJson,
{ force: opts.force },
);
}
export async function writeFileSec(
path: string | URL,
data: Uint8Array,
options?: WriteFileSecOptions,
): Promise<void> {
if (options?.force) {
return await Deno.writeFile(path, data, options);
}
try {
const file = await Deno.readFile(path);
if (file) {
console.warn(
`Warning: file ${path} already exists. Use --force if you want to overwrite an existing file.`,
);
}
} catch (_error) {
await Deno.writeFile(path, data, options);
}
}
export function process(opts: Options): Options {
// TODO: investigate using the json schema to check this instead
if (opts.map && !isSupported("map", Deno.version.deno)) {
throw new Error("The option '--map' requires Deno v1.20 or higher");
}
if (opts.task && !isSupported("task", Deno.version.deno)) {
throw new Error("The option '--task' requires Deno v1.20 or higher");
}
if (opts.test && !isSupported("test", Deno.version.deno)) {
throw new Error("The option '--test' requires Deno v1.24 or higher");
}
if (opts.lock && !isSupported("test", Deno.version.deno)) {
throw new Error("The option '--lock' requires Deno v1.29 or higher");
}
if (opts.lockfile && !isSupported("test", Deno.version.deno)) {
throw new Error("The option '--lockfile' requires Deno v1.29 or higher");
}
if (opts.lock && opts.lockfile) {
throw new Error(
"'lock' and 'lockfile' cannot be used simultaneously. Use 'lockfile' to enable and set a custom lock file name, and use 'lock' to enable/disable deno.lock generation. Setting 'lockfile' implies 'lock': true",
);
}
if (opts.yes) {
if (
opts.tsconfig || opts.fmt || opts.lint || opts.task || opts.map ||
opts.test || opts.bench || opts.lock || opts.lockfile
) {
throw new Error("--yes cannot be used with other options");
}
}
if (
opts.yes || opts.fill || opts.fmt || opts.lint ||
opts.tsconfig || opts.jsonc || opts.task || opts.map || opts.test ||
opts.bench || opts.lock || opts.lockfile
) {
return { ...defaultOptions, ...opts };
} else {
const choices = promptUserInputs({ testmode: false });
return { ...opts, ...choices };
}
}
const featureVersionReqs: FeatureVersions = {
map: {
major: 1,
minor: 20,
},
task: {
major: 1,
minor: 20,
},
test: {
major: 1,
minor: 24,
},
lock: {
major: 1,
minor: 29,
},
lockfile: {
major: 1,
minor: 29,
},
};
function isSupported(feature: string, v: string): boolean {
const components = v.split(".");
const major = parseInt(components[0]);
const minor = parseInt(components[1]);
return major >= featureVersionReqs[feature].major &&
minor >= featureVersionReqs[feature].minor;
}
export function promptUserInputs(promptOpts: PromptAnswerOptions) {
const tsconfigAnswer = promptQuestion(
`Would you like to add custom TypeScript configuration? (y/n)`,
`y`,
promptOpts.testmode,
);
const tsconfig = processAnswer(tsconfigAnswer, defaultOptions.tsconfig);
const lintingAnswer = promptQuestion(
"Would you like to add custom linter configuration? (y/n)",
`y`,
promptOpts.testmode,
);
const lint = processAnswer(lintingAnswer, defaultOptions.lint);
const formattingAnswer = promptQuestion(
"Would you like to add custom formatter configuration? (y/n)",
`y`,
promptOpts.testmode,
);
const fmt = processAnswer(formattingAnswer, defaultOptions.fmt);
const testingAnswer = promptQuestion(
"Would you like to add custom testing configuration? (y/n)",
`y`,
promptOpts.testmode,
);
const test = processAnswer(testingAnswer, defaultOptions.test);
const taskAnswer = promptQuestion(
"Would you like to add tasks? (y/n)",
`y`,
promptOpts.testmode,
);
const task = processAnswer(taskAnswer, defaultOptions.task);
const importMapAnswer = promptQuestion(
"Would you like to add an import map? (y/n)",
`n`,
promptOpts.testmode,
);
const importMap = processAnswer(importMapAnswer, defaultOptions.map);
let name = promptQuestion(
"What should the config file be named?",
`deno.json`,
promptOpts.testmode,
);
if (!name) {
name = defaultOptions.name;
}
const opts = {
tsconfig,
lint,
fmt,
task,
test,
importMap,
name,
};
return opts;
}
function promptQuestion(
question: string,
defaultValue: string,
testmode: boolean,
) {
return (testmode) ? defaultValue : prompt(
question,
defaultValue,
);
}
export function processAnswer(
answer: string | null,
defaultValue: boolean,
): boolean {
let v;
if (!answer) {
v = defaultValue;
} else if (/y(?:es)?/i.test(answer)) {
v = true;
} else if (/n(?:o)?/i.test(answer)) {
v = false;
} else {
v = defaultValue;
}
return v;
}