-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
executable file
·84 lines (75 loc) · 2.73 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
#!/usr/bin/env node
// @TODO +++ Fix the use of the parser in shepherd and fix the matching of module description and what's expected from Shepherd.
// @TODO + Add an index.js for automatic discovery in NodeJS
var fs = require('fs'),
jsp = require("uglify-js").parser,
pro = require("uglify-js").uglify;
var parserLocation = './lib/harmony-parser/harmony_parser.js',
whenLocation = './lib/when/when.js',
shepherdLocation = './src/shepherd.js';
var flavourConfig = {
'client-dev' : {
min: false,
flavourFile: './src/flavours/browser.js',
outputSuffix: 'dev'
},
'client' : {
min: true,
flavourFile: './src/flavours/browser.js',
outputSuffix: 'min'
},
'server' : {
min: false,
flavourFile: './src/flavours/server.js',
outputSuffix: 'server'
},
'shears' : {
min: false,
flavourFile: './src/flavours/optimizer.js',
outputSuffix: 'optimizer'
}
};
function run (flavourKey) {
var flavour = flavourConfig[flavourKey];
// Scopes the parser so it doesn't pollute globals
// Declaring exports to undefined avoids CommonJS exports
var parserCode = '(function() {var exports;' + fs.readFileSync(parserLocation).toString() + ';return harmony_parser;})();';
var shepherdCode = fs.readFileSync(shepherdLocation).toString();
var whenCode = '(function() {var module = {};' + fs.readFileSync(whenLocation).toString() + ';return module.exports;})();';
var flavourCode = fs.readFileSync(flavour.flavourFile).toString();
var output;
devCode = '(function () {' +
'var harmonyParser = ' + parserCode + ';\n' +
'var when = ' + whenCode + ';\n' +
'var flavour = ' + flavourCode + ';\n' +
shepherdCode +
'})()';
var devAst = jsp.parse(devCode);
if (flavour.min) {
devAst = pro.ast_mangle(devAst, {defines: {MINIFY: ['name', true]}});
devAst = pro.ast_squeeze(devAst);
}
output = pro.gen_code(devAst, {beautify: !flavour.min});
fs.writeFileSync('./build/shepherd.' + flavour.outputSuffix + '.js', output);
}
function build () {
for (var i in flavourConfig) {
if (!flavourConfig.hasOwnProperty(i)) { continue; }
var msg;
try {
run(i);
msg = 'Built Shepherd with flavour ' + i + ' on: ' + (new Date()).toString();
} catch (e) {
msg = 'Build of flavour ' + i + ' failed. (' + e.message + ')';
}
console.log(msg);
}
}
build();
fs.watch(parserLocation, build);
fs.watch(shepherdLocation, build);
fs.watch(whenLocation, build);
for (var i in flavourConfig) {
if (!flavourConfig.hasOwnProperty(i)) { continue; }
fs.watch(flavourConfig[i].flavourFile, build);
}