-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.js
executable file
·75 lines (71 loc) · 2.09 KB
/
index.js
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
#!/usr/bin/env node
const renderDir = require('./renderDir');
const path = require('path');
const fs = require('fs-extra');
const inquirer = require('inquirer');
const argv = require('minimist')(process.argv.slice(2));
const execAsync = require('./execAsync');
async function main() {
let projectName = argv?._?.[0];
if (!projectName || typeof projectName !== 'string') {
const ans = await inquirer.prompt([
{
type: 'input',
name: 'projectName',
message: '目录名',
},
]);
projectName = ans.projectName;
}
const targetDir = path.resolve(process.cwd(), projectName);
if (fs.existsSync(targetDir)) {
throw new Error('目录已存在');
}
console.log(__dirname, 'rendering...');
// await execAsync("rm -rf .init-tmp");
// await execAsync("mkdir .init-tmp");
// renderDir()
await renderDir(
// `${process.cwd()}/.init-tmp`,
__dirname,
'template',
`${process.cwd()}/${projectName}`,
{
prompts: [
{
type: 'string',
name: 'name',
default: projectName,
required: true,
message: '名称',
validate(v) {
if (/^[a-z][A-z0-9-]*$/.test(v)) {
return true;
}
return '输入不合法';
},
},
{
type: 'string',
name: 'description',
default: 'description',
message: '描述',
},
],
},
);
// await fs.copySync(__dirname + '.gitignore', `${targetDir}/.gitignore`);
// await execAsync(`cp ${__dirname}/template/.gitignore ${targetDir}/.gitignore`);
// todo 这个link不太灵
await execAsync(`ln -s ../CHANGELOG.md changelog.md`, { cwd: `${targetDir}/website` });
await execAsync('rm -rf .git', { cwd: targetDir });
try {
await execAsync('git init -b main', { cwd: targetDir });
} catch (error) {
await execAsync('git init && git checkout -b main', { cwd: targetDir });
}
await execAsync("git add . && git commit -nam 'init'", { cwd: targetDir });
await execAsync('rm -rf .init-tmp');
console.log('✨ done');
}
main();