forked from smogon/damage-calc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build
executable file
·69 lines (61 loc) · 2.49 KB
/
build
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
#!/usr/bin/env node
'use strict';
const path = require('path');
const fs = require('fs');
const crypto = require('crypto');
const rootDir = '.';
// update version
function makeCachebuster(template, output) {
let indexContents = fs.readFileSync(path.resolve(rootDir, template), {encoding: 'utf8'});
// add hashes to js and css files
process.stdout.write("Updating hashes... ");
indexContents = indexContents.replace(/(src|href)="\.\/(.*?)\?[a-z0-9]*?"/g, function (a, b, c) {
let hash = Math.random(); // just in case creating the hash fails
try {
const filename = c.replace('//play.pokemonshowdown.com/', '../../play.pokemonshowdown.com/');
const fstr = fs.readFileSync(path.resolve(rootDir, 'dist', filename), 'utf8');
hash = crypto.createHash('md5').update(fstr).digest('hex').substr(0, 8);
} catch (e) {}
return b + '="./' + c + '?' + hash + '"';
}).replace(/\.template\.html/g, '.html');
process.stdout.write("Writing new `" + output + "` file... ");
fs.writeFileSync(path.resolve(rootDir, output), indexContents);
console.log("DONE");
}
function cpdir(src, dest) {
console.log("Copying `" + src + "` to `" + dest + "`");
try {
fs.mkdirSync(dest, '0755');
} catch(e) {
if (e.code != 'EEXIST') throw e;
}
for (const file of fs.readdirSync(src)) {
var current = fs.lstatSync(path.join(src, file));
if (current.isDirectory()) {
cpdir(path.join(src, file), path.join(dest, file));
} else if (current.isSymbolicLink()) {
var symlink = fs.readlinkSync(path.join(src, file));
fs.symlinkSync(symlink, path.join(dest, file));
} else if (file.endsWith('.js')) {
let contents = fs.readFileSync(path.join(src, file), 'utf8').replace(/^exports.* = void 0;$/gm, '');
// Minify the sets so we save bandwith
if (src === 'src/js/data/sets') {
const brkOpenIdx = contents.indexOf('{');
contents = `${contents.slice(0, brkOpenIdx)}${JSON.stringify(JSON.parse(contents.slice(brkOpenIdx, -2)))};`
}
fs.writeFileSync(path.join(dest, file), contents);
} else {
fs.copyFileSync(path.join(src, file),path.join(dest, file));
}
}
}
if (process.argv[2] !== 'view') {
require('child_process').execSync('npm --prefix calc/ run compile', {stdio: 'inherit'});
}
cpdir('src', 'dist');
cpdir('calc/dist', 'dist/calc');
makeCachebuster('dist/honkalculate.template.html', 'dist/honkalculate.html');
makeCachebuster('dist/index.template.html', 'dist/index.html');
makeCachebuster('dist/randoms.template.html', 'dist/randoms.html')
makeCachebuster('dist/oms.template.html', 'dist/oms.html');
;