This repository has been archived by the owner on May 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
126 lines (113 loc) · 3.59 KB
/
build.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
const child_process = require('child_process');
const fs = require('fs');
const path = require('path');
const yargs = require('yargs');
const argv = yargs
.option('test', {
boolean: true,
default: false,
describe: 'If set to true, the programm will build a test env'
})
.option('prefixURL', {
string: true,
default: '',
describe: 'Used to set the prefix URL for proxies'
}).argv;
const buildOptions = {
ignore: [
'test',
'temp',
'data',
'*.ts',
'*.html',
'*.log',
'.github',
'.git',
'build',
'update.zip',
'tsconfig.json',
'webpack.config.js',
'.eslintrc.json',
'settings.json'
]
};
if (!argv.test) {
buildOptions.ignore.push(
'SSL' + path.sep + 'server.crt',
'SSL' + path.sep + 'server.key',
'node_modules',
)
}
deleteDir('build');
const tsc = child_process.exec('tsc -p tsconfig.json');
tsc.on('close', (code) => console.log(`[Typescript] Finished with code ${code}`));
tsc.on('error', (err) => console.log(`[Typescript] ${err}`));
tsc.on('message', (msg) => console.log(`[Typescript] ${msg}`));
tsc.on('close', () => {
const webpack = child_process.exec(`npx webpack --env PREFIX_URL=${argv.prefixURL} ${!argv.test ? '--mode production' : ''}`);
webpack.on('close', (code) => console.log(`[Webpack] Finished with code ${code}`));
webpack.on('error', (err) => console.log(`[Webpack] ${err}`));
webpack.on('message', (msg) => console.log(`[Webpack] ${msg}`));
webpack.on('close', () => {
let copy = getAllFiles('.').filter(a => {
return buildOptions.ignore.every(ign => {
if (ign.match(/\*\.[A-Za-z]/)) {
if (path.extname(a) === ign.substring(1))
return false;
} else {
if (path.resolve(a).startsWith(path.resolve(ign)))
return false;
}
return true;
}) && fs.lstatSync(a).isFile();
});
copy.forEach(c => {
createParents(path.join('build', c).split(path.sep).slice(0, -1).join(path.sep));
fs.copyFile(c, path.join('build', c), (err) => {
if (err) {
console.log(err);
console.log('❌', c);
process.exit(1);
}
});
});
console.log('Copyed files');
});
});
function getAllFiles(p) {
let files = [];
if (!fs.existsSync(p))
return files;
if (fs.statSync(p).isDirectory()) {
for (let f of fs.readdirSync(p)) {
let stats = fs.statSync(path.join(p, f));
if (stats.isDirectory())
files = files.concat(getAllFiles(path.join(p, f)));
else
files.push(path.join(p, f));
}
}
files.push(p);
return files;
}
function createParents(p) {
p.split(path.sep).forEach((_, index, array) => {
if (!fs.existsSync(array.slice(0, index+1).join(path.sep)))
fs.mkdirSync(array.slice(0, index+1).join(path.sep));
});
}
function deleteDir(p) {
if (!fs.existsSync(p))
return;
if (fs.statSync(p).isDirectory()) {
for (let f of fs.readdirSync(p)) {
let stats = fs.statSync(path.join(p, f));
if (stats.isDirectory())
deleteDir(path.join(p, f));
else
fs.unlinkSync(path.join(p, f));
}
fs.rmdirSync(p);
} else
fs.unlinkSync(p);
}