-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
236 lines (197 loc) · 5.67 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
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
#!/usr/bin/env node
'use strict'
var program = require('commander');
var path = require('path'),
fs = require('fs-extra'),
glob = require('glob'),
chalk = require('chalk'),
shell = require('shelljs');
/*** functions ***/
function log(lines) {
lines = [].concat(lines);
console.log('\n' + lines.join('\n'));
}
function execOut(cmd, options) {
const res = shell.exec(cmd, options);
return res.stdout;
}
function execCode(cmd, options) {
const res = shell.exec(cmd, options);
return res.code;
}
function fileExists(file) {
return fs.existsSync(file);
}
function readFile(file) {
if (!fs.existsSync(file)) { return ''; }
return fs.readFileSync(file, { encoding: 'utf8' });
}
function files(dir) {
return glob.sync(path.join(dir, '**'), { dot: true })
.filter(file => !fs.lstatSync(file).isDirectory());
}
function dirs(dir) {
return glob.sync(path.join(dir, '*'), { dot: true })
.filter(file => fs.lstatSync(file).isDirectory());
}
function copyFile(src, dest) {
fs.mkdirsSync(dest.replace(new RegExp('/[^/]*$'), ''));
fs.copySync(src, dest);
}
function listTemplates() {
const lines = dirs(templatesRoot)
.map(dir => dir.replace(templatesRoot, '').slice(1))
.map(name => chalk.green(' ' + name));
log(
['List of templates:'].concat(lines)
);
}
function showTemplate(template) {
const templateRoot = path.join(templatesRoot, template);
if (!fileExists(templateRoot)) {
log('Template ' + chalk.yellow(template) + ' does not exist');
return;
}
const templateReadMe = path.join(templateRoot, 'template.md');
if (!fileExists(templateReadMe)) {
log('Template ' + chalk.yellow(template) + ' does not have readme. i.e. /template.md');
return;
}
const lines = readFile(templateReadMe)
.split('\n')
.map(line => ' ' + line);
log(lines);
}
/*** checking ***/
let projectName = '';
function parseArgs() {
program
.arguments('<project-directory>')
.action(name => projectName = name)
.option('--use-npm')
.option('-t --template [value]', 'template name')
.parse(process.argv);
}
function moduleInfo(module) {
let out = execOut('npm list --global --depth=0 ' + module, { silent: true});
if (!out || out.indexOf(module) < 0) {
out = execOut('npm list --depth=0 ' + module, { silent: true});
}
if (!out || out.indexOf(module) < 0) {
return null;
}
const lines = out.split('\n');
const moduleRoot = lines[0];
const moduleLines = lines
.filter(line => line.indexOf(module) >= 0)
const version = moduleLines[0].split('@')[1].trim();
return {
location: path.join(moduleRoot, 'node_modules', module),
version: version
}
}
function moduleVersion(module) {
const info = moduleInfo(module);
return info? info.version : '';
}
function hasYarn() {
let out = execOut('which yarn', { silent: true});
return !!out;
}
parseArgs();
const useNpm = program.useNpm || !hasYarn();
let template = program.template;
const cbra = moduleInfo('create-bootstrap-react-app');
const templatesRoot = path.join(cbra.location, 'templates');
if (!projectName) {
if (template) {
if (template === true) {
listTemplates();
} else {
showTemplate(template);
}
}
log([
'Please specify the project directory:',
' ' + chalk.cyan('create-bootstrap-react-app ') + chalk.green('<project-directory>'),
'',
'For example:',
' ' + chalk.cyan('create-bootstrap-react-app ') + chalk.green('my-bootstrap-react-app')
])
process.exit(1);
}
const nodeVersion = process.versions.node;
const craVersion = moduleVersion('create-react-app');
template = template || 'starter';
log([
chalk.cyan('Node version: ') + nodeVersion,
chalk.cyan('create-react-app version: ') + craVersion
]);
if (!craVersion) {
log([
chalk.red('Requires Create React App'),
' Please install with command: ' + chalk.cyan('npm install -g create-react-app')
]);
}
/*** create react app ***/
function createApp() {
return execCode('create-react-app ' + projectName);
}
log('');
const cra_ok = (createApp() === 0);
if (!cra_ok) {
log([
chalk.cyan('create-react-app') + chalk.yellow(' was not successful. Check the error and try again.')
]);
process.exit(1);
}
/*** bootrap-4-react ***/
console.log('Installing bootstrap-4-react');
shell.cd(projectName);
if (useNpm) {
execCode('npm install --save bootstrap-4-react');
} else {
execCode('yarn add bootstrap-4-react');
}
shell.cd('..');
/*** copy template ***/
log('Copying template');
const CWD = process.cwd();
const projectRoot = path.join(CWD, projectName);
let templateRoot = path.join(templatesRoot, template);
if (!fileExists(templateRoot)) {
log('Template ' + chalk.yellow(template) + ' does not exist, use starter');
template = 'starter';
templateRoot = path.join(templatesRoot, template);
}
files(templateRoot)
.filter(file => !file.endsWith('template.md'))
.forEach(file => {
const dest = file.replace(templateRoot, projectRoot);
copyFile(file, dest);
});
/*** success ***/
log([
'Success! Created ' + projectName + ' at ' + projectRoot,
'Inside that directory, you can run several commends:',
'',
' ' + chalk.cyan('npm start'),
' Starts that development server.',
'',
' ' + chalk.cyan('npm run build'),
' Bundles the app into static files for production.',
'',
' ' + chalk.cyan('npm test'),
' Starts the test runner.',
'',
' ' + chalk.cyan('npm run eject'),
' Removes this tool and copies build dependencies, configuration files',
' and scripts into the app directory. If you do this, you can’t go back!',
'',
'We suggest that you begin by typing:',
'',
' ' + chalk.cyan('cd') + ' ' + projectName,
' ' + chalk.cyan('npm start'),
'',
'Happy hacking!'
]);