-
Notifications
You must be signed in to change notification settings - Fork 9
/
gulpfile.js
188 lines (171 loc) · 5.84 KB
/
gulpfile.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
'use strict';
var
gulp = require('gulp'),
fs = require('fs'),
Promise = require('bluebird'),
jshint = require('gulp-jshint'),
nom = require('nomnom'),
rimraf = Promise.promisify(require('rimraf')),
stylish = require('jshint-stylish');
var args = nom
.script('gulp')
.option('task', {position:0, 'default':'build', help:'Optional specific gulp task to execute. Defaults to "build".'})
.option('production', {abbr:'P', flag:true, 'default':false, help:'Build in production mode.'})
.option('source-maps', {flag:true, 'default':true, help:'Whether or not to build source-maps.'})
.option('cache', {flag:true, 'default':true, help:'Enables the use of a cache-file.'})
.option('clean', {flag:true, 'default':false, help:'This will empty the outdir before writing any new files to it.'})
.option('samples', {flag:false, abbr: 's', help:'Comma-separated list of samples to build overriding the standard (e.g. enyo,moonstone,spotlight).'})
.option('log-level', {abbr:'l', 'default':'error', help:'Log level; available options are [fatal, error, warn, info, debug, trace].'})
.option('log-json', {flag:true, 'default':false, help:'Enable this flag to ensure the output of the logging is the normal bunayn ' +
'"JSON" format to STDOUT that can be piped to their separate bunyan cli tool for filtering.'})
.option('user', {flag:true, 'default':true, help:'Set this to false when executing from an automated script or in ' +
'an environment where a user-environment should not be used.'})
.option('init-libs', {abbr: 'i', flag: true, default: true, hidden:true})
.option('link-all-libs', {abbr: 'L', flag: true, default: false, hidden:true})
.option('link-available', {abbr: 'D', flag: true, default: false, hidden:true})
.parse();
gulp.task('default', ['build']);
gulp.task('init', init);
gulp.task('build', build);
gulp.task('build-all', buildAll);
gulp.task('moonstone-extra', moonstone);
gulp.task('garnet', garnet);
gulp.task('sunstone', sunstone);
gulp.task('clean', clean);
gulp.task('jshint', lint);
function init() {
var enyo = require('enyo-dev');
var initializer = enyo.initialize({
initLibs: args['init-libs'],
linkAllLibs: args['link-all-libs'],
linkAvailLibs: args['link-available']
});
var promiseOn = Promise.promisify(initializer.on, {context:initializer});
return promiseOn('end');
}
function build() {
return buildStrawman([
'enyo', 'moonstone', 'layout', 'spotlight', 'enyo-ilib', 'onyx', 'canvas', 'svg', 'enyo-webos'
]);
}
function buildAll() {
var samples = fs.readdirSync('./src');
for(var i=0; i<samples.length; i++) {
if(samples[i].indexOf('-samples') > -1) {
samples[i] = samples[i].replace('-samples', '');
} else {
samples.splice(i, 1);
i--;
}
}
return buildStrawman(samples);
}
function moonstone() {
return buildStrawman([
'enyo', 'moonstone-extra', 'layout', 'spotlight', 'enyo-ilib', 'onyx', 'canvas', 'svg', 'enyo-webos'
]);
}
function garnet() {
return buildStrawman([
'enyo', 'garnet', 'layout', 'enyo-ilib', 'canvas', 'svg', 'enyo-webos'
]);
}
function sunstone() {
return buildStrawman([
'enyo', 'sunstone', 'layout', 'enyo-ilib', 'canvas', 'svg', 'enyo-webos'
]);
}
function writeConfig(samples) {
var output = '';
var exportsObject = {};
for(var i=0; i<samples.length; i++) {
if(samples[i] === 'enyo-ilib') {
exportsObject[samples[i]] = 'iLib';
} else if(samples[i] === 'enyo-webos') {
exportsObject[samples[i]] = 'webOS';
} else {
var name = samples[i].replace('enyo-', '').replace('-extra', '').replace('-', ' ');
name = name.replace(/\w\S*/g, function(str){return str.slice(0, 1).toUpperCase() + str.slice(1);});
exportsObject[samples[i].replace('-extra', '')] = name;
}
}
output += 'window.strawmanConfig = ' + JSON.stringify(exportsObject, null, '\t') + ';\n';
fs.writeFileSync('./config.js', output, {encoding:'utf8'});
}
function buildStrawman(samples) {
samples = (args.samples) ? args.samples.split(',') : samples;
var mI = samples.indexOf('moonstone');
var meI = samples.indexOf('moonstone-extra');
if(meI > -1) {
if(mI > -1) {
samples.splice(mI, 1);
meI = samples.indexOf('moonstone-extra');
}
samples.splice(meI+1, 0, 'moonstone-extra-light');
} else {
if(mI > -1) {
samples.splice(mI+1, 0, 'moonstone-light');
}
}
writeConfig(samples);
if(args.clean) {
return clean().then(function() {
return promiseStrawman(samples);
});
} else {
return promiseStrawman(samples);
}
}
function clean() {
return rimraf('./dist', {disableGlob:true});
}
function lint () {
return gulp
.src(['./src/**/*.js'])
.pipe(jshint())
.pipe(jshint.reporter(stylish, {verbose: true}))
.pipe(jshint.reporter('fail'));
}
function promiseStrawman(samples) {
var enyo = require('enyo-dev');
var opts = samplerOpts().options;
opts.subpackage = [];
for(var i=0; i<samples.length; i++) {
opts.subpackage.push(samplerOpts(samples[i]));
}
console.log('Building Enyo-Strawman...');
var packager = enyo.packager(opts);
packager.on('subpackage', function(e) {
var name = e.options.outDir.replace('../../dist/', '');
if(e.name.indexOf('-extra')>-1) {
name = name.replace('moonstone', 'moonstone-extra');
}
console.log('Building ' + name + ' samples...');
});
var promiseOn = Promise.promisify(packager.on, {context:packager});
return promiseOn('end');
}
function samplerOpts(item) {
var target = '.';
var opts = {
'package': '.',
sourceMaps: args['source-maps'],
clean: false,
cache: args.cache,
production: args.production,
title: 'Sampler',
logLevel: args['log-level'],
logJson: args['log-json'],
user: args.user
};
if(item) {
target = './src/' + item.replace('-light', '') + '-samples';
opts.outDir = '../../dist/' + item.replace('-extra', '');
if(item.indexOf('moonstone')>-1 && item.indexOf('-light')>-1) {
opts.lessVars = [{name: '@moon-theme', value: 'light'}];
}
} else {
opts.headScripts = ['./config.js'];
}
return {name:target, options:opts};
}