This repository has been archived by the owner on Aug 6, 2020. It is now read-only.
forked from bevry/domain-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nakefile.js
491 lines (424 loc) · 12.9 KB
/
nakefile.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
// 2015 September 11
// https://github.com/bevry/base
'use strict'
// =====================================
// Imports
const fsUtil = require('fs')
const pathUtil = require('path')
// =====================================
// Variables
// Define environment things
const WINDOWS = process.platform.indexOf('win') === 0
const NODE = process.execPath
const NPM = WINDOWS ? process.execPath.replace('node.exe', 'npm.cmd') : 'npm'
// const EXT = WINDOWS ? '.cmd' : ''
const GIT = 'git'
// Define project paths
const APP_PATH = process.cwd()
const PACKAGE_PATH = pathUtil.join(APP_PATH, 'package.json')
const PACKAGE_DATA = require(PACKAGE_PATH)
const PACKAGE_CONFIG = PACKAGE_DATA.nakeConfiguration || PACKAGE_DATA.cakeConfiguration || {}
// Define module paths
const MODULES_PATH = pathUtil.join(APP_PATH, 'node_modules')
const DOCPAD_PATH = pathUtil.join(MODULES_PATH, 'docpad')
const COFFEE = pathUtil.join(MODULES_PATH, '.bin', 'coffee')
const PROJECTZ = pathUtil.join(MODULES_PATH, '.bin', 'projectz')
const DOCCO = pathUtil.join(MODULES_PATH, '.bin', 'docco')
const DOCPAD = pathUtil.join(MODULES_PATH, '.bin', 'docpad')
const BISCOTTO = pathUtil.join(MODULES_PATH, '.bin', 'biscotto')
const YUIDOC = pathUtil.join(MODULES_PATH, '.bin', 'yuidoc')
const BABEL = pathUtil.join(MODULES_PATH, '.bin', 'babel')
const ESLINT = pathUtil.join(MODULES_PATH, '.bin', 'eslint')
const COFFEELINT = pathUtil.join(MODULES_PATH, '.bin', 'coffeelint')
// Define configuration
const config = {
TEST_PATH: 'test',
DOCCO_SRC_PATH: null,
DOCCO_OUT_PATH: 'docs',
BISCOTTO_SRC_PATH: null,
BISCOTTO_OUT_PATH: 'docs',
YUIDOC_SRC_PATH: null,
YUIDOC_OUT_PATH: 'docs',
YUIDOC_SYNTAX: 'js',
COFFEE_SRC_PATH: null,
COFFEE_OUT_PATH: 'out',
DOCPAD_SRC_PATH: null,
DOCPAD_OUT_PATH: 'out',
BABEL_SRC_PATH: null,
BABEL_OUT_PATH: 'es5',
ESLINT_SRC_PATH: null,
COFFEELINT_SRC_PATH: null
}
// Move package.json:nakeConfiguration into our configuration object
Object.keys(PACKAGE_CONFIG).forEach(function (key) {
const value = PACKAGE_CONFIG[key]
config[key] = value
})
// =====================================
// Generic
const childProcess = require('child_process')
const spawn = function (command, args, opts, next) {
const commandString = command + ' ' + args.join(' ')
if ( opts.output === true ) {
console.log(commandString)
opts.stdio = 'inherit'
}
const pid = childProcess.spawn(command, args, opts)
pid.on('close', function () {
let args = Array.prototype.slice.call(arguments)
if ( args[0] === 1 ) {
const error = new Error('Process [' + commandString + '] exited with error status code.')
next(error)
}
else {
next()
}
})
return pid
}
const exec = function (command, opts, next) {
if ( opts.output === true ) {
console.log(command)
return childProcess.exec(command, opts, function (error, stdout, stderr) {
console.log(stdout)
console.log(stderr)
if ( next ) next(error)
})
}
else {
return childProcess.exec(command, opts, next)
}
}
const steps = function (next, steps) {
let step = 0
const complete = function (error) {
// success status code
if ( error === 0 ) {
error = null
}
// error status code
else if ( error === 1 ) {
error = new Error('Process exited with error status code')
}
// Error
if ( error ) {
next(error)
}
else {
++step
if ( step === steps.length ) {
next()
}
else {
steps[step](complete)
}
}
}
return steps[step](complete)
}
// =====================================
// Actions
const actions = {
clean: function (opts, next) {
// Steps
steps(next, [
function (complete) {
console.log('\nclean:')
// Prepare rm args
let args = ['-Rf']
// Add compilation paths to args
Object.keys(config).forEach(function (key) {
let value = config[key]
if ( key.indexOf('OUT_PATH') !== -1 ) {
args.push(value)
}
})
// Add common ignore paths to args
; [APP_PATH, config.TEST_PATH].forEach(function (path) {
args.push(
pathUtil.join(path, 'build'),
pathUtil.join(path, 'components'),
pathUtil.join(path, 'bower_components'),
pathUtil.join(path, 'node_modules'),
pathUtil.join(path, '*out'),
pathUtil.join(path, '*log'),
pathUtil.join(path, '*heapsnaphot'),
pathUtil.join(path, '*cpuprofile')
)
})
// rm
spawn('rm', args, {output: true, cwd: APP_PATH}, complete)
}
])
},
setup: function (opts, next) {
// Steps
steps(next, [
function (complete) {
console.log('\nnpm install (for app):')
spawn(NPM, ['install'], {output: true, cwd: APP_PATH}, complete)
},
function (complete) {
if ( !config.TEST_PATH || !fsUtil.existsSync(config.TEST_PATH) ) return complete()
console.log('\nnpm install (for test):')
spawn(NPM, ['install'], {output: true, cwd: config.TEST_PATH}, complete)
},
function (complete) {
if ( !fsUtil.existsSync(DOCPAD_PATH) ) return complete()
console.log('\nnpm install (for docpad tests):')
spawn(NPM, ['install'], {output: true, cwd: DOCPAD_PATH}, complete)
}
])
},
compile: function (opts, next) {
// Steps
steps(next, [
function (complete) {
if ( !config.COFFEE_SRC_PATH || !fsUtil.existsSync(COFFEE) ) return complete()
console.log('\ncoffee compile:')
spawn(NODE, [COFFEE, '-co', config.COFFEE_OUT_PATH, config.COFFEE_SRC_PATH], {output: true, cwd: APP_PATH}, complete)
},
function (complete) {
if ( !config.BABEL_SRC_PATH || !fsUtil.existsSync(BABEL) ) return complete()
console.log('\nbabel compile:')
spawn(NODE, [BABEL, config.BABEL_SRC_PATH, '--out-dir', config.BABEL_OUT_PATH], {output: true, cwd: APP_PATH}, complete)
},
function (complete) {
if ( !config.DOCPAD_SRC_PATH || !fsUtil.existsSync(DOCPAD) ) return complete()
console.log('\ndocpad generate:')
spawn(NODE, [DOCPAD, 'generate'], {output: true, cwd: APP_PATH}, complete)
}
])
},
watch: function (opts, next) {
// Steps
steps(next, [
function (complete) {
if ( !config.BABEL_SRC_PATH || !fsUtil.existsSync(BABEL) ) return complete()
console.log('\nbabel compile:')
spawn(NODE, [BABEL, '-w', config.BABEL_SRC_PATH, '--out-dir', config.BABEL_OUT_PATH], {output: true, cwd: APP_PATH}, complete)
},
function (complete) {
if ( !config.COFFEE_SRC_PATH || !fsUtil.existsSync(COFFEE) ) return complete()
console.log('\ncoffee watch:')
spawn(NODE, [COFFEE, '-wco', config.COFFEE_OUT_PATH, config.COFFEE_SRC_PATH], {output: true, cwd: APP_PATH}) // background
complete() // continue while coffee runs in background
},
function (complete) {
if ( !config.DOCPAD_SRC_PATH || !fsUtil.existsSync(DOCPAD) ) return complete()
console.log('\ndocpad run:')
spawn(NODE, [DOCPAD, 'run'], {output: true, cwd: APP_PATH}) // background
complete() // continue while docpad runs in background
}
])
},
verify: function (opts, next) {
// Steps
steps(next, [
function (complete) {
console.log('\nnake compile')
actions.compile(opts, complete)
},
function (complete) {
console.log('\ncoffeelint:')
if ( !config.COFFEELINT_SRC_PATH || !fsUtil.existsSync(COFFEELINT) ) return complete()
spawn(COFFEELINT, [config.COFFEELINT_SRC_PATH], {output: true, cwd: APP_PATH}, complete)
},
function (complete) {
console.log('\neslint:')
if ( !config.ESLINT_SRC_PATH || !fsUtil.existsSync(ESLINT) ) return complete()
spawn(ESLINT, [config.ESLINT_SRC_PATH], {output: true, cwd: APP_PATH}, complete)
},
function (complete) {
console.log('\nnpm test:')
spawn(NPM, ['test'], {output: true, cwd: APP_PATH}, complete)
}
])
},
meta: function (opts, next) {
// Steps
steps(next, [
function (complete) {
if ( !config.DOCCO_SRC_PATH || !fsUtil.existsSync(DOCCO) ) return complete()
console.log('\ndocco compile:')
let command = [NODE, DOCCO,
'-o', config.DOCCO_OUT_PATH,
config.DOCCO_SRC_PATH
].join(' ')
exec(command, {output: true, cwd: APP_PATH}, complete)
},
function (complete) {
if ( !config.BISCOTTO_SRC_PATH || !fsUtil.existsSync(BISCOTTO) ) return complete()
console.log('\nbiscotto compile:')
let command = [BISCOTTO,
'-n', PACKAGE_DATA.title || PACKAGE_DATA.name,
'--title', '"' + (PACKAGE_DATA.title || PACKAGE_DATA.name) + ' API Documentation"',
'-r', 'README.md',
'-o', config.BISCOTTO_OUT_PATH,
config.BISCOTTO_SRC_PATH,
'-', 'LICENSE.md HISTORY.md'
].join(' ')
exec(command, {output: true, cwd: APP_PATH}, complete)
},
function (complete) {
if ( !fsUtil.existsSync(YUIDOC) ) return complete()
console.log('\nyuidoc compile:')
let command = [YUIDOC]
if ( config.YUIDOC_OUT_PATH ) command.push('-o', config.YUIDOC_OUT_PATH)
if ( config.YUIDOC_SYNTAX ) command.push('--syntaxtype', config.YUIDOC_SYNTAX, '-e', '.' + config.YUIDOC_SYNTAX)
if ( config.YUIDOC_SRC_PATH ) command.push(config.YUIDOC_SRC_PATH)
spawn(NODE, command, {output: true, cwd: APP_PATH}, complete)
},
function (complete) {
if ( !fsUtil.existsSync(PROJECTZ) ) return complete()
console.log('\nprojectz compile')
spawn(NODE, [PROJECTZ, 'compile'], {output: true, cwd: APP_PATH}, complete)
}
])
},
prerelease: function (opts, next) {
// Steps
steps(next, [
function (complete) {
console.log('\nnake verify')
actions.verify(opts, complete)
},
function (complete) {
console.log('\nnake meta')
actions.meta(opts, complete)
}
])
},
release: function (opts, next) {
// Steps
steps(next, [
function (complete) {
console.log('\nnake prerelease')
actions.prerelease(opts, complete)
},
function (complete) {
console.log('\nnpm publish:')
spawn(NPM, ['publish'], {output: true, cwd: APP_PATH}, complete)
// ^ npm will run prepublish and postpublish for us
},
function (complete) {
console.log('\nnake postrelease')
actions.postrelease(opts, complete)
}
])
},
postrelease: function (opts, next) {
// Steps
steps(next, [
function (complete) {
console.log('\ngit tag:')
spawn(GIT, ['tag', 'v' + PACKAGE_DATA.version, '-a'], {output: true, cwd: APP_PATH}, complete)
},
function (complete) {
console.log('\ngit push origin master:')
spawn(GIT, ['push', 'origin', 'master'], {output: true, cwd: APP_PATH}, complete)
},
function (complete) {
console.log('\ngit push tags:')
spawn(GIT, ['push', 'origin', '--tags'], {output: true, cwd: APP_PATH}, complete)
}
])
}
}
// =====================================
// Commands
const commands = {
clean: 'clean up instance',
setup: 'setup our project for development',
compile: 'compile our files',
watch: 'compile our files initially, and again for each change',
verify: 'verify our project works (includes compile)',
meta: 'compile our meta files',
prerelease: 'prepare our project for publishing (includes verify and meta)',
release: 'publish our project using npm (includes prerelease and postrelease)',
postrelease: 'prepare our project after publishing'
}
const aliases = {
install: 'setup',
test: 'verify',
docs: 'meta',
prepare: 'prerelease',
prepublish: 'prerelease',
publish: 'release',
postpublish: 'postpublish'
}
const combined = {}
Object.keys(commands).forEach(function (command) {
const description = commands[command]
const method = actions[command]
combined[command] = {
description: description,
method: method
}
})
Object.keys(aliases).forEach(function (alias) {
const command = aliases[alias]
const description = 'alias for ' + command
const method = actions[command]
combined[alias] = {
description: description,
method: method
}
})
// =====================================
// Command Line Interface
let desiredAction = null
let longestNameLength = 0
const finish = function (error) {
if ( error ) {
process.stderr.write( (error.stack || error.message || error) + '\n' )
throw error
}
else {
process.stdout.write('OK\n')
}
}
const output = function (list) {
let result = ''
Object.keys(list).forEach(function (key) {
const description = combined[key].description
let name = key + ': '
while ( name.length < longestNameLength + 10 ) name += ' '
result += name + ' ' + description + '\n'
})
return result
}
Object.keys(combined).forEach(function (command) {
if ( command.length > longestNameLength ) {
longestNameLength = command.length
}
if ( process.argv.indexOf(command) !== -1 ) {
desiredAction = command
}
})
// Fire the method
if ( desiredAction ) {
combined[desiredAction].method({}, finish)
}
// Display the help
else {
console.log([
'Nakefile help for ' + PACKAGE_DATA.name,
'',
'USAGE',
'',
'Standard usage:',
'node --harmony nakefile.js $action',
'',
'NPM script usage:',
'npm run-script $action',
'',
'Shell alias usage:',
'nake $action',
'',
'ACTIONS',
'',
output(commands),
output(aliases).trim()
].join('\n'))
}