-
Notifications
You must be signed in to change notification settings - Fork 8
/
jakefile.js
129 lines (109 loc) · 3.93 KB
/
jakefile.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
/* global jake:false */
/* global task:false */
/* global desc:false */
/* global complete:false */
/* global process:false */
var exec = (cmd, arg) => new Promise(resolve => {
jake.exec(cmd, arg || {}, resolve);
});
var invoke = (task) => new Promise(resolve => {
var t = jake.Task[task];
t.addListener('complete', resolve);
t.invoke();
});
function babel(args) {
args = args || {};
var fs = require('fs');
var path = require('path');
var babel = require('babel');
var tsconfig = require(path.resolve('tsconfig.json'));
var sourceMaps = args.sourceMaps === undefined ? true : !!args.sourceMaps;
var srcPath = args.srcPath || tsconfig.compilerOptions.out;
var outPath = args.outPath || srcPath;
var es6src = fs.readFileSync(srcPath, 'utf8');
var es6map = sourceMaps && fs.readFileSync(srcPath + '.map', 'utf8');
var es5 = babel.transform(es6src, {
inputSourceMap: sourceMaps && JSON.parse(es6map),
sourceMaps: sourceMaps,
loose: args.loose === false ? undefined : 'all',
compact: false,
blacklist: args.blacklist || [
'regenerator',
//'es6.forOf',
//'es6.classes',
//'es6.blockScoping',
//'es6.templateLiterals',
]
});
es5.code = es5.code.replace(
/(\r?\n\/\/# sourceMappingURL=)(.+)$/img,
sourceMaps ? '$1' + outPath + '.map' : '$1(removed; see jakefile.js)');
fs.writeFileSync(outPath, es5.code, 'utf8');
sourceMaps && fs.writeFileSync(outPath + '.map', JSON.stringify(es5.map, null, '\t'), 'utf8');
}
desc('Builds the tsumego.js library.');
task('lib', { async: true }, () => {
console.log('building tsumego.js...');
exec('node ./node_modules/typescript/lib/tsc', { printStdout: true }).then(() => {
require('fs').writeFileSync(
'bin/tsumego.npm.d.ts',
'/// <reference path="tsumego.d.ts" />\nexport = tsumego;',
'utf8');
console.log('generating the es5 version...');
babel({
sourceMaps: false, // babel 5.8.23 screws source maps
outPath: 'bin/tsumego.es5.js',
blacklist: []
});
console.log('generating the es6 version...');
babel({
sourceMaps: false, // babel 5.8.23 screws source maps
outPath: 'bin/tsumego.es6.js'
});
}).then(complete);
});
desc('Runs unit tests.');
task('test', ['lib'], { async: true }, filter => {
console.log('building tests.js...');
process.chdir('tests');
exec('node ../node_modules/typescript/lib/tsc', { printStdout: true }).then(() => {
return babel();
}).then(() => {
console.log('npm install...');
return exec('npm i');
}).then(() => {
if (/^debug:/.test(filter)) {
console.log('debugging tests...');
return exec('node-debug tests.js ' + filter.slice(6), { printStdout: true });
} else {
console.log('running tests...');
return exec('node tests.js ' + (filter || ''), { printStdout: true });
}
}).then(() => {
if (!filter)
return exec('node tests mode:es5', { printStdout: true });
}).then(() => {
process.chdir('..');
}).then(complete);
});
desc('Prepares a release package.');
task('release', ['lib'], () => {
console.log('prepairing a release package...');
jake.rmRf('bin/release');
jake.mkdirP('bin');
jake.cpR('release', 'bin');
jake.cpR('libs/regenerator-runtime.js', 'bin/release/regenerator-runtime.js');
var copy = path => jake.cpR('bin/' + path, 'bin/release/' + path);
copy('tsumego.es5.js');
copy('tsumego.es6.js');
copy('tsumego.d.ts');
});
desc('Builds everything and runs the tests.');
task('default', { async: true }, () => {
jake.rmRf('bin');
invoke('lib').then(() => {
return invoke('release');
}).then(() => {
return invoke('test');
}).then(complete);
});