This repository has been archived by the owner on Sep 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
pack.js
338 lines (303 loc) · 9.52 KB
/
pack.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
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
/**
* Node dependencies
**/
import path from 'path';
import del from 'del';
/**
* NPM dependencies
**/
import chalk from 'chalk';
import emoji from 'node-emoji';
import fs from 'fs-extra';
import cp from 'child_process';
import tarball from 'tarball-extract';
import prettyHrtime from 'pretty-hrtime';
import opener from 'opener';
import rimraf from 'rimraf';
import webpack from 'webpack';
import WebpackDevServer from 'webpack-dev-server';
const delimiter = chalk.magenta('[grommet:pack]');
const ENV = process.env.NODE_ENV || 'production';
let port = process.env.PORT || 3000;
function deleteDistributionFolder() {
if (ENV === 'production') {
return new Promise((resolve, reject) => {
console.log(
`${delimiter}: Deleting previously generated distribution folder...`
);
rimraf(path.resolve('dist'), (err) => err ? reject(err) : resolve());
});
} else {
// in dev mode, all resources are compiled and served from memory by
// webpack-dev-server so there is no reason to delete the dist folder
return Promise.resolve();
}
}
function runDevServer(compiler, devServerConfig) {
console.log(
`${delimiter}: Starting dev server...`
);
const devServer = new WebpackDevServer(compiler, devServerConfig);
if (!process.env.PORT && devServerConfig.port) {
port = devServerConfig.port;
}
devServer.listen(port, (err, result) => {
if (err) {
throw err;
}
});
}
function build(config, options) {
return new Promise((resolve, reject) => {
let handleResponse;
// only handle response for production mode
if (ENV === 'production') {
handleResponse = (err, stats) => {
const statHandler = (stat) => {
if (err) {
reject(err);
} else if (stat.compilation.errors.length) {
reject(stat.compilation.errors);
} else {
console.log(stat.toString({
chunks: false,
colors: true
}));
}
};
if (stats.stats) { // multiple stats
stats.stats.forEach(statHandler);
} else {
statHandler(stats);
}
resolve();
};
}
const compiler = webpack(config, handleResponse);
if (ENV === 'development') {
const startServer = (devServerConfig) => {
let firstCompilation = true;
compiler.plugin('done', (stats) => {
const statHandler = (stat) => {
if (stat.compilation.errors.length) {
errorHandler(stat.compilation.errors);
} else {
console.log(stat.toString({
chunks: false,
colors: true
}));
console.log(
`${delimiter}: ${chalk.green('success')}`
);
if (!options['skip-open'] && firstCompilation) {
// https can be an object or just a boolean but either way will
// be truthy when it is turned on
const protocol = devServerConfig.https ? 'https' : 'http';
console.log(
`${delimiter}: Opening the browser at ${protocol}://localhost:${port}`
);
opener(`${protocol}://localhost:${port}`);
}
firstCompilation = false;
}
};
if (stats.stats) { // multiple stats
stats.stats.forEach(statHandler);
} else {
statHandler(stats);
}
});
runDevServer(compiler, devServerConfig);
};
let devServerConfig = config.devServer;
if (!devServerConfig && Array.isArray(config)) {
config.some((c) => {
if (c.devServer) {
devServerConfig = c.devServer;
return true;
}
return false;
});
}
if (devServerConfig) {
startServer(devServerConfig);
} else {
getDevServerConfig().then(startServer);
}
}
});
}
function getWebpackConfig() {
return new Promise((resolve, reject) => {
let webpackConfig = path.resolve(process.cwd(), 'webpack.config.js');
fs.exists(webpackConfig, (exists) => {
if (exists) {
resolve(require(webpackConfig));
} else {
webpackConfig = path.resolve(process.cwd(), 'webpack.config.babel.js');
fs.exists(webpackConfig, (exists) => {
if (exists) {
resolve(require(webpackConfig).default);
} else {
reject('Webpack config not found');
}
});
}
});
});
}
function getDevServerConfig() {
return new Promise((resolve, reject) => {
let devServerConfig = path.resolve(process.cwd(), 'devServer.config.js');
fs.exists(devServerConfig, (exists) => {
if (exists) {
console.warn(
`${delimiter}: devServerConfig has been deprecated. Move your configuration to webpack.config devServer entry.`
);
resolve(require(devServerConfig));
} else {
devServerConfig = path.resolve(
process.cwd(), 'devServer.config.babel.js'
);
fs.exists(devServerConfig, (exists) => {
if (exists) {
console.warn(
`${delimiter}: devServerConfig has been deprecated. Move your configuration to webpack.config devServer entry.`
);
resolve(require(devServerConfig).default);
} else {
reject('devServer config not found');
}
});
}
});
});
}
function packProject(options) {
return new Promise((resolve, reject) => {
console.log(
`${delimiter}: Running webpack...`
);
getWebpackConfig().then(
(config) => build(config, options).then(resolve, reject), reject
);
});
}
function projectLicenses(options) {
return new Promise((resolve, reject) => {
console.log(
`${delimiter}: Evaluating licenses...`
);
const packageJSON = path.resolve('package.json');
const packageJSONAsString = fs.readFileSync(packageJSON);
const json = JSON.parse(packageJSONAsString);
if (json.dependencies) {
json.bundledDependencies = Object.keys(json.dependencies);
fs.writeFileSync(packageJSON, JSON.stringify(json, null, 2));
}
try {
cp.exec('npm pack', (packErr, stdout, stderr) => {
console.log(stdout);
console.error(stderr);
if (packErr) {
throw packErr;
}
const licenseMap = {
name: json.name,
version: json.version,
dependencies: { licenseNotFound: [] }
};
const tarballName = `${json.name}-${json.version}.tgz`;
tarball.extractTarball(tarballName, './tmp', (err) => {
if (err) {
throw err;
}
fs.renameSync(
path.resolve(tarballName),
path.resolve(`${json.name}-${json.version}-src-with-dependecies.tgz`)
);
const dependencies = fs.readdirSync('./tmp/package/node_modules');
dependencies.forEach((dependency) => {
const dependencyPackageJSON = path.resolve(
`node_modules/${dependency}/package.json`
);
const contents = fs.readFileSync(dependencyPackageJSON);
const instance = JSON.parse(contents);
let license = instance.license;
if (!license && instance.licenses) {
license = instance.licenses[0];
}
if (!license) {
licenseMap.dependencies.licenseNotFound.push(dependency);
} else if (license.type) {
licenseMap.dependencies[dependency] = license.type;
} else {
licenseMap.dependencies[dependency] = license;
}
});
const dependencyLicense = path.resolve(
`${json.name}-${json.version}-licenses.json`
);
// write dependency license map
fs.writeFileSync(dependencyLicense, JSON.stringify(
licenseMap, null, 2)
);
// revert original package.json
fs.writeFileSync(packageJSON, JSON.stringify(
JSON.parse(packageJSONAsString), null, 2)
);
del.sync(['./tmp']);
resolve();
});
});
} catch (e) {
console.log(e);
// revert original package.json
fs.writeFileSync(packageJSON, JSON.stringify(
JSON.parse(packageJSONAsString), null, 2)
);
reject(e);
}
});
}
function errorHandler(err = {}) {
console.log(
`${delimiter}: ${chalk.red('failed')}`
);
const isArray = Array.isArray(err);
if (isArray) {
err.forEach(e => console.error(e.message ? e.message : e));
} else {
console.error(err.message ? err.message : err);
}
}
export default function (vorpal) {
vorpal
.command(
'pack',
'Builds a grommet application for development and/or production'
)
.option('--skip-open', 'Skip browser opening on first compilation')
.option('--licenses', 'Generate license information')
.action((args, cb) => {
const timeId = process.hrtime();
deleteDistributionFolder()
.then(() => packProject(args.options))
.then(() => {
if (args.options.licenses) {
return projectLicenses();
}
return Promise.resolve();
})
.then(() => {
console.log(
`${delimiter}: ${chalk.green('success')}`
);
const t = process.hrtime(timeId);
console.log(`${emoji.get('sparkles')} ${prettyHrtime(t)}`);
}).catch((err) => {
errorHandler(err);
process.exit(1);
});
});
};