generated from BloomTech-Labs/labs-spa-starter
-
Notifications
You must be signed in to change notification settings - Fork 62
/
bw-prep.js
143 lines (133 loc) Β· 4.2 KB
/
bw-prep.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
const prompts = require('prompts');
const fs = require('fs');
const { exec } = require('child_process');
const kleur = require('kleur');
const YAML = require('yaml');
/***
* In order to better serve build week teams we want to remove some items that
* can cause distraction from the unit objectives.
*
* Packages removed
* - ant design
* - storybook
* - plotly (if not a team with DS)
*
* Alter the index.js file
* Remove code climate from ci.yml
*/
(async () => {
console.log(kleur.bold().cyan('Preparing Labs Scaffold for BW projects'));
const response = await prompts({
type: 'confirm',
name: 'hasDS',
message: 'Does your team have data science members?',
initial: false,
});
var packages = [
'@storybook/react',
'@storybook/addon-actions',
'@storybook/addon-knobs',
'@storybook/addon-notes',
'@storybook/addons',
'@storybook/storybook-deployer',
'antd',
'prompts',
'kleur',
'yaml',
];
if (!response.hasDS) {
// remove dataviz example component
fs.rmdirSync('./src/components/pages/ExampleDataViz', { recursive: true });
// add plotly packages to be removed
packages.push('plotly.js', 'react-plotly.js');
}
uninstall(packages);
// remove lines from app index.js
alterIndex(response.hasDS);
// change ci.yml
removeCC();
removeThemeFromCraco();
// remove storybook stories
fs.rmdirSync('./src/stories', { recursive: true });
fs.rmdirSync('./.storybook', { recursive: true });
// remove ant files
fs.rmdirSync('./src/styles', { recursive: true });
// remove scaffold .git π
fs.rmdirSync('./.git', { recursive: true });
// copy the .env.sample file to .env
fs.copyFile('./.env.sample', './.env', err => {
if (err) throw err;
console.log('.env.sample was copied to .env');
});
// remove this file π±
fs.unlinkSync('./bw-prep.js');
})();
function removeCC() {
const ciFile = './.github/workflows/ci.yml';
const file = fs.readFileSync(ciFile, 'utf8');
const ci = YAML.parse(file);
//remove the code climate step
ci.jobs.coverage.steps.pop();
//add a build step
ci.jobs.coverage.steps.push({ run: 'npm run build' });
fs.writeFileSync(ciFile, YAML.stringify(ci), 'utf8');
}
function uninstall(packages) {
const packageNames = packages.join(' ');
console.log(kleur.bold(`uninstalling packages: ${packageNames}`));
exec(`npm uninstall ${packageNames}`, (error, stdout, stderr) => {
console.log(stderr);
if (error !== null) {
console.log(kleur.bold().yellow().bgRed(`exec error: ${error}`));
}
// why not run git init for them?
console.log(
kleur.bold().green('Uninstall complete'),
`\n\nThe scaffold is now ready for your Build Week project. You may now run ${kleur
.bold()
.underline('git init')} \n\nππππ π`
);
});
}
function alterIndex(hasDS) {
var indexFile = './src/index.js';
try {
var file = fs.readFileSync(indexFile, 'utf8');
var lines = file.split('\n');
replaceHomePage(lines);
if (!hasDS) lines = removeLines(lines, 'ExampleDataViz');
lines = removeLines(lines, 'antd');
lines = removeLines(lines, 'ProfileListPage');
lines = removeLines(lines, 'HomePage');
lines = removeLines(lines, 'LoadingComponent');
var result = lines.join('\n');
fs.writeFileSync(indexFile, result, 'utf8');
} catch (e) {
console.error(e);
}
}
function removeThemeFromCraco() {
const fileName = './craco.config.js';
const file = fs.readFileSync(fileName, 'utf8');
var lines = file.split('\n');
lines.splice(10, 1);
lines.splice(0, 1);
const result = lines.map(line => line).join('\n');
fs.writeFileSync(fileName, result, 'utf8');
}
function replaceHomePage(lines) {
// super brittle stuff here
lines[49] = lines[49].replace(/SecureRoute/g, 'Route');
lines[54] = lines[54].replace(/SecureRoute/g, 'Route');
lines[52] = lines[52].replace(
/HomePage LoadingComponent={LoadingComponent}/g,
'LandingPage'
);
}
function removeLines(fileLines, matchStr) {
let indexes = [];
for (let i = fileLines.length - 1; i > -1; i--)
if (fileLines[i].match(matchStr)) indexes.push(i);
for (let l = 0; l < indexes.length; l++) fileLines.splice(indexes[l], 1);
return fileLines.map(s => s);
}