-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
101 lines (86 loc) · 2.98 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
console.time("build");
const UglifyJS = require("uglify-js");
const Promise = require('bluebird');
const browserify = require('browserify');
const path = require('path');
const fs = Promise.promisifyAll(require('fs'));
const values = require('lodash.values');
const userscriptUtils = require('userscript-utils');
const crossSpawn = require('cross-spawn');
const promiseSpawn = function () {
const args = arguments;
return new Promise((resolve, reject) => {
crossSpawn.apply(crossSpawn, args)
.on('exit', resolve)
.on('error', reject);
});
};
const files = {
script_src: "battered-shield-helper.src.js",
script_out: "battered-shield-helper.user.js",
meta_in: "metablock.txt",
meta_out: "battered-shield-helper.meta.js"
};
let actions = {};
actions.get_metablock = fs.readFileAsync(path.join(__dirname, files.meta_in), 'utf8');
actions.browserify = new Promise((resolve, reject) => {
browserify({entries: path.join(__dirname, files.script_src)})
.transform('babelify',
{
"presets": [
"stage-3",
"stage-2",
"stage-1",
"stage-0",
"latest"
],
"plugins": [
"transform-es5-property-mutators"
]
}
)
.bundle((e, r) => {
if (e) reject(e);
else resolve(r.toString());
});
});
actions.wrap = actions.browserify.then(js => {
const vars = [
"GM_notification",
"GM_info",
"GM_getResourceText",
"GM_registerMenuCommand",
"GM_getValue",
"GM_setValue",
"GM_listValues",
"buzz",
"Promise",
"ko",
"String",
"document"
].join(",");
return `(function(${vars}){ko.options.deferUpdates = true;${js}})(${vars})`;
});
actions.uglify = actions.wrap.then(js => UglifyJS.minify(js, {fromString: true}).code);
actions.concat_metablock_and_script = Promise.all([actions.get_metablock, actions.uglify])
.spread((metablock, script) => `${metablock}\n${script}`);
actions.write_out_script = actions.concat_metablock_and_script.then(str => {
return fs.writeFileAsync(path.join(__dirname, files.script_out), str + '\n', 'utf8');
});
actions.stringify_upadate_block = actions.get_metablock.then(str => {
return new Promise((resolve, reject) => {
userscriptUtils.getUpdateMetablock.fromString(str, (e, metablock) => {
if (e) reject(e);
else resolve(metablock);
}, true, true)
})
});
actions.save_meta_js = actions.stringify_upadate_block.then(str => {
return fs.writeFileAsync(path.join(__dirname, files.meta_out), str + "\n", 'utf8');
});
actions.doctocChangelog = promiseSpawn('npm', ['run', 'doctoc:changelog']);
actions.doctocReadme = promiseSpawn('npm', ['run', 'doctoc:readme']);
//Finish timer
Promise.all(values(actions)).then(() => {
console.timeEnd("build")
});