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 | 1
1
1
1
1
1
1
1
2
2
2
2
2
2
2
1
4
4
4
4
4
4
4
4
1
2
2
2
2
2
2
1
4
4
4
4
4
4
4
8
4
1
1
1
2
| var Promise = require('es6-promise').Promise;
var log = require('./utils/log');
var helper = require('./utils/config-helper');
var extend = require('util')._extend;
var path = require('path');
var config, build = {};
build.htmlMin = function htmlMin(source, target, options) {
var htmlWrapper = helper.matches(config.tasks.build, ['html-min']);
if (!htmlWrapper) return Promise.resolve();
log.info(' * HTML Min');
var Fn = require('./wrappers/html-min');
return (new Fn(source, target, options)).write().catch(log.onError);
};
build.html = function html(source, target, options) {
var wrapper = helper.matches(config.tasks.build, ['jade','mustache']);
Iif (!wrapper) return Promise.resolve();
log.info(' * HTML: ' + source);
options.now = Date().split(' ').splice(0,5).join(' ');
options.pkg = extend(config.pkg || {}, options.pkg || {}); //allow nodeAPI to be ruler of config
var Fn = require('./wrappers/' + wrapper);
return (new Fn(source, target, options)).write().catch(log.onError);
};
build.scripts = function scripts(source, target, options){
var wrapper = helper.matches(config.tasks.build, ['browserify','requirejs']);
Iif (!wrapper) return Promise.resolve();
log.info(' * Scripts: ' + source);
options.browserify = config.pkg.browserify;
options.browser = config.pkg.browser;
options["browserify-shim"] = config.pkg["browserify-shim"];
var Fn = require('./wrappers/' + wrapper);
return (new Fn(source, target, options)).write().catch(log.onError);
};
build.styles = function styles(source, target, options){
var wrapper = helper.matches(config.tasks.build, ['sass']);
Iif (!wrapper) return Promise.resolve();
log.info(' * Styles: ' + source);
options.appRoot = config.appRoot;
var Fn = require('./wrappers/' + wrapper);
return (new Fn(source, target, options)).write().catch(log.onError);
};
//pipe all task execution through here to unify task prep and config normalisation
function exec(subtask, source, target, options){
config = helper.getConfig();
//get out if config does not exist && node API did not pass a source/target
//if (!config.tasks.build && task == 'all') return Promise.resolve();
Iif (subtask == 'all' && source) log.onError('Please refrain from using `.all`. from the NodeJS script');
Iif (!config.tasks.build && !source) return Promise.resolve();
log.info('Building :' );
subtask = (subtask === 'all') ? ['html', 'styles', 'scripts'] : subtask;
//normalise the args into an array of tasks
var tasks = helper.normaliseBuild(subtask, config, source, target, options || { });
var promises = tasks.map(function(params){
return build[params.subTask](params.source, params.target, params.options).then(params.options.reload).catch(log.warn);
});
return Promise.all(promises).catch(log.onError);
}
module.exports = {
html: function(source, target, options){ return exec('html', source, target, options); },
styles: function(source, target, options){ return exec('styles', source, target, options); },
scripts: function(source, target, options){return exec('scripts', source, target, options); },
all: function(source, target, options){ return exec('all', source, target, options); }
}; |