-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·79 lines (63 loc) · 2.05 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
76
77
78
79
#! /usr/bin/env node
const { spawn } = require('child_process');
const fs = require('fs');
const args = process.argv.slice(2);
const helpMessage = `
Usage: create-react-app-antd <options> <app-name>
Options
-y|--y|-yarn|--yarn Use yarn instead of npm.
-h|-help|--h|--help Display this message.`;
if (args.find(arg => arg.match(/^(-|--)(h|help)$/))) {
return console.log(helpMessage);
}
const yarnIndex = args.findIndex(arg => arg.match(/^(-|--)(y|yarn)$/));
let yarnFlag = false;
if (yarnIndex >= 0) {
args.splice(yarnFlag, 1);
yarnFlag = true;
}
const name = args[0];
if (!name || name.match(/[<>:"\/\\|?*\x00-\x1F]/) || fs.existsSync(name)) {
return console.log(`
Invalid or already existing directory name.
${helpMessage}
`);
}
const platform = /^win/.test(process.platform) ? 'win' : 'unix';
const repoURL = 'https://github.com/ant-design/create-react-app-antd.git';
const gitDir = `${name}${platform === 'win'? '\\' : '/'}.git`;
const commands = {
git: ['git', ['clone', repoURL, name]],
rm: platform === 'win' ? ['cmd', ['/c', 'rmdir', '/s', '/q', gitDir]] : ['rm', ['-rf', gitDir]],
npm: [platform === 'win' ? 'npm.cmd' : 'npm', ['install']],
yarn: [platform === 'win' ? 'yarn.cmd' : 'yarn', ['install']]
};
runCommand(...commands.git)
.then(() => {
if (fs.existsSync(gitDir)) {
return runCommand(...commands.rm);
} else {
return Promise.resolve();
}
}).then(() => {
console.log('Installing dependencies...');
return runCommand(...commands[yarnFlag ? 'yarn' : 'npm'], {
cwd: process.cwd() + '/' + name
});
}).then(() => {
console.log('Done! 🏁');
console.log('');
console.log('To get started:');
console.log('cd', name);
console.log(yarnFlag ? 'yarn start' : 'npm start');
});
function runCommand(command, args, options = undefined) {
const spawned = spawn(command, args, options);
return new Promise((resolve) => {
spawned.stdout.pipe(process.stdout);
spawned.stderr.pipe(process.stderr);
spawned.on('close', () => {
resolve();
});
});
}