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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#!/usr/bin/env node
const shell = require('shelljs');
const { exec } = require('child_process');
const path = require('path');
const fs = require('fs');
const readline = require('readline');
const compareVersions = require('compare-versions');
const chalk = require('chalk');
const animateProgress = require('./helpers/progress');
const addCheckMark = require('./helpers/checkmark');
const addXMark = require('./helpers/xmark');
const npmConfig = require('./helpers/get-npm-config');
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdout.write('\n');
let interval = -1;
/**
* Deletes a file in the current directory
* @param {string} file
* @returns {Promise<any>}
*/
function deleteFileInCurrentDir(file) {
return new Promise((resolve, reject) => {
fs.unlink(path.join(__dirname, file), err => reject(new Error(err)));
resolve();
});
}
/**
* Checks if we are under Git version control
* @returns {Promise<boolean>}
*/
function hasGitRepository() {
return new Promise((resolve, reject) => {
exec('git status', (err, stdout) => {
if (err) {
reject(new Error(err));
}
const regex = new RegExp(/fatal:\s+Not\s+a\s+git\s+repository/, 'i');
/* eslint-disable-next-line no-unused-expressions */
regex.test(stdout) ? resolve(false) : resolve(true);
});
});
}
/**
* Checks if this is a clone from our repo
* @returns {Promise<any>}
*/
function checkIfRepositoryIsAClone() {
return new Promise((resolve, reject) => {
exec('git remote -v', (err, stdout) => {
if (err) {
reject(new Error(err));
}
const isClonedRepo = stdout
.split(/\r?\n/)
.map(line => line.trim())
.filter(line => line.startsWith('origin'))
.filter(line => /react-boilerplate\/react-boilerplate\.git/.test(line))
.length;
resolve(!!isClonedRepo);
});
});
}
/**
* Remove the current Git repository
* @returns {Promise<any>}
*/
function removeGitRepository() {
return new Promise((resolve, reject) => {
try {
shell.rm('-rf', '.git/');
resolve();
} catch (err) {
reject(err);
}
});
}
/**
* Ask user if he wants to start with a new repository
* @returns {Promise<boolean>}
*/
function askUserIfWeShouldRemoveRepo() {
return new Promise(resolve => {
process.stdout.write(
'\nDo you want to start with a new repository? [Y/n] ',
);
process.stdin.resume();
process.stdin.on('data', pData => {
const answer =
pData
.toString()
.trim()
.toLowerCase() || 'y';
/* eslint-disable-next-line no-unused-expressions */
answer === 'y' ? resolve(true) : resolve(false);
});
});
}
/**
* Checks if we are under Git version control.
* If we are and this a clone of our repository the user is given a choice to
* either keep it or start with a new repository.
* @returns {Promise<boolean>}
*/
async function cleanCurrentRepository() {
const hasGitRepo = await hasGitRepository().catch(reason =>
reportError(reason),
);
// We are not under Git version control. So, do nothing
if (hasGitRepo === false) {
return false;
}
const isClone = await checkIfRepositoryIsAClone().catch(reason =>
reportError(reason),
);
// Not our clone so do nothing
if (isClone === false) {
return false;
}
const answer = await askUserIfWeShouldRemoveRepo();
if (answer === true) {
process.stdout.write('Removing current repository');
await removeGitRepository().catch(reason => reportError(reason));
addCheckMark();
}
return answer;
}
/**
* Check Node.js version
* @param {!number} minimalNodeVersion
* @returns {Promise<any>}
*/
function checkNodeVersion(minimalNodeVersion) {
return new Promise((resolve, reject) => {
exec('node --version', (err, stdout) => {
const nodeVersion = stdout.trim();
if (err) {
reject(new Error(err));
} else if (compareVersions(nodeVersion, minimalNodeVersion) === -1) {
reject(
new Error(
`You need Node.js v${minimalNodeVersion} or above but you have v${nodeVersion}`,
),
);
}
resolve('Node version OK');
});
});
}
/**
* Check NPM version
* @param {!number} minimalNpmVersion
* @returns {Promise<any>}
*/
function checkNpmVersion(minimalNpmVersion) {
return new Promise((resolve, reject) => {
exec('npm --version', (err, stdout) => {
const npmVersion = stdout.trim();
if (err) {
reject(new Error(err));
} else if (compareVersions(npmVersion, minimalNpmVersion) === -1) {
reject(
new Error(
`You need NPM v${minimalNpmVersion} or above but you have v${npmVersion}`,
),
);
}
resolve('NPM version OK');
});
});
}
/**
* Install all packages
* @returns {Promise<any>}
*/
function installPackages() {
return new Promise((resolve, reject) => {
process.stdout.write(
'\nInstalling dependencies... (This might take a while)',
);
setTimeout(() => {
readline.cursorTo(process.stdout, 0);
interval = animateProgress('Installing dependencies');
}, 500);
exec('npm install', err => {
if (err) {
reject(new Error(err));
}
clearInterval(interval);
addCheckMark();
resolve('Packages installed');
});
});
}
/**
* Initialize a new Git repository
* @returns {Promise<any>}
*/
function initGitRepository() {
return new Promise((resolve, reject) => {
exec('git init', (err, stdout) => {
if (err) {
reject(new Error(err));
} else {
resolve(stdout);
}
});
});
}
/**
* Add all files to the new repository
* @returns {Promise<any>}
*/
function addToGitRepository() {
return new Promise((resolve, reject) => {
exec('git add .', (err, stdout) => {
if (err) {
reject(new Error(err));
} else {
resolve(stdout);
}
});
});
}
/**
* Initial Git commit
* @returns {Promise<any>}
*/
function commitToGitRepository() {
return new Promise((resolve, reject) => {
exec('git commit -m "Initial commit"', (err, stdout) => {
if (err) {
reject(new Error(err));
} else {
resolve(stdout);
}
});
});
}
/**
* Report the the given error and exits the setup
* @param {string} error
*/
function reportError(error) {
clearInterval(interval);
if (error) {
process.stdout.write('\n\n');
addXMark(() => process.stderr.write(chalk.red(` ${error}\n`)));
process.exit(1);
}
}
/**
* End the setup process
*/
function endProcess() {
clearInterval(interval);
process.stdout.write(chalk.blue('\n\nDone!\n'));
process.exit(0);
}
/**
* Run
*/
(async () => {
const repoRemoved = await cleanCurrentRepository();
// Take the required Node and NPM version from package.json
const {
engines: { node, npm },
} = npmConfig;
const requiredNodeVersion = node.match(/([0-9.]+)/g)[0];
await checkNodeVersion(requiredNodeVersion).catch(reason =>
reportError(reason),
);
const requiredNpmVersion = npm.match(/([0-9.]+)/g)[0];
await checkNpmVersion(requiredNpmVersion).catch(reason =>
reportError(reason),
);
await installPackages().catch(reason => reportError(reason));
await deleteFileInCurrentDir('setup.js').catch(reason => reportError(reason));
if (repoRemoved) {
process.stdout.write('\n');
interval = animateProgress('Initialising new repository');
process.stdout.write('Initialising new repository');
try {
await initGitRepository();
await addToGitRepository();
await commitToGitRepository();
} catch (err) {
reportError(err);
}
addCheckMark();
clearInterval(interval);
}
endProcess();
})();