-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathindex.ts
481 lines (404 loc) · 12.6 KB
/
index.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
import chalk from 'chalk';
import { epicfail } from 'epicfail';
import execa, { CommonOptions, ExecaChildProcess } from 'execa';
import { existsSync, writeFileSync } from 'fs';
import {
availableLicenses as getAvailableLicenses,
makeLicenseSync,
} from 'license.js';
import path, { join, resolve } from 'path';
import yargsInteractive, { OptionData } from 'yargs-interactive';
import { exists, isOccupied } from './fs';
import { getGitUser, initGit } from './git';
import {
addDeps,
initPackage,
installDeps,
PackageManager,
whichPm,
} from './npm';
import { copy, getAvailableTemplates } from './template';
export interface Option {
[key: string]: OptionData | { default: boolean };
}
/** Options for `create` function */
export interface Options {
/** Path to templates folder.
*
* In default, `templateRoot` is set to `path.resolve(__dirname, '../templates')`. You can change this to any location you like. */
templateRoot: string;
/** Modify package name.
*
* ```js
* {
* modifyName: (name) => (name.startsWith('create-') ? name : `create-${name}`);
* }
* ```
*/
modifyName?: (name: string) => string | Promise<string>;
/** Additional questions can be defined.
*
* These options will be available as CLI flags, interactive questions, and template strings. In the example above, `--language` flag and the `{{language}}` template string will be enabled in the app.
*/
extra?: Option;
/** Default value for a package description (default: `description`) */
defaultDescription?: string;
/** Default value for a package author (default: `user.name` in `~/.gitconfig` otherwise `Your name`) */
defaultAuthor?: string;
/** Default value for a package author email (default: `user.email` in `~/.gitconfig` otherwise `Your email`) */
defaultEmail?: string;
/** Default value for a template (default: `default`) */
defaultTemplate?: string;
/** Default value for license (default: `MIT`) */
defaultLicense?: string;
/** Default value for package manager (default: `undefined`)
*
* `npm`, `yarn` and `pnpm` are available. `undefined` to auto detect package manager. */
defaultPackageManager?: PackageManager;
/** Interactively asks users for a description */
promptForDescription?: boolean;
/** Interactively asks users for a package author */
promptForAuthor?: boolean;
/** Interactively asks users for a package author email */
promptForEmail?: boolean;
/** Interactively asks users for a template
*
* If there are no multiple templates in the `templates` directory, it won't display a prompt anyways.
*
* Even if `promptForTemplate` is set to `false`, users can still specify a template via a command line flag `--template <template>`.
* ```
* create-something <name> --template <template>
* ```
*/
promptForTemplate?: boolean;
/** Interactively asks users for a license */
promptForLicense?: boolean;
/** Interactively asks users for a package manager */
promptForPackageManager?: boolean;
/** Skip initializing a git repository at a creation time. */
skipGitInit?: boolean;
/** Skip installing package dependencies at a creation time. */
skipNpmInstall?: boolean;
/** Define after-hook script to be executed right after the initialization process. */
after?: (options: AfterHookOptions) => void | Promise<void>;
/** The caveat message will be shown after the entire process is completed.
*
* ```js
* create('create-greet', {
* caveat: 'Happy coding!',
* });
* ```
*
* ```js
* create('create-greet', {
* caveat: ({ answers }) => `Run -> cd ${answers.name} && make`,
* });
* ```
*/
caveat?:
| string
| ((options: AfterHookOptions) => string | void | Promise<string | void>);
}
/** Records of user inputs */
export interface Answers {
/** Selected template name
*
* e.g. `typescript`
*/
template: string;
/** Package name
*
* e.g. `create-greet`
*/
name: string;
/** Package description */
description: string;
/** Package author (e.g. "John Doe") */
author: string;
/** Package author email (e.g. "[email protected]") */
email: string;
/** Package author contact (e.g. "John Doe <[email protected]>") */
contact: string;
/** Package license (e.g. "MIT") */
license: string;
[key: string]: string | number | boolean | any[];
}
/** Options for after hook and caveat scripts */
export interface AfterHookOptions {
/** Created package root
*
* e.g. `/path/to/ohayo`
*/
packageDir: string;
/** Template directory
*
* e.g. `/path/to/create-greet/templates/default`
*/
templateDir: string;
/** Current year */
year: number;
/** Node.js package manager to be used to initialize a package */
packageManager: PackageManager;
/** records of user inputs */
answers: Answers;
/** Package name
*
* e.g. `create-greet`
*
* @deprecated Use `answers.name` instead.
*/
name: string;
/** Selected template name
*
* e.g. `typescript`
*
* @deprecated Use `answers.template` instead
*/
template: string;
/** execute shell commands in the package dir */
run: (
command: string,
options?: CommonOptions<string>
) => ExecaChildProcess<string>;
/** install npm package. uses package manager specified by --node-pm CLI param (default: auto-detect) */
installNpmPackage: (packageName: string, isDev?: boolean) => Promise<void>;
}
export class CLIError extends Error {
constructor(message: string) {
super(message);
this.name = 'CLIError';
}
}
export function printCommand(...commands: string[]) {
console.log(chalk.gray('>', ...commands));
}
/** @see https://github.com/uetchy/create-create-app */
export async function create(appName: string, options: Options) {
epicfail(require?.main?.filename ?? process.cwd(), {
assertExpected: (err) => err.name === 'CLIError',
});
const gitUser = await getGitUser();
const {
templateRoot,
modifyName,
extra: extraOptions = {},
after,
caveat,
defaultDescription = 'description',
defaultAuthor = gitUser.name ?? 'Your name',
defaultEmail = gitUser.email ?? 'Your email',
defaultTemplate = 'default',
defaultLicense = 'MIT',
defaultPackageManager = undefined, // undefined by default, we'll try to guess pm manager later
promptForDescription = true,
promptForAuthor = true,
promptForEmail = true,
promptForTemplate = false,
promptForLicense = true,
promptForPackageManager = false,
skipGitInit = false,
skipNpmInstall = false,
} = options;
// verify args
const firstArg = process.argv[2];
if (firstArg === undefined) {
throw new CLIError(`${appName} <name>`);
}
// configure package name and root direcotry
const useCurrentDir = firstArg === '.';
const name: string = useCurrentDir
? path.basename(process.cwd())
: modifyName
? await Promise.resolve(modifyName(firstArg))
: firstArg;
const packageDir = useCurrentDir ? process.cwd() : resolve(name);
if (isOccupied(packageDir)) {
throw new CLIError(`${packageDir} is not empty directory.`);
}
// construct yargs options
const availableTemplates = getAvailableTemplates(templateRoot);
if (availableTemplates.length === 0) {
throw new CLIError(`No template found`);
}
const availableLicenses = [...getAvailableLicenses(), 'UNLICENSED'];
const isMultipleTemplates = availableTemplates.length > 1;
const yargsOption = {
interactive: { default: true },
description: {
type: 'input',
describe: 'Description',
default: defaultDescription,
prompt: promptForDescription ? 'if-no-arg' : 'never',
},
author: {
type: 'input',
describe: 'Author name',
default: defaultAuthor,
prompt: promptForAuthor ? 'if-no-arg' : 'never',
},
email: {
type: 'input',
describe: 'Author email',
default: defaultEmail,
prompt: promptForEmail ? 'if-no-arg' : 'never',
},
template: {
type: 'list',
describe: 'Template',
default: defaultTemplate,
prompt: isMultipleTemplates && promptForTemplate ? 'if-no-arg' : 'never',
choices: availableTemplates,
},
license: {
type: 'list',
describe: 'License',
choices: availableLicenses,
default: defaultLicense,
prompt: promptForLicense ? 'if-no-arg' : 'never',
},
'node-pm': {
type: 'list',
describe: 'Package manager to use for installing packages from npm',
choices: ['npm', 'yarn', 'pnpm'],
default: defaultPackageManager, // undefined by default, we'll try to guess pm later
prompt: promptForPackageManager ? 'if-no-arg' : 'never',
},
'skip-git': {
type: 'confirm',
describe: 'Skip initializing git repository',
prompt: 'never',
},
'skip-install': {
type: 'confirm',
describe: 'Skip installing package dependencies',
prompt: 'never',
},
...extraOptions,
};
const args = (await yargsInteractive()
.usage('$0 <name> [args]')
.interactive(yargsOption as any)) as Record<keyof typeof yargsOption, any>;
const template = args['template'];
const templateDir = resolve(templateRoot, template);
if (!existsSync(templateDir)) {
throw new CLIError('No template found');
}
// guess which package manager to use
const packageManager = args['node-pm'] ?? whichPm();
const ignoredProps = ['name', 'interactive', 'node-pm', 'nodePm'];
const filteredArgs = Object.entries<string>(args)
.filter((arg) => arg[0].match(/^[^$_]/) && !ignoredProps.includes(arg[0]))
.reduce(
(sum, cur) => ((sum[cur[0]] = cur[1]), sum),
{} as {
[key in keyof Answers]: Answers[key];
}
);
const year = new Date().getFullYear();
const contact = toContact(args['author'], args['email']);
// construct answers
const answers = {
...filteredArgs,
name,
contact,
};
// copy files from the template folder
console.log(`\nCreating a new package in ${chalk.green(packageDir)}.`);
await copy({
sourceDir: templateDir,
targetDir: packageDir,
view: {
...answers,
year,
packageManager,
},
});
// create license file
try {
const license = makeLicenseSync(args['license'], {
year,
project: name,
description: args.description,
organization: toContact(args['author'], args['email']),
});
const licenseText =
(license.header ?? '') + license.text + (license.warranty ?? '');
writeFileSync(resolve(packageDir, 'LICENSE'), licenseText);
} catch (e) {
// do not generate LICENSE
}
// init git if option skipGitInit or arg --skip-git are not set
if (!(skipGitInit || args['skip-git'])) {
try {
console.log('\nInitializing a git repository');
await initGit(packageDir);
} catch (err: any) {
if (err?.exitCode == 127) return; // no git available
throw err;
}
}
const installNpmPackage = async (
pkg: string | string[],
isDev: boolean = false
): Promise<void> => {
if (!existsSync(join(packageDir, 'package.json'))) {
await initPackage(packageDir, { pm: packageManager });
}
await addDeps(packageDir, Array.isArray(pkg) ? pkg : [pkg], {
isDev,
pm: packageManager,
});
};
// run Node.js related tasks only if `package.json` does exist in the package root
// and skipNpmInstall is not falsy
const installDepsOnCreation =
exists('package.json', packageDir) &&
!(skipNpmInstall || args['skip-install']);
if (installDepsOnCreation) {
console.log(`\nInstalling dependencies using ${packageManager}`);
await installDeps(packageDir, packageManager);
}
// setup afterHookOptions
const run = (command: string, options: CommonOptions<string> = {}) => {
const [script, ...scriptArgs] = command.split(' ');
return execa(script, scriptArgs, {
stdio: 'inherit',
cwd: packageDir,
...options,
});
};
const hookOptions: AfterHookOptions = {
packageDir,
templateDir,
year,
packageManager,
answers,
run,
installNpmPackage,
name,
template,
};
// run after hook script
if (after) {
await Promise.resolve(after(hookOptions));
}
console.log(`\nSuccessfully created ${chalk.bold.cyan(packageDir)}`);
// show caveat
if (caveat) {
switch (typeof caveat) {
case 'string':
console.log(caveat);
break;
case 'function':
const response = await Promise.resolve(caveat(hookOptions));
if (response) {
console.log(response);
}
break;
default:
}
}
}
function toContact(author: string, email?: string) {
return `${author}${email ? ` <${email}>` : ''}`;
}