/
build.js
234 lines (183 loc) · 5.88 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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// node.js based build script
// run `node build -h` for usage information
var _helpers = require('./_build/helpers');
var _config = require('./_build/config');
var _cli = require('commander');
var _path = require('path');
_cli
.option('-s, --silent', 'suppress log messages.');
_cli
.command('doc')
.description('build documentation.')
.action( cmd(buildDocs) );
_cli
.command('pkg')
.description('update packages and specs')
.action( cmd(updatePackages) );
_cli
.command('add <moduleName> [templateName]')
.description('add a new module.')
.action( cmd(addModule) );
_cli
.command('mv <moduleName> <newName>')
.description('rename module.')
.action( cmd(renameModule) );
_cli
.command('cjs <destinationPath>')
.description('convert mout into a node.js compatible package.')
.action( cmd(convert) );
_cli
.command('release <version>')
.description('bump version number, run tests, git tag, publish')
.action( cmd(release) );
_cli
.command('testling')
.description('prepare files for ci.testling.com')
.action( cmd(generateTestBundle) );
_cli.parse(process.argv);
// show help by default
if (!_cli.args.length) {
_cli.outputHelp();
process.exit(0);
}
// just a helper to run multiple commands in sequence and process global
// options before executing them.
function cmd(var_args){
var fns = Array.prototype.slice.call(arguments);
return function(){
if (_cli.silent) {
_helpers.isSilent = true;
}
var args = Array.prototype.slice.call(arguments);
fns.forEach(function(fn){
fn.apply(null, args);
});
};
}
// --
function buildDocs(){
var mdoc = require('mdoc');
mdoc.run({
inputDir : 'doc',
outputDir : 'doc_html',
baseTitle : 'mout',
indexContentPath : 'README.md',
templatePath : '_build/doc_template'
});
}
function updatePackages(){
var pkg = require('./_build/package');
pkg.updateAllPackages();
}
function addModule(moduleName, templateName){
var add = require('./_build/add');
add.createSource(moduleName, templateName);
add.createSpec(moduleName);
}
function renameModule(moduleName, newName){
var rename = require('./_build/rename');
rename.renameModule(moduleName, newName);
}
function convert(destinationPath){
var glob = _path.join(_config.SRC_FOLDER, '/**/**.js');
_helpers.echo('Converting modules to node.js:');
require('nodefy').batchConvert(glob, destinationPath, function(err, results){
results = results.map(function(r){ return r.outputPath; });
_helpers.echoList(results);
_helpers.echo('Finished node.js conversion');
});
}
function release(version){
if (! version) throw new Error('version is a required argument');
version = String(version).replace(/[^\d\.]/g, '');
var remote = require('./package.json').repository.url;
updateJsonVersion('./package.json', version);
updateJsonVersion('./component.json', version);
updateChangelog('./CHANGELOG.md', version);
_helpers.shellSeries([
// make sure we checkout master so git will warn about uncommited
// changes and it will also avoid publishing wrong branch
'git checkout master',
// scripts.pretest already generates the packages
'npm test --coverage',
'git add -A',
'git commit --verbose',
'git tag v'+ version,
'git push '+ remote,
'git push --tags',
// scripts.prepublish already generates the cjs modules
'npm publish',
// TODO: use node instead of a shell script (so it works on windows)
'sh _build/clean.sh'
], function(err){
if (err) {
console.log(err.toString());
process.exit(1);
}
});
}
function updateJsonVersion(path, version) {
var target = require(path);
var fs = require('fs');
target.version = version;
fs.writeFileSync(path, JSON.stringify(target, null, ' '), _config.ENCODING);
}
function updateChangelog(path, version) {
var fs = require('fs');
var content = fs.readFileSync(path).toString();
var release = 'v'+ version +' ('+ today() +')';
var dashedLine = (new Array(release.length + 1)).join('-');
// add version number and release date to changelog if first h2 doesn't
// start with "v"
content = content.replace(/^(?!v)[^\n]+\n\-+/m, release +'\n'+ dashedLine);
fs.writeFileSync(path, content, _config.ENCODING);
}
function today(){
// YYYY/MM/DD
var date = new Date();
var dateString = '';
dateString += date.getFullYear();
dateString += '/'+ pad(date.getMonth() + 1);
dateString += '/'+ pad(date.getDate());
return dateString;
}
function pad(val){
val = String(val);
if (val.length < 2) {
val = '0'+ val;
}
return val;
}
// ci.testling.com can't load relative resources so we need to bundle all the
// source files and tests into few files to make it work properly.
// we can't use rawgithub.com to load the files dynamically since it can
// timeout and was causing more issues (see #10).
function generateTestBundle(){
var rjs = require('requirejs');
_helpers.echo('generateTestBundle: generating AMD bundles...');
// yes, it's ugly but works for now.
rjs.optimize({
logLevel : 3,
baseUrl : '.',
optimize: 'none',
name : 'mout/index',
paths : {
'mout' : 'src'
},
out : 'tests/testling/src.js'
}, function(){
rjs.optimize({
logLevel : 3,
baseUrl : '.',
optimize: 'none',
name : 'spec/spec-index',
paths : {
'mout' : 'empty:',
'spec' : 'tests/spec'
},
out : 'tests/testling/specs.js'
}, function(){
_helpers.echo('generateTestBundle: done.');
});
});
}