-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.ts
129 lines (113 loc) · 3.38 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
#!/usr/bin/env node
// https://github.com/vercel/next.js/blob/canary/packages/create-next-app/create-app.ts
import chalk from 'chalk';
import Commander from 'commander';
import path from 'path';
import prompts from 'prompts';
import checkForUpdate from 'update-check';
import { validateNpmName, getPkgManager } from './utils';
import packageJson from './package.json';
import { createApp } from './createApp';
let projectPath: string = '';
const program = new Commander.Command(packageJson.name)
.version(packageJson.version)
.arguments('<project-directory>')
.usage(`${chalk.green('<project-directory>')} [options]`)
.action(name => {
projectPath = name;
})
.option(
'-f, --fast-mode',
`
Install dependencies from cache.
`,
)
.allowUnknownOption()
.parse(process.argv);
async function run(): Promise<void> {
if (typeof projectPath === 'string') {
projectPath = projectPath.trim();
}
if (!projectPath) {
const res = await prompts({
type: 'text',
name: 'path',
message: 'What is your project named?',
initial: 'ks-project',
validate: name => {
const validation = validateNpmName(path.basename(path.resolve(name)));
if (validation.valid) {
return true;
}
return 'Invalid project name: ' + validation.problems![0];
},
});
if (typeof res.path === 'string') {
projectPath = res.path.trim();
}
}
if (!projectPath) {
console.log(
'\nPlease specify the project directory:\n' +
` ${chalk.cyan(program.name())} ${chalk.green('<project-directory>')}\n` +
'For example:\n' +
` ${chalk.cyan(program.name())} ${chalk.green('my-ext')}\n\n` +
`Run ${chalk.cyan(`${program.name()} --help`)} to see all options.`,
);
process.exit(1);
}
const resolvedProjectPath = path.resolve(projectPath);
const projectName = path.basename(resolvedProjectPath);
const { valid, problems } = validateNpmName(projectName);
if (!valid) {
console.error(
`Could not create a project called ${chalk.red(
`"${projectName}"`,
)} because of npm naming restrictions:`,
);
problems!.forEach(p => console.error(` ${chalk.red.bold('*')} ${p}`));
process.exit(1);
}
await createApp({
appPath: resolvedProjectPath,
packageManager: getPkgManager(),
fastMode: program.fastMode,
});
}
const update = checkForUpdate(packageJson).catch(() => null);
async function notifyUpdate(): Promise<void> {
try {
const res = await update;
if (res?.latest) {
const pkgManager = getPkgManager();
console.log(
chalk.yellow.bold('A new version of `create-ks-project` is available!') +
'\n' +
'You can update by running: ' +
chalk.cyan(
pkgManager === 'yarn'
? 'yarn global add create-ks-project'
: `${pkgManager} install --global create-ks-project`,
) +
'\n',
);
}
process.exit();
} catch {
// ignore error
}
}
run()
.then(notifyUpdate)
.catch(async reason => {
console.log();
console.log('Aborting installation.');
if (reason.command) {
console.log(` ${chalk.cyan(reason.command)} has failed.`);
} else {
console.log(chalk.red('Unexpected error. Please report it as a bug:') + '\n', reason);
}
console.log();
await notifyUpdate();
process.exit(1);
});