-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
50 lines (37 loc) · 1.17 KB
/
script.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
const fs = require('fs');
const path = require('path');
const { spawn } = require('child_process');
const resolve = (file) => {
return path.resolve(__dirname, file)
}
// const build = spawn('./node_modules/.bin/webpack --config ./webpack.config.js');
if (fs.existsSync(resolve('./dist'))) {
fs.rmdirSync(resolve('./dist'));
};
if (fs.existsSync(resolve('./public/dist'))) {
fs.rmdirSync(resolve('./public/dist'));
}
const build = spawn('npm', ['run', 'build']);
build.stdout.on('data', (data) => {
// console.log('stdout: ', data);
})
build.stderr.on('data', (data) => {
console.log('stderr: ', data);
})
build.on('close', (code) => {
if (code != 0) return;
console.log('build successfully');
const templatePath = resolve('./index.html');
if (!fs.existsSync(templatePath)) return
if (!fs.existsSync(resolve('./dist/main.js'))) return
fs.copyFileSync(templatePath, resolve('./public/dist/index.html'));
console.log('copy successfully');
const js = fs.readFileSync(resolve('./dist/main.js'), {encoding: 'utf8'});
fs.appendFileSync(
resolve('./public/dist/index.html'),
`${js}\n</script>\n</body>\n</html>`,
{
encoding: 'utf8',
}
);
})