-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
90 lines (78 loc) · 2.74 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
#!/usr/bin/env node
var config = (process.argv.indexOf('--config') !== -1);
var boiler = (process.argv.indexOf('--boiler') !== -1);
var safe = (process.argv.indexOf('--safe') !== -1);
var fs = require('fs');
var path = require('path');
var ncp = require('ncp').ncp;
var parentRoot = path.resolve(__dirname).split('/node_modules')[0] + '/';
var thisRoot = path.resolve(__dirname) + '/';
if (config) {
copySampleConfig();
}
else if (boiler) {
copyBoilerplateBuild();
}
else {
printHelp();
}
function printHelp() {
console.log('This utility helps keep your webpack build up to date and provides an Angular app boilerplate to start from. It recongnizes two arguments: "--config" and "--boiler"');
console.log('"ng2-webpack --boiler" will install the app and build boilerplate code into your current directory');
console.log('"ng2-webpack --config" will create/update a sample.webpack.config.js file with the latest build goodness so you can see what is new');
}
function copySampleConfig() {
console.log('copying sample webpack config for comparison purposes');
fs.createReadStream(thisRoot + 'webpack.config.js').pipe(fs.createWriteStream(parentRoot + 'sample.webpack.config.js'));
}
function copyBoilerplateBuild() {
var locations = ['entry', 'config', 'app', 'typings', 'webpack.config.js', 'tsconfig.json', 'index.html'];
var test = false;
var index = 0;
console.log('preparing to copy webpack config and app boilerplate into your project');
while (index < locations.length && !test) {
test = checkIfExists(parentRoot + locations[index]);
index++;
}
if (test instanceof Error) {
console.log('something went wrong, aborting. Error: ', test);
}
else if (test) {
if (safe) {
console.log('safe option specified, files not overwritten');
return;
}
process.stdout.write('this is a hard copy and will overwrite your typings, app, entry, and config directories, as well as your webpack.config.js, tsconfig.json and index.html files. Continue? (y/n) ');
process.stdin.once('data', function (buffer) {
var text = buffer.toString().trim().toLowerCase();
if (text === 'y' || text === 'yes') {
hardCopyAll(locations);
}
else {
console.log('aborting');
process.exit();
}
});
}
else {
hardCopyAll(locations);
}
}
function checkIfExists(fullPath) {
try {
fs.lstatSync(fullPath);
return true;
}
catch (err) {
if (err.code == 'ENOENT') return false;
return err;
}
}
function hardCopyAll(locations) {
if (!locations.length) process.exit();
var current = locations.shift();
ncp(thisRoot + current, parentRoot + current, function (err) {
if (err) console.log('error copying file ' + current, err);
hardCopyAll(locations);
});
}