-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.js
61 lines (53 loc) · 1.83 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
const fs = require('fs');
const glob = require('glob');
const { exec, execSync } = require("child_process");
const ignoreFilesRegexes = [
/^.*\/\..*$/g,
/^.*\/package\.json$/g,
/^.*\/yarn\.lock$/g
];
console.log('Removing old build directories...');
execSync('rm -rf ./functions/server ./functions/static ./functions/favicons ./functions/node_modules');
console.log('Removing old build files...');
const files = glob.sync('functions/**/*', { 'follow': true, 'dot': true });
files.forEach((file) => {
let ignored = false;
for (const regex of ignoreFilesRegexes) {
if ((file.match(regex) || []).length) {
console.log('Ignore', file);
ignored = true;
break;
}
}
if (!ignored && !fs.statSync(file).isDirectory()) {
fs.unlinkSync(file)
}
});
console.log('Compiling front end app...');
exec('cd ./app && yarn && yarn build', {}, (error, stdout, stderr) => {
if (error) {
console.error('stderr:', stderr);
throw error;
}
console.log('Copying front end build to functions...');
execSync('cp -R ./app/build/ ./functions/');
console.log('Inserting HTML comment for meta into index.html...');
const indexPath = './functions/index.html';
const builtHtml = fs.readFileSync(indexPath, 'utf8');
const finalHtml = builtHtml.replace('<head>', '<head><!-- ::META:: -->'); // Add target comment in <head>
fs.writeFile(indexPath, finalHtml, (error) => {
if (error) {
console.error('stderr:', stderr);
throw error;
}
console.log('Build complete! Run `yarn serveall` to serve locally.');
});
});
console.log('Compiling server code...');
console.log('Reminder to ensure that functions/package.json is caught up with ./package.json');
exec('yarn babel && cd ./functions && yarn', {}, (error, stdout, stderr) => {
if (error) {
console.error('stderr:', stderr);
throw error;
}
});