-
Notifications
You must be signed in to change notification settings - Fork 9
/
bootstrap.js
executable file
·63 lines (46 loc) · 1.88 KB
/
bootstrap.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
#!/usr/bin/env node
const path = require('path');
const fs = require('fs-extra');
const webpackReplacementPath = path.join(__dirname, 'webpack.config.replacement.js');
const strap = 'babel-lerna-loader-cra:';
const log = (msg, data) => {
console.log(strap, msg, data || '');
}
log('bootstraping...');
const config = require(path.join(__dirname, 'loadConfig'))();
log('config =', config);
const webpackConfigPath = (dir, type) => {
return path.join(dir, `node_modules/react-scripts/config/webpack.config.${type}.js`);
}
const webpackBackupPath = (dir, type) => {
return path.join(dir, `node_modules/react-scripts/config/backup.webpack.config.${type}.js`);
}
const prettyAppPath = uglyPath =>
path.parse(path.relative(config.lernaRoot, uglyPath)).dir
.replace(/^packages\//, '')
.replace(/\/node_modules\/react-scripts\/config/, '');
const prettyFile = uglyPath => path.parse(uglyPath).base;
const linkOverride = (reactAppDir, type) => {
const configFile = webpackConfigPath(reactAppDir, type);
const configBackup = webpackBackupPath(reactAppDir, type);
if (fs.existsSync(configBackup)) {
// Backup already exists, copy cancelled.
} else {
log(`copying: ${prettyAppPath(configFile)}/... ${prettyFile(configFile)} => ${prettyFile(configBackup)}`);
fs.copySync(configFile, configBackup, {overwrite: false});
}
log(`copying: ${prettyAppPath(configFile)}/... ${prettyFile(webpackReplacementPath)} => ${prettyFile(configFile)}`);
if (fs.existsSync(configFile)) {
try {
fs.removeSync(configFile);
} catch (error) {
throw new Error('The symlink could not be removed!');
}
}
fs.copySync(webpackReplacementPath, configFile, {overwrite: true});
console.log();
}
config.apps.forEach(reactAppDir => {
linkOverride(reactAppDir, 'dev');
linkOverride(reactAppDir, 'prod');
});