forked from scaffold-eth/scaffold-eth-2
-
Notifications
You must be signed in to change notification settings - Fork 5
/
prompt-for-missing-options.ts
72 lines (60 loc) · 2.03 KB
/
prompt-for-missing-options.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
import { config } from "../config";
import { Options, RawOptions, isDefined } from "../types";
import inquirer from "inquirer";
// default values for unspecified args
const defaultOptions: RawOptions = {
project: "my-dapp-example",
install: true,
dev: false,
externalExtension: null,
help: false,
solidityFramework: null,
};
const invalidQuestionNames = ["project", "install"];
const nullExtensionChoice = {
name: "none",
value: null,
};
export async function promptForMissingOptions(options: RawOptions): Promise<Options> {
const cliAnswers = Object.fromEntries(Object.entries(options).filter(([, value]) => value !== null));
const questions = [];
questions.push({
type: "input",
name: "project",
message: "Your project name:",
default: defaultOptions.project,
validate: (value: string) => value.length > 0,
});
config.questions.forEach(question => {
if (invalidQuestionNames.includes(question.name)) {
throw new Error(
`The name of the question can't be "${question.name}". The invalid names are: ${invalidQuestionNames
.map(w => `"${w}"`)
.join(", ")}`,
);
}
const extensions = question.extensions.filter(isDefined);
const hasNoneOption = question.extensions.includes(null);
questions.push({
type: question.type === "multi-select" ? "checkbox" : "list",
name: question.name,
message: question.message,
choices: hasNoneOption ? [...extensions, nullExtensionChoice] : extensions,
});
});
questions.push({
type: "confirm",
name: "install",
message: "Install packages?",
default: defaultOptions.install,
});
const answers = await inquirer.prompt(questions, cliAnswers);
const mergedOptions: Options = {
project: options.project ?? answers.project,
install: options.install ?? answers.install,
dev: options.dev ?? defaultOptions.dev,
solidityFramework: options.solidityFramework ?? answers.solidityFramework,
externalExtension: options.externalExtension,
};
return mergedOptions;
}