-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
208 lines (185 loc) · 6.15 KB
/
index.js
File metadata and controls
208 lines (185 loc) · 6.15 KB
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
#!/usr/bin/env node
'use strict';
const readline = require('readline');
const validateProjectName = require('validate-npm-package-name');
const chalk = require('chalk');
const commander = require('commander');
const fs = require('fs-extra');
const path = require('path');
const execSync = require('child_process').execSync;
const spawn = require('cross-spawn');
const Promise = require('bluebird');
const ora = require('ora');
const emoji = require('node-emoji')
const packageJson = require('./package.json');
let projectName;
commander
.version(packageJson.version)
.arguments('<project-directory>')
.usage(`${chalk.green('<project-directory>')}`)
.action(name => {
projectName = name;
})
.parse(process.argv);
if (typeof projectName === 'undefined') {
console.error('Please specify the project directory:');
console.log(
` ${chalk.cyan(commander.name())} ${chalk.green('<project-directory>')}`
);
console.log();
console.log('For example:');
console.log(` ${chalk.cyan(commander.name())} ${chalk.green('my-react-app')}`);
console.log();
process.exit(1);
}
createApp(projectName);
function createApp(name) {
const root = path.resolve(name);
const appName = path.basename(root);
const useYarn = shouldUseYarn();
checkAppName(appName);
validateDir(root, appName)
.then(function () {
process.chdir(root);
initGit();
console.log(chalk.cyan(`${emoji.find('sparkles').emoji} git has been successfully initialized`));
})
.then(function () {
const spinner = ora().start();
spinner.color = 'yellow';
spinner.text = 'Creating the project directory and it may take few minutes.';
return fs.copy(path.resolve(__dirname, 'template'), root)
.then(() => {
spinner.stop();
});
})
.then(function () {
return saveProjectName(root, appName);
})
.then(function () {
console.log(chalk.cyan(`${emoji.find('sparkles').emoji} ${appName} Project directory has been successfully created`));
console.log('Starts installing dependencies and it may take few minutes')
return installDependencies(useYarn);
})
.then(function () {
showSuccessMessage(root, appName, useYarn);
})
.catch(function (err) {
console.log(chalk.red('Error'));
console.error(err);
})
}
function checkAppName(appName) {
const validationResult = validateProjectName(appName);
if (!validationResult.validForNewPackages) {
console.error(
`Could not create a project called ${chalk.red(`"${appName}"`)} because of npm naming restrictions:`
);
printValidationResults(validationResult.errors);
printValidationResults(validationResult.warnings);
process.exit(1);
}
}
function printValidationResults(results) {
if (typeof results !== 'undefined') {
results.forEach(error => {
console.error(chalk.red(` * ${error}`));
});
}
}
function shouldUseYarn() {
try {
execSync('yarnpkg --version', { stdio: 'ignore' });
return true;
} catch (e) {
return false;
}
}
function initGit() {
execSync('git init');
}
function validateDir(dir, appName) {
const isExist = fs.existsSync(dir);
if (!isExist) {
return fs.ensureDir(dir);
}
const stat = fs.statSync(dir);
if (stat.isDirectory()) {
return new Promise((resolve) => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question(`Folder ${appName} already exists, continuing operation may result in overwriting of existing files, whether to continue y/n ?`, (answer) => {
rl.close();
if (!answer || answer !== 'y') {
process.exit(1);
} else {
return resolve();
}
});
});
} else {
console.error(
`${chalk.red(`"${appName}"`)} Already exists, please reassign the project name`
);
process.exit(1);
}
}
function installDependencies(useYarn) {
let command;
let args;
if (useYarn) {
command = 'yarn';
} else {
command = 'npm';
args = ['install'];
}
return new Promise(function (resolve, reject) {
const child = spawn(command, args, { stdio: 'inherit' });
child.on('close', code => {
if (code !== 0) {
reject({
command: `${command} ${args.join(' ')}`,
});
return;
}
resolve();
});
});
}
function saveProjectName(root, name) {
const filePath = path.join(root, 'package.json');
const packageJson = require(filePath);
packageJson.name = name;
return fs.writeJson(filePath, packageJson, { spaces: 4 });
}
function showSuccessMessage(root, appName, useYarn) {
const displayedCommand = useYarn ? 'yarn' : 'npm';
console.log()
console.log(chalk.green(`${emoji.find('sparkles').emoji} Success! Created ${appName} at ${root}`));
console.log();
console.log('Inside that directory, you can run several commands:');
console.log();
console.log(chalk.cyan(` ${displayedCommand} dev`));
console.log(' Starts the development server.');
console.log();
console.log(chalk.cyan(` ${displayedCommand} prod`));
console.log(' Starts the production server.');
console.log();
console.log(
chalk.cyan(` ${displayedCommand} ${useYarn ? '' : 'run '}build:dev`)
);
console.log(' Bundles the app into static files for development.');
console.log();
console.log(
chalk.cyan(` ${displayedCommand} ${useYarn ? '' : 'run '}build:prod`)
);
console.log(' Bundles the app into static files for production.');
console.log();
console.log(
chalk.cyan(` ${displayedCommand} ${useYarn ? '' : 'run '}test`)
);
console.log(' Run the tests.');
console.log();
}