-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-prod.js
134 lines (126 loc) · 3.87 KB
/
build-prod.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
const npmRun = require("npm-run");
const fs = require("fs").promises;
const fse = require("fs-extra");
const path = require("path");
/**
* This script builds a production version of the game, compatible with
* older and more obscure browsers
*/
/** @type{string} set these values to use a custom score server, e.g. "https" */
const SCORE_SERVER_SCHEME = process.env.SCORE_SERVER_SCHEME || undefined;
/** @type{string} set these values to use a custom score server, e.g. "example.com" */
const SCORE_SERVER_DOMAIN = process.env.SCORE_SERVER_DOMAIN || undefined;
/**
* wraps npmRun.run() in a function that has no options and returns a promise to
* avoid callback hell
* @param{string} script the npm script to run
* @return{Promise<void>}
*/
const runPromise = script => {
return new Promise((resolve, reject) => {
npmRun.exec(script, {}, (err, stdout, stderr) => {
if (err) {
console.error(`Error encountered while running command '${script}':`);
console.error(stderr);
process.exit(1);
}
resolve(stdout);
});
});
};
/**
* find and replace text within a file
* @param{string} path file path
* @param{RegExp} find regular expression to find
* @param{string} replace string to replace
* @return{Promise<void>}
*/
const replaceInFile = (path, find, replace) => {
return fs
.readFile(path, "utf8")
.then(contents => fs.writeFile(path, contents.replace(find, replace)));
};
/**
* builds a production version of the game, compatible with older and more
* obscure browsers
* @param{string} scoreServerScheme scheme for custom score server, e.g. "https"
* @param{string} scoreServerDomain domain for custom score server, e.g. "example.com"
* @return{Promise<void>}
*/
const buildProd = (
scoreServerScheme = undefined,
scoreServerDomain = undefined
) => {
console.log("Building production version with Babel...");
return new Promise(resolve => {
runPromise("babel static --out-dir build")
.then(() => runPromise("browserify -vd build/main.js -o dist/bundle.js"))
.then(() =>
fse.copy(
path.join("static", "index.html"),
path.join("dist", "index.html")
)
)
.then(() =>
fse.copy(
path.join("static", "style.css"),
path.join("dist", "style.css")
)
)
.then(() =>
fse.copy(
path.join("static", "license.txt"),
path.join("dist", "license.txt")
)
)
.then(() =>
fse.copy(
path.join("static", "favicon.ico"),
path.join("dist", "favicon.ico")
)
)
.then(() =>
fse.copy(
path.join("static", "anonymous-pro-b.ttf"),
path.join("dist", "anonymous-pro-b.ttf")
)
)
.then(() =>
replaceInFile(
path.join("dist", "index.html"),
/<script type="module" src="main.js"/,
'<script src="bundle.js"'
)
)
.then(() => {
if (
scoreServerScheme !== undefined &&
scoreServerDomain !== undefined
) {
return replaceInFile(
path.join("dist", "bundle.js"),
/var GAME_URL = ".*";/,
`var GAME_URL = "${scoreServerScheme}://${scoreServerDomain}";`
);
} else {
return Promise.resolve();
}
})
.then(() => replaceInFile(path.join("dist", "bundle.js"), /\.\./g, "."))
.then(() =>
fse.copy(path.join("static", "sounds"), path.join("dist", "sounds"))
)
.then(() =>
fse.copy(path.join("static", "images"), path.join("dist", "images"))
)
.then(() => {
console.log("Production version created at dist");
resolve();
})
.catch(reason => {
console.error("Error encountered in buildProd:");
console.error(reason);
});
});
};
buildProd(SCORE_SERVER_SCHEME, SCORE_SERVER_DOMAIN).then();