-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
404 lines (346 loc) · 15.1 KB
/
index.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
var fs = require('fs');
var path = require('path');
var events = require('events');
var _ = require('lodash');
var through = require('through2');
var series = require('stream-series');
var del = require('del');
var gulp = require('gulp');
var gulpWatch = require('gulp-watch');
var gulpWrap = require('gulp-wrap');
var gulpRename = require('gulp-rename');
var livereload = require('gulp-livereload');
var msg = require('gulp-msg');
var amdOptimize = require('amd-optimize');
var configs = require('projects-config');
var karma = require('karma');
var WebServer = require('webserver-lite');
var args = require('get-gulp-args')();
var compileProject = require('./lib/compile-project');
var compileIndex = require('./lib/compile-index');
var utils = require('./lib/utils');
process.env.NODE_ENV = args[0] || 'dev';
process.env.PROJECT = args[1] || '*';
msg.Success('--', 'Enviroment: <%= env.NODE_ENV %>. Project: <%= env.PROJECT %>', '--');
var rootPath = process.cwd();
var defaults = {
projectsPath: path.join(rootPath, 'projects'),
localConfigsPath: path.join(rootPath, 'configs'),
karmaConfigPath: path.join(rootPath, 'karma.conf.js'),
configsDir: '*/_config',
compiledDir: 'compiled',
buildDir: 'build',
filesDir: 'files',
vendorDir: 'vendor',
testVendorDir: 'node_modules',
moduleIndexFile: 'index.js',
maxListeners: 100,
browsers: ['chrome >= 35', 'ff >= 20', 'safari >= 7', 'ie >= 10', 'opera >= 12.10', 'android >= 4.4', 'ios >= 7', 'phantomjs >= 1.9']
};
function createBuilder(options) {
var opts = _.defaults(options || {}, defaults);
opts.configsPath = path.join(opts.projectsPath, opts.configsDir);
opts.compiledPath = path.join(opts.projectsPath, opts.compiledDir);
opts.buildPath = path.join(opts.projectsPath, opts.buildDir);
opts.projectsDir = path.basename(opts.projectsPath);
//TODO refactor it
if (rootPath === opts.projectsPath) {
opts.projectsDir = '';
}
events.EventEmitter.prototype._maxListeners = opts.maxListeners;
configs.load(opts.configsPath, opts.localConfigsPath, {defaults: setProjectConfig});
function setProjectConfig(env, projectName) {
var publicConfig = this.public || {};
publicConfig.project = projectName;
var defaults = {
project: projectName,
localhost: {
project: projectName,
manifest: {
env: 'local',
name: projectName,
config: publicConfig,
browsers: opts.browsers
}
},
build: {
project: projectName,
filesDir: opts.filesDir,
manifest: {
env: env,
name: projectName,
config: publicConfig,
browsers: opts.browsers
},
copy: {
'favicon.ico': 'favicon.ico'
}
}
};
defaults.build.copy[opts.filesDir + '/**'] = opts.filesDir + '/';
return defaults;
}
function config() {
return configs.stream({name: 'manifests.js', section: 'localhost.manifest'})
.pipe(gulpWrap('window.manifests = <%= contents %>'))
.pipe(gulp.dest(opts.compiledPath));
}
function webserver() {
var defaultSettings = {
fallback: 'index.html',
root: opts.projectsPath
};
var livereloadPort = 2300;
function startWebServer(settings, name) {
if (settings.root) {
settings.root = path.resolve(opts.projectsPath, settings.root);
}
if (settings.livereload === true) {
settings.livereload = livereloadPort++;
}
var webServer = new WebServer(_.defaults(settings, {segment: name}, defaultSettings));
return webServer.start();
}
return configs.forEach('localhost.webserver', startWebServer)
.pipe(msg.flush.info('', 'Webservers started!', '-'));
}
function compileStyles(params) {
params = params || {};
var projectsSrcCache = {};
function compileProjectStyle(projectName, config, moduleName) {
var projectSrcStream;
if (projectsSrcCache[projectName] && projectsSrcCache[projectName].complete) {
projectSrcStream = through.obj();
projectsSrcCache[projectName].forEach(function(file) {
projectSrcStream.push(file);
});
projectSrcStream.end();
} else {
projectsSrcCache[projectName] = [];
projectSrcStream = amdOptimize.src(projectName + '/bootstrap', {
baseUrl: opts.projectsPath,
configFile: path.join(opts.projectsPath, projectName, 'requireconfig.js')
})
.pipe(through.obj(function(file, enc, callback) {
projectsSrcCache[projectName].push(file);
callback(null, file);
}, function(callback) {
projectsSrcCache[projectName].complete = true;
callback();
}));
}
return projectSrcStream.pipe(msg.flush.info('', projectName + ' styles compiled', '-'))
.pipe(compileProject(opts.projectsPath, _.merge({
styleOnly: true,
rev: false,
includeCss: false,
minifyCss: false,
combine: 'style',
styles: config.build.styles,
less: config.build.less,
scss: config.build.scss,
sass: config.build.sass,
stylus: config.build.stylus,
cache: moduleName
}, opts)))
.pipe(msg.flush.info('', projectName + ' styles compiled', '-'))
.pipe(gulp.dest(path.join(opts.compiledPath, projectName)));
}
var totalStream = configs.forEach(function(config, projectName) {
if (params.watch) {
return gulpWatch(path.join(opts.projectsPath, projectName, '**', '!(*.html|*.js)'), function(file) {
var projectInfo = utils.projectInfoFromPath(file.path, opts.projectsPath, opts);
return compileProjectStyle(projectName, config, projectInfo.moduleName)
.pipe(livereload())
.pipe(msg.flush.note('Listening...'));
});
} else {
return compileProjectStyle(projectName, config);
}
});
return totalStream.pipe(msg.flush.info('', 'Styles compiled!', '-'));
}
function copy() {
var streams = [], generalCopiesMap = {};
configs.forEach(function(config, projectName) { //clean build folder
del.sync(path.join(opts.buildPath, projectName, '**/!(index.html)')); //except index because it conflicts with livereload
});
configs.forEach(function(config, projectName) {
if (config.build.index) { //copying for local projects
streams = streams.concat(copy(config.build.copy, opts.projectsPath, path.join(opts.buildPath, projectName)));
} else {
_.extend(generalCopiesMap, config.build.copy);
}
});
streams = streams.concat(copy(generalCopiesMap, opts.projectsPath, opts.buildPath)); //copying for other projects
function copy(copiesMap, projectsPath, buildPath) {
return _.map(copiesMap, function(dstPath, srcPath) {
if (/\/$/.test(dstPath)) {
//copy directory
return gulp.src(path.join(projectsPath, srcPath))
.pipe(gulp.dest(path.join(buildPath, dstPath)));
} else {
//copy file
return gulp.src(path.join(projectsPath, srcPath))
.pipe(gulpRename(dstPath))
.pipe(gulp.dest(buildPath));
};
});
}
return series.apply(series, streams);
}
function compileVendor() {
var stream = through.obj();
config()
.on('end', function() {
var totalStream = configs.forEach(function(config, projectName) {
if (!config.vendor) {
return;
}
return compileProject.src(opts.projectsPath, _.merge({
modules: config.vendor
}, opts))
.pipe(gulp.dest(path.join(opts.compiledPath, projectName, opts.vendorDir)))
.pipe(msg.flush.info('', 'Vendor for <%= project %> is created', '-', config));
});
if (totalStream) {
return totalStream
.pipe(msg.flush.info('', 'Vendors created!', '-'))
.pipe(through.obj(function(file, enc, cb) {cb(null, file);}, function(callback) {
callback();
stream.end();
}));
} else {
return stream.end();
}
});
return stream;
}
function build() {
var stream = through.obj();
series(copy(), compileVendor())
.on('end', function() {
return configs.forEach(function(config, projectName) {
return amdOptimize.src(projectName + '/bootstrap', {
baseUrl: opts.projectsPath,
configFile: path.join(opts.projectsPath, projectName, 'requireconfig.js')
})
.pipe(compileProject(opts.projectsPath, _.merge(config.build, opts)))
.pipe(msg.flush.info('', 'Project <%= project %> is compiled', '-', config))
})
.pipe(compileIndex(opts.projectsPath, {configWrap: 'window.manifests = <%= contents %>'}))
.pipe(msg.flush.info('', 'Build for <%= env.NODE_ENV %> completed!', '-'))
.pipe(through.obj(function(file, enc, cb) {cb(null, file);}, function(callback) {
callback();
stream.end();
}))
.pipe(gulp.dest(opts.buildPath));
});
return stream;
}
function test(done) {
var projectsDir = opts.projectsDir ? opts.projectsDir + '/' : '';
var karmaConfigPath = opts.karmaConfigPath;
var karmaConfig = {
frameworks: ['jasmine-jquery', 'jasmine', 'requirejs'],
singleRun: true,
plugins: [
'karma-requirejs',
'karma-jasmine',
'karma-phantomjs-launcher',
'karma-jasmine-jquery',
'karma-ng-html2js-preprocessor',
'karma-requirejs-preprocessor'
],
requirejsPreprocessor: {
config: {
baseUrl: '/base/' + projectsDir,
paths: {
angular: opts.testVendorDir + '/angular/angular', //for no-angular projects
angularMocks: opts.testVendorDir + '/angular-mocks/angular-mocks'
},
shim: {
angularMocks: ['angular']
},
deps: [
'angularMocks'
]
}
},
exclude: [
opts.testVendorDir + '/**/*spec.js'
]
};
if (fs.existsSync(karmaConfigPath)) {
karmaConfig.set = function(config) {
_.merge(this, config, function(a, b) {
if (_.isArray(a)) {
return _.union(a, b);
}
});
};
require(karmaConfigPath)(karmaConfig);
delete karmaConfig.set;
}
var startKarmaServers = configs.reduceRight(function(next, config, projectName) {
return function(err, result) {
msg.Info('-', 'Start Karma-server for <%= project %>', '-', config);
createKarmaServer(projectName, next).start();
};
}, done);
startKarmaServers();
function createKarmaServer(projectName, done) {
var projectsDir = opts.projectsDir ? opts.projectsDir + '/' : '';
if (!karmaConfig.browsers) {
karmaConfig.browsers = ['PhantomJS'];
}
karmaConfig.files = [
{pattern: opts.testVendorDir + '/**/*.js', included: false, watched: false},
{pattern: opts.compiledDir + '/' + projectName + '/**/*.js', included: false, watched: false},
{pattern: projectsDir + projectName + '/**/!(requireconfig).js', included: false},
{pattern: projectsDir + projectName + '/**/*.html', included: false},
projectsDir + opts.compiledDir +'/manifests.js',
projectsDir + 'lib.js',
// needs to be last http://karma-runner.github.io/0.12/plus/requirejs.html
projectsDir + projectName + '/requireconfig.js'
];
karmaConfig.preprocessors = {};
karmaConfig.preprocessors[projectsDir + projectName + '/**/*.html'] = 'ng-html2js';
karmaConfig.preprocessors[projectsDir + projectName + '/requireconfig.js'] = 'requirejs';
return new karma.Server(karmaConfig, done);
}
}
function watch() {
livereload.listen();
return compileStyles({watch: true});
}
function compile() {
var stream = through.obj();
series(compileVendor())
.on('end', function() {
return compileStyles()
.pipe(through.obj(function(file, enc, cb) {cb(null, file);}, function(callback) {
callback();
stream.end();
}));
});
return stream;
//return series(compileVendor(), compileStyles()); //TODO compileVendor must to return correct stream
}
function run() {
return series(webserver(), compile().pipe(msg.flush.note('Listening...')), watch());
}
return {
config: config,
webserver: webserver,
watch: watch,
compileStyles: compileStyles,
copy: copy,
compileVendor: compileVendor, //[config]
compile: compile, //[config, compileVendor, compileStyles] order is important
build: build, //[copy, compileVendor]
test: test,
run: run //[compile, webserver, watch]
}
}
module.exports = createBuilder;