-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnpm-init.ts
174 lines (157 loc) · 4.91 KB
/
npm-init.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
import fs from "node:fs";
import path from "node:path";
import * as inquirer from "@inquirer/prompts";
import { InteractiveCommand, InteractiveOption } from "../src/index.ts";
const program = new InteractiveCommand();
const createPackageJson = <T extends Record<string, unknown>>(record: T): T => {
const { test, ...otherProperties } = record;
return Object.fromEntries(
Object.entries({
...otherProperties,
...(test
? {
scripts: {
test: test ?? 'echo "Error: no test specified" && exit 1',
},
}
: {}),
}).filter(([_, value]) => value !== undefined),
) as T;
};
program
.version("1.0.0", "-V, --display-version", "output the version number")
.hook("preSubcommand", async (thisCommand, actionCommand) => {
if (actionCommand.name() !== "init") {
return;
}
// Without package.json, npm init prompts for the following:
// - name
// - version
// - description
// - entry point
// - test command
// - git repository
// - keywords
// - author
// - license
// When package.json exists, npm init prompts for the following:
// - name
// - version
// - description
// - git repository
// - author
// - license
let packageJson: Record<string, unknown> = {};
try {
packageJson = JSON.parse(
await fs.promises.readFile("package.json", "utf8"),
) as Record<string, unknown>;
// Disable some prompts when package.json exists
const promptsToDisable = [
// Entry point
"main",
"test",
"keywords",
];
for (const promptToDisable of promptsToDisable) {
(
actionCommand.options.find(
(option) => option.attributeName() === promptToDisable,
) as InteractiveOption
)
// eslint-disable-next-line unicorn/no-useless-undefined
.prompt(undefined);
}
} catch {}
for (const [key, value] of Object.entries(packageJson)) {
actionCommand.setOptionValueWithSource(key, value, "config");
}
actionCommand.setOptionValueWithSource(
"name",
packageJson.name ?? path.basename(process.cwd()),
"config",
);
})
.command("init")
.addOption(
new InteractiveOption("-n, --name <name>", "package name")
.argParser((value) => {
if (
typeof value !== "string" ||
// From: https://github.com/dword-design/package-name-regex/blob/master/src/index.js
!/^(@[\da-z~-][\d._a-z~-]*\/)?[\da-z~-][\d._a-z~-]*$/.test(value)
) {
throw new TypeError("Invalid package name");
}
return value;
})
.makeOptionMandatory(),
)
.requiredOption("-v, --version <version>", "version", "1.0.0")
.option("-d, --description [description]", "description")
.option("-m, --main [entry]", "entry point", "index.js")
.option("-t, --test [test]", "test command")
.option("-g, --git [git]", "git repository")
.addOption(
new InteractiveOption("-k, --keywords [keywords...]", "keywords").argParser(
(value) => value.split(/[ ,]+/g).filter(Boolean),
),
)
.option("-a, --author [author]", "author")
.option("-l, --license [license]", "license", "ISC")
.addOption(
// This option is defined here for generating the help message
new InteractiveOption("-y, --yes", "confirm the creation of package.json")
// eslint-disable-next-line unicorn/no-useless-undefined
.prompt(undefined),
)
.action(
async (
{
confirm,
// This option is for the subcommand "init". When positional
// options are not enabled, this value is always set to the default
// value (true)
interactive,
...otherOptions
},
command: InteractiveCommand,
) => {
let newConfirm = confirm === true || confirm === undefined;
const isInteractive = Boolean(command.optsWithGlobals().interactive);
if (isInteractive) {
console.log(
JSON.stringify(createPackageJson(otherOptions), undefined, 2),
);
newConfirm = await inquirer.confirm({
message: "Is this OK?",
default: Boolean(newConfirm),
});
}
if (!newConfirm) {
console.log("Aborted.");
return;
}
console.log("TODO: create package.json with the following contents:");
console.log(
JSON.stringify(createPackageJson(otherOptions), undefined, 2),
);
},
);
await program
.interactive("-I, --no-interactive", "disable interactive mode")
.parseAsync()
.catch((error) => {
if (
!(
error instanceof Error &&
error.message.startsWith("User force closed the prompt with")
)
) {
throw error;
}
});
// Try the following commands, with and without a package.json in the current working directory:
// npm-init init
// npm-init init --name foo
// npm-init init -I --name foo --version "2.0.0"