-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
574 lines (488 loc) · 17.4 KB
/
Copy pathindex.js
File metadata and controls
574 lines (488 loc) · 17.4 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
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/***************************************************
getreact cli -- https://code4mk.org
Authored by code4mk <https://twitter.com/code4mk>
****************************************************/
// ----------------------
// IMPORTS
// Node
const os = require('os');
const path = require('path');
// Third-party
const boxen = require('boxen');
const chalk = require('chalk');
const yargs = require('yargs');
const emoji = require('node-emoji');
const updateNotifier = require('update-notifier');
const semver = require('semver');
const inquirer = require('inquirer');
const spdx = require('spdx');
const fse = require('fs-extra');
const spawn = require('cross-spawn');
const temp = require('temp').track();
const yauzl = require('yauzl');
const request = require('request');
const mkdirp = require('mkdirp');
const sortObj = require('sort-object');
// Local
const banner = require('./banner.js');
const usage = require('./usage.js');
const pkg = require('../package.json');
// ----------------------
const kit = {
version: '1.0.1',
}
// Notice placeholder, for displaying a message back to the user before
// key events -- like prior to starting a new project, or checking the version
let notice;
/*
Helper functions
*/
// If there's a notice to display, show it -- otherwise, ignore.
function showNotice() {
if (notice) console.log(notice);
}
// Show error message. We'll use this if yarn/npm throws back a non-zero
// code, to display the problem back to the console
function fatalError(msg) {
console.error(`
${chalk.bold.bgRed('ERROR')}${chalk.bgRed(' -- See the output below:')}
${msg}
`.trim());
process.exit(1);
}
// Finished instructions. Show the user how to use the starter kit
function finished(dir) {
return `
${separator}
${emoji.get('rocket')} Let's go ....
First, navigate to your new project folder:
${chalk.bgRed.white(`cd ${dir}`)}
Start production server on ${chalk.cyan.underline('http://localhost:8080')}
${chalk.bgRed.white('npm run dev')}
${separator}
`.trim();
}
// ASCII chars to show possible 'spinner' positions. We'll prefix this to
// the installation message during yarn/npm packaging
const spinner = [
'/',
'|',
'\\',
'-',
];
// Validation functions. Used to validate args and Inquirer questions
const validate = {
// Helper function to determine whether the value passed in via options
// also validates against the Inquirer validation function
option(value, func) {
if (typeof value === 'undefined') return true;
if (func(value) !== true) return true;
return false;
},
// Project name. Accept a-z/0-9, optionally separated by a hypen or underscore
// between words. No spaces. 1-32 characters.
name(val) {
if (!/^.{1,32}$/i.test(val)) {
return 'Between 1-32 characters only';
}
return /^([a-z0-9]+[-_]?)*[a-z0-9]$/i.test(val)
|| 'Alphanumeric only. No spaces. One hypens/underscore between words allowed.';
},
// Project description. Accept any character, max length 64.
desc(val) {
if (!/^.{0,64}$/i.test(val)) {
return 'Maximum of 64 characters, please.';
}
return true;
},
// Project description. Accept any character, max length 32
license(val) {
if (!/^UNLICENSED$/.test(val) && !spdx.valid(val)) {
return 'Invalid license. Enter "UNLICENSED" if not licensed.';
}
return true;
},
// Directory to install to. Creates the folder during the Validation
// process, which means it must not already exist
path(val) {
// Assume the directory exists to begin with
try {
const stats = fse.statSync(val);
// Do we have a directory?
if (!stats.isDirectory()) {
return 'Path must be a folder.';
}
// Is the folder empty?
if (fse.readdirSync(val).length) {
return 'Path must be a new, empty folder.';
}
} catch(e) {
// Error accessing folder. Let's now try to create it.
try {
fse.mkdirSync(val);
} catch(_) {
return 'Could not create directory. Enter another path.';
}
}
return true;
}
};
// Get the ZIP stream. This is used for both first-time installs as well
// as upgrades
function startInstallation(installationPath, isUpgrade = false) {
// Create a tmp file stream to save the file locally
const file = temp.createWriteStream();
// Path to `package.json`
const packagePath = path.resolve(installationPath, 'package.json');
// If we're upgrading, there ought to be a `package.json` file in the
// installation path that we'll want to merge with. Load that into
// memory, so we can merge it with the new `package.json` later
let existingPackageJson;
if (isUpgrade) {
try {
existingPackageJson = fse.readJsonSync(packagePath);
} catch (e) { /* ignore any errors */ }
}
// Show the separator to make it clear we've moved on to the
// next step
console.log(separator);
console.log(`Downloading kit v${kit.version} from Github...`);
// Download the .zip containing the kit's source code
request
.get(`https://github.com/code4mk/webpack-react/archive/${kit.version}.zip`)
.pipe(
file.on('finish', () => {
console.log('Extracting archive...');
yauzl.open(file.path, { lazyEntries: true }, (e, zip) => {
if (e) fatalError("Couldn't read zip file");
// Kick-start reading the first zip entry
zip.readEntry();
// Process zip files as they're streamed
zip.on('entry', entry => {
// Remove leading folder that Github uses
const fileName = entry.fileName
.split('/')
.slice(1)
.join('/');
// Files to skip
const skip = [
/^README\.md$/,
/^CHANGELOG\.md$/,
/^LICENSE$/,
];
// Skip `src/*` if upgrading
if (isUpgrade) skip.push(/^src\//);
// If the current file is one to skip, move on...
if (skip.find(toSkip => toSkip.test(fileName)))
return zip.readEntry();
// Proceed only if we have a file name
if (fileName) {
// Resolve the full file name, including the path
const fullName = path.resolve(installationPath, fileName);
// If it's a folder (based on original filename), create it
if (/\/$/.test(fileName)) {
mkdirp(fullName, e => {
if (e) fatalError(`Couldn't create folder ${fullName}`);
zip.readEntry();
});
} else {
// Otherwise, it's a regular file -- write it
zip.openReadStream(entry, (e, readStream) => {
if (e) fatalError(`Couldn't create ZIP read stream`);
readStream
.pipe(fse.createWriteStream(fullName))
.on('finish', () => zip.readEntry());
});
}
} else {
// Blank filename - move on to the next one
zip.readEntry();
}
})
.on('end', () => {
// If we're upgrading a kit...
if (isUpgrade) {
// Delete common NPM lock files
['yarn.lock', 'package-lock.json'].forEach(lockFile => {
try {
fse.removeSync(path.resolve(installationPath, lockFile));
} catch (e) { /* ignore errors */ }
});
// If we have an existing `package.json`, merge it with the old
if (existingPackageJson) {
console.log('Merging package.json...');
try {
// Load the new `package.json`
const newPackageJson = fse.readJsonSync(packagePath);
// Merge `dependencies`, `devDependencies` and `scripts`
['dependencies', 'devDependencies', 'scripts'].forEach(key => {
existingPackageJson[key] = sortObj(Object.assign({},
existingPackageJson[key],
newPackageJson[key]
));
});
// Overwrite `package.json`
fse.writeJsonSync(packagePath, existingPackageJson);
} catch (e) { /* ignore errors */ }
}
} else {
// Only if we're installing a new kit...
const pkgJsonFile = path.resolve(installationPath, 'package.json');
const pkgJson = require(pkgJsonFile);
fse.writeJsonSync(
pkgJsonFile,
Object.assign(pkgJson, {
name: args.name,
description: args.desc,
license: args.license,
}),
{
spaces: 2,
}
);
}
// Add/edit `.getreact` file containing the current version, to enable
// later upgrades
fse.writeFileSync(
path.resolve(installationPath, '.getreact'),
kit.version
);
// Install package dependencies using NPM
const installer = ['yarn', ['install']];
// Create a bottom bar to display the installation spinner at the bottom
// of the console.
const ui = new inquirer.ui.BottomBar({ bottomBar: spinner[0] });
// Temporary var to track the position of the 'spinner'
let i = 0;
// Update the spinner every 300ms, to reflect the installation activity
const update = setInterval(function () {
ui.updateBottomBar(`\n${spinner[++i % 4]} Installing modules -- Please wait boss...`);
}, 300);
// Execute yarn/npm as a child process, pipe output to stdout
spawn(...installer, {cwd: installationPath, stdio: 'pipe'})
.stdout.pipe(ui.log)
.on('error', () => fatalError("Couldn't install packages"))
// When finished, stop the spinner, update with usage instructons and exit
.on('close', function () {
clearInterval(update);
ui.updateBottomBar('');
// If running on Windows, `node rebuild node-sass` to fix
// https://github.com/reactql/cli/issues/64
if (os.platform() === 'win32') {
console.log('Rebuilding SASS support for Windows...');
spawn.sync('npm', ['rebuild', 'node-sass'], {
cwd: installationPath,
stdio: 'inherit',
});
}
if (isUpgrade) {
console.log(
chalk.green(`Upgraded project to ReactQL kit v${kit.version}`)
);
} else {
console.log(finished(installationPath));
}
process.exit();
});
});
})
})
)
.on('error', () => {
console.error("Couldn't download source code from Github");
process.exit(1);
});
}
// Banners / text snippets
const separator = `
================================================================================
`;
const preQuestion = `
${separator}
${banner}
Spawning new project...
`.trimLeft();
// Check that the installed Node version meets requirements: 7.6+
if (!semver.gte(process.version, '7.6.0')) {
let warning = `${chalk.bold('Warning')}: You need Node.js 7.6 or above for getreact to work properly.\n`;
warning += `You have ${process.version}. Upgrade @ ${chalk.cyan.underline('https://nodejs.org')}`;
console.log(boxen(chalk.red(warning), { padding: 1 }));
}
// Set command / arg options. A user can specify args/switches to the `reacql`
// command, or just type `reactql new` and walk through the 'wizard'
const args = yargs
.usage(usage)
.command({
command: 'upgrade',
aliases: ['u'],
desc: `Upgrade existing getreact project to <getreact/> v${kit.version}`,
handler() {
const cwd = process.cwd();
let currentVersion;
try {
// Check that we're inside an active ReactQL project by looking for a
// `.reactql` file, and attempting to read its contents
try {
currentVersion = fse.readFileSync(path.resolve(cwd, '.reactql'), 'utf8').trim();
} catch(e) {
throw new Error('This is not a React-Pack project folder - `.reactql` missing');
}
// Is the contents of `.reactql` a valid semver?
if (!semver.valid(currentVersion)) {
throw new Error('Invalid getreact version inside `.reactql`');
}
} catch (e) {
console.log(
chalk.red(`Error: ${e.message}`)
);
process.exit(1);
}
// Is there even a newer version available?
if (!semver.lt(currentVersion, kit.version)) {
console.log(`Already at latest version (v${currentVersion})`);
return;
}
// Confirm that the user is happy to overwrite existing files
let warning = `${chalk.red.underline('Warning:')} Your getreact project will be `;
warning += `upgraded from ${chalk.dim(currentVersion)} -> ${chalk.green(kit.version)}\n`;
warning += `By proceeding, existing getreact files/dirs will be overwritten (except ${chalk.bgYellow('src/*')})`;
console.log(
boxen(warning, {
padding: 1,
})
);
const questions = [
{
name: 'confirm',
type: 'confirm',
message: `Upgrade to getreact <getreact/> v${kit.version}?`,
},
];
// Once questions have been answered, we'll have an `answers` object
// containing the responses
inquirer.prompt(questions).then(answers => {
// If the user has elected not to upgrade, simply return
if (!answers.confirm) return;
// Yup, upgrade is a go...
startInstallation(cwd, true);
});
}
})
.command({
command: 'new',
aliases: ['n'],
desc: 'Create new project',
handler(args) {
console.log(preQuestion);
showNotice();
// Questions/options
const questions = [
{
name: 'name',
type: 'input',
message: 'Project/Folder name?',
default: 'React-Pack',
validate: validate.name,
when: validate.option(args.name, validate.name),
},
{
name: 'desc',
type: 'input',
message: 'Project description?',
default: 'New React-Pack project',
validate: validate.desc,
when: validate.option(args.desc, validate.desc),
},
{
name: 'license',
type: 'input',
message: 'License?',
default: 'UNLICENSED',
validate: validate.license,
when: validate.option(args.license, validate.license),
},
{
name: 'path',
type: 'input',
message: 'Where to install?',
default(answers) {
const dir = (answers.name || args.name).toLowerCase();
let current = process.cwd();
return dir ? path.resolve(current, dir) : current;
},
validate: validate.path,
when: validate.option(args.path, validate.path),
},
];
// Once questions have been answered, we'll have an `answers` object
// containing the responses
inquirer.prompt(questions).then(answers => {
// Inject answers to our `args`, so that we've got a complete set
// of options to use
Object.assign(args, answers);
// Modify path to be absolute
args.path = path.resolve(process.cwd(), args.path);
// Install the kit's .zip to the required folder
startInstallation(args.path, false);
});
},
})
.command({
command: 'version',
aliases: ['v'],
desc: 'Show getreact version',
handler() {
console.log(pkg.version);
showNotice();
},
})
.options({
name: {
alias: 'n',
describe: 'New project name',
},
desc: {
alias: 'd',
describe: 'Project description',
},
path: {
alias: 'p',
describe: 'Path to install the <getreact/>',
},
license: {
alias: 'l',
describe: 'License for pkg.json',
},
})
.help();
// Check for ReactQL updates automatically. This overrides `update-notifier`'s
// default behaviour of checking in a separate child process (and then displaying
// the message the next time it's run), since it's likely a user would want to
// know if an update was available before starting a new project
updateNotifier({
pkg,
updateCheckInterval: 0,
callback(e, update) {
// If there's an error checking for updates, direct the user to NPM
if (e) {
notice = chalk.red(`Error checking for getreact updates. Try ${chalk.bold.underline('https://www.npmjs.com/package/reactpack')}`);
} else if (update && semver.lt(pkg.version, update.latest)) {
// There's an update available...
notice = `${chalk.bold.magenta('getreact')} CLI update available: `;
notice += `${chalk.dim(pkg.version)} -> ${chalk.bold.green(update.latest)}\n\n`;
notice += `Run ${chalk.cyan('npm i -g getreact')} to upgrade.`;
// Wrap `notice` in a box to make it obvious
notice = boxen(notice, {
padding: 1,
borderColor: 'green',
});
}
// Parse the CLI arguments via `yargs`
const cli = args.argv;
// If no arguments are given, show the help
if (!cli._.length) {
yargs.showHelp();
showNotice();
}
}
});