gulp4 ftw

This commit is contained in:
Tim Lancina
2015-07-21 22:58:12 -05:00
parent cc9ff757b0
commit 8aa0614cde
2 changed files with 206 additions and 216 deletions

View File

@ -53,91 +53,96 @@ var tscReporter = {
} }
}; };
gulp.task('clean.build', function() { // Framework
runSequence( gulp.task('transpile', transpileTask);
gulp.task('bundle.js', bundleJsTask);
gulp.task('sass', sassTask);
gulp.task('fonts', fontsTask);
// Karma testing
gulp.task('tests', testsTask);
gulp.task('karma', karmaTask);
gulp.task('karma-watch', karmaWatchTask);
// e2e testing
gulp.task('e2e.copy', e2eCopyTask);
gulp.task('e2e.template', e2eHTMLTemplateTask);
gulp.task('e2e.transpile', e2eTranspileTask);
gulp.task('e2e.tests', e2eTestsTask);
gulp.task('e2e', gulp.parallel('e2e.copy', 'e2e.template', 'e2e.transpile', 'e2e.tests'));
// Utility
gulp.task('clean', cleanTask);
gulp.task('vendor', vendorTask);
gulp.task('copy-scripts', copyScriptsTask);
gulp.task('serve', serveTask);
gulp.task('watch-task', watchTask);
gulp.task('watch', gulp.series('serve', 'watch-task'));
gulp.task('examples', gulp.parallel('e2e.copy', 'e2e.template', 'e2e.transpile'));
// Build
gulp.task('build', gulp.parallel(
gulp.series('transpile', 'bundle.js', 'copy-scripts'),
'sass',
'fonts',
'vendor'
))
gulp.task('clean.build', gulp.series(
'clean', 'clean',
'transpile', 'build'
//'bundle.deps', ));
'bundle.js',
'examples',
'sass',
'fonts',
'vendor',
'copy-scripts');
})
gulp.task('build', function() { gulp.task('build.watch', gulp.series(
runSequence( gulp.parallel('build', 'examples'),
'transpile',
'bundle.js',
'examples',
'sass',
'fonts',
'vendor',
'copy-scripts');
})
gulp.task('watch', function() {
runSequence(
'transpile',
'bundle.js',
'examples',
'sass',
'fonts',
'vendor',
'copy-scripts',
'serve', 'serve',
'watch-task'
))
function() { /*****************************************************************************/
watch(
[ function watchTask(done) {
watch([
'ionic/**/*.js', 'ionic/**/*.js',
'ionic/**/*.ts', 'ionic/**/*.ts',
'!ionic/components/*/test/**/*', '!ionic/components/*/test/**/*',
'!ionic/util/test/*' '!ionic/util/test/*'
], ],
function() { gulp.series('transpile', 'bundle.js')
runSequence(
'transpile',
'bundle.js',
'examples'
)
}
); );
watch('ionic/components/*/test/**/*.ts', e2eTranspileTask);
watch('scripts/e2e/e2e.template.html', e2eHTMLTemplateTask);
watch([
'ionic/components/*/test/**/*',
'!ionic/components/*/test/**/*.ts'
],
e2eCopyTask
);
watch('ionic/**/*.scss', sassTask);
done();
}
watch('ionic/components/*/test/**/*', function() { function serveTask(done) {
gulp.start('examples');
});
watch('ionic/**/*.scss', function() {
gulp.start('sass');
});
})
});
gulp.task('serve', function() {
connect.server({ connect.server({
root: 'dist', root: 'dist',
port: 8000, port: 8000,
livereload: false livereload: false
}); });
}); done();
}
gulp.task('clean', function(done) { function cleanTask(done) {
del(['dist/'], done); del(['dist/'], done);
}); }
gulp.task('transpile', function() { function transpileTask() {
var stream = gulp.src( var stream = gulp.src([
[
'ionic/**/*.ts', 'ionic/**/*.ts',
'ionic/**/*.js', 'ionic/**/*.js',
'!ionic/components/*/test/**/*', '!ionic/components/*/test/**/*',
'!ionic/init.js', '!ionic/init.js',
'!ionic/util/test/*' '!ionic/util/test/*'
] ])
)
.pipe(cache('transpile', { optimizeMemory: true })) .pipe(cache('transpile', { optimizeMemory: true }))
.pipe(tsc(tscOptions, null, tscReporter)) .pipe(tsc(tscOptions, null, tscReporter))
.on('error', function(error) { .on('error', function(error) {
@ -152,15 +157,15 @@ gulp.task('transpile', function() {
.pipe(gulp.dest('dist/js/es5/ionic')) .pipe(gulp.dest('dist/js/es5/ionic'))
return stream; return stream;
}); }
gulp.task('bundle.js', function() { function bundleJsTask() {
return gulp.src(['dist/js/es5/ionic/**/*.js', 'ionic/init.js']) return gulp.src(['dist/js/es5/ionic/**/*.js', 'ionic/init.js'])
.pipe(concat('ionic.bundle.js')) .pipe(concat('ionic.bundle.js'))
.pipe(gulp.dest('dist/js/')); .pipe(gulp.dest('dist/js/'));
}); }
gulp.task('tests', function() { function testsTask() {
return gulp.src('ionic/components/*/test/*/**/*.spec.ts') return gulp.src('ionic/components/*/test/*/**/*.spec.ts')
.pipe(tsc(tscOptions, null, tscReporter)) .pipe(tsc(tscOptions, null, tscReporter))
.pipe(babel(getBabelOptions('tests'))) .pipe(babel(getBabelOptions('tests')))
@ -168,128 +173,104 @@ gulp.task('tests', function() {
file.dirname = file.dirname.replace(path.sep + 'test' + path.sep, path.sep) file.dirname = file.dirname.replace(path.sep + 'test' + path.sep, path.sep)
})) }))
.pipe(gulp.dest('dist/tests')) .pipe(gulp.dest('dist/tests'))
}) }
gulp.task('examples', function() { function copyScriptsTask() {
var buildTest = lazypipe() return gulp.src([
.pipe(tsc, tscOptions, null, tscReporter) 'scripts/resources/*.js',
.pipe(babel, getBabelOptions('examples')) 'config.js',
'dist/js/ionic.bundle.js',
'dist/vendor/web-animations-js/web-animations.min.js'
])
.pipe(gulp.dest('dist/lib'));
}
// Get each test folder with gulp.src
return gulp.src(['ionic/components/*/test/*/**/*', '!ionic/components/*/test/*/**/*.spec.ts']) function e2eCopyTask() {
.pipe(cache('examples', { optimizeMemory: true })) return gulp.src([
.pipe(gulpif(/.ts$/, buildTest())) 'ionic/components/*/test/*/**/*',
'!ionic/components/*/test/*/**/*.ts'
])
.pipe(rename(function(file) {
file.dirname = file.dirname.replace(path.sep + 'test' + path.sep, path.sep)
}))
.pipe(gulp.dest('dist/e2e'))
}
function e2eTranspileTask() {
return gulp.src([
'ionic/components/*/test/*/**/*.ts',
'!ionic/components/*/test/*/**/*.spec.ts',
'!ionic/components/*/test/*/**/e2e.ts'
])
.pipe(cache('e2e.transpile', { optimizeMemory: true }))
.pipe(tsc(tscOptions, null, tscReporter))
.pipe(babel(getBabelOptions('e2e')))
.on('error', function (err) { .on('error', function (err) {
console.log("ERROR: " + err.message); console.log("ERROR: " + err.message);
this.emit('end'); this.emit('end');
}) })
.pipe(gulpif(/index.js$/, createIndexHTML())) //TSC changes .ts to .js
.pipe(rename(function(file) { .pipe(rename(function(file) {
file.dirname = file.dirname.replace(path.sep + 'test' + path.sep, path.sep) file.dirname = file.dirname.replace(path.sep + 'test' + path.sep, path.sep)
})) }))
.pipe(gulp.dest('dist/examples/')) .pipe(gulp.dest('dist/e2e/'))
function createIndexHTML() {
var template = _.template(
fs.readFileSync('scripts/e2e/example.template.html')
)({
buildConfig: buildConfig
});
return through2.obj(function(file, enc, next) {
var self = this;
var module = path.dirname(file.path)
.replace(__dirname, '')
.replace('/ionic/components/', 'examples/')
.replace('/test/', '/') +
'/index';
var indexContents = template.replace('{{MODULE}}', module);
self.push(new VinylFile({
base: file.base,
contents: new Buffer(indexContents),
path: path.join(path.dirname(file.path), 'index.html'),
}));
next(null, file);
});
} }
});
gulp.task('copy-scripts', function(){ function e2eHTMLTemplateTask() {
gulp.src(['scripts/resources/*.js', 'config.js', 'dist/js/ionic.bundle.js', var HTMLTemplate = _.template(
'dist/vendor/web-animations-js/web-animations.min.js'])
.pipe(gulp.dest('dist/lib'));
})
gulp.task('e2e', ['copy-scripts'], function() {
var buildTest = lazypipe()
//.pipe(traceur, traceurOptions)
.pipe(tsc, tscOptions, null, tscReporter)
.pipe(babel, getBabelOptions('e2e'))
var buildE2ETest = lazypipe()
//.pipe(traceur, traceurOptions)
.pipe(tsc, tscOptions, null, tscReporter)
.pipe(babel)
var indexTemplate = _.template(
fs.readFileSync('scripts/e2e/e2e.template.html') fs.readFileSync('scripts/e2e/e2e.template.html')
)({ )({
buildConfig: buildConfig buildConfig: buildConfig
});
}) // we won't actually do anything with index.ts, we just know that this is the
var testTemplate = _.template( fs.readFileSync('scripts/e2e/e2e.template.js') ) // location index.html should be
return gulp.src('ionic/components/*/test/*/**/index.ts')
var platforms = [ .pipe(cache('e2e.html.template', { optimizeMemory: true }))
'android', .pipe(through2.obj(function(file, enc, next) {
'core',
'ios',
];
// Get each test folder with gulp.src
return gulp.src(['ionic/components/*/test/*/**/*', '!ionic/components/*/test/*/**/*.spec.ts'])
.pipe(cache('e2e', { optimizeMemory: true }))
.pipe(gulpif(/e2e.ts$/, buildE2ETest()))
.pipe(gulpif(/.ts$/, buildTest()))
.on('error', function (err) {
console.log("ERROR: " + err.message);
this.emit('end');
})
.pipe(gulpif(/index.js$/, createIndexHTML())) //TSC changes .ts to .js
.pipe(rename(function(file) {
file.dirname = file.dirname.replace(path.sep + 'test' + path.sep, path.sep)
}))
.pipe(gulpif(/e2e.js$/, createPlatformTests()))
.pipe(gulp.dest('dist/e2e/'))
function createIndexHTML() {
return through2.obj(function(file, enc, next) {
var self = this;
var module = path.dirname(file.path) var module = path.dirname(file.path)
.replace(__dirname, '') .replace(__dirname, '')
.replace('/ionic/components/', 'e2e/') .replace('/ionic/components/', 'e2e/')
.replace('/test/', '/') + .replace('/test/', '/') +
'/index'; '/index';
var indexContents = indexTemplate.replace('{{MODULE}}', module); var templateContents = HTMLTemplate.replace('{{MODULE}}', module);
var indexFile = new VinylFile({
self.push(new VinylFile({
base: file.base, base: file.base,
contents: new Buffer(indexContents), contents: new Buffer(templateContents),
path: path.join(path.dirname(file.path), 'index.html'), path: path.join(path.dirname(file.path), 'index.html'),
})); })
next(null, file); next(null, indexFile);
}); }))
.pipe(rename(function(file) {
file.dirname = file.dirname.replace(path.sep + 'test' + path.sep, path.sep)
}))
.pipe(gulp.dest('dist/e2e/'))
} }
function createPlatformTests(file) { function e2eTestsTask() {
return through2.obj(function(file, enc, next) { var testTemplate = _.template(fs.readFileSync('scripts/e2e/e2e.template.js'));
var self = this var platforms = [
var relativePath = path.dirname(file.path.replace(/^.*?ionic(\/|\\)components(\/|\\)/, '')) 'android',
var contents = file.contents.toString() 'core',
'ios',
];
return gulp.src('ionic/components/*/test/*/**/e2e.ts')
.pipe(cache('e2e.tests', { optimizeMemory: true }))
.pipe(tsc(tscOptions, null, tscReporter))
.pipe(babel())
.on('error', function (err) {
console.log("ERROR: " + err.message);
this.emit('end');
})
.pipe(rename(function(file) {
file.dirname = file.dirname.replace(path.sep + 'test' + path.sep, path.sep)
}))
.pipe(through2.obj(function(file, enc, next) {
var self = this;
var relativePath = path.dirname(file.path.replace(/^.*?ionic(\/|\\)components(\/|\\)/, ''));
var contents = file.contents.toString();
platforms.forEach(function(platform) { platforms.forEach(function(platform) {
var platformContents = testTemplate({ var platformContents = testTemplate({
contents: contents, contents: contents,
@ -304,11 +285,11 @@ gulp.task('e2e', ['copy-scripts'], function() {
})) }))
}) })
next() next()
}) }))
.pipe(gulp.dest('dist/e2e/'))
} }
});
gulp.task('sass', function() { function sassTask() {
return gulp.src('ionic/ionic.scss') return gulp.src('ionic/ionic.scss')
.pipe(sass({ .pipe(sass({
onError: function(err) { onError: function(err) {
@ -317,25 +298,25 @@ gulp.task('sass', function() {
})) }))
.pipe(autoprefixer(buildConfig.autoprefixer)) .pipe(autoprefixer(buildConfig.autoprefixer))
.pipe(gulp.dest('dist/css/')); .pipe(gulp.dest('dist/css/'));
}); }
gulp.task('fonts', function() { function fontsTask() {
return gulp.src('ionic/components/icon/fonts/**/*') return gulp.src('ionic/components/icon/fonts/**/*')
.pipe(gulp.dest('dist/fonts')); .pipe(gulp.dest('dist/fonts'));
}); }
gulp.task('vendor', function() { function vendorTask() {
return gulp.src(['scripts/vendor/**/*']) return gulp.src(['scripts/vendor/**/*'])
.pipe(gulp.dest('dist/vendor')); .pipe(gulp.dest('dist/vendor'));
}); }
require('./scripts/snapshot/snapshot.task')(gulp, argv, buildConfig); function karmaTask() {
gulp.task('karma', function() {
return karma.start({ configFile: __dirname + '/scripts/test/karma.conf.js' }) return karma.start({ configFile: __dirname + '/scripts/test/karma.conf.js' })
//return karma.start({ configFile: __dirname + '/karma.conf.js' }) }
});
gulp.task('karma-watch', function() { function karmaWatchTask() {
return karma.start({ configFile: __dirname + '/scripts/test/karma-watch.conf.js' }) return karma.start({ configFile: __dirname + '/scripts/test/karma-watch.conf.js' })
}); }
// snapshot tasks
require('./scripts/snapshot/snapshot.task')(gulp, argv, buildConfig);

View File

@ -14,16 +14,24 @@ module.exports = function(gulp, argv, buildConfig) {
var protractorHttpServer; var protractorHttpServer;
var snapshotValues = _.merge(snapshotConfig.platformDefauls, argv); var snapshotValues = _.merge(snapshotConfig.platformDefauls, argv);
gulp.task('protractor-server', function() { gulp.task('protractor-server', protractorServerTask);
gulp.task('snapshot-task', snapshotTask);
gulp.task('snapshot', gulp.series(
gulp.parallel('build', 'e2e', 'protractor-server'),
'snapshot-task'
));
function protractorServerTask(done) {
var app = connect().use(serveStatic(projectRoot)); // serve everything that is static var app = connect().use(serveStatic(projectRoot)); // serve everything that is static
protractorHttpServer = http.createServer(app).listen(buildConfig.protractorPort); protractorHttpServer = http.createServer(app).listen(buildConfig.protractorPort);
console.log('Serving `dist` on http://localhost:' + buildConfig.protractorPort); console.log('Serving `dist` on http://localhost:' + buildConfig.protractorPort);
}); done();
}
gulp.task('snapshot', ['e2e', 'protractor-server'], function(done) { function snapshotTask(done) {
var protractorConfigFile = path.resolve(projectRoot, 'scripts/snapshot/protractor.config.js'); var protractorConfigFile = path.resolve(projectRoot, 'scripts/snapshot/protractor.config.js');
snapshot(done, protractorConfigFile); snapshot(done, protractorConfigFile);
}); }
function snapshot(done, protractorConfigFile) { function snapshot(done, protractorConfigFile) {
snapshotValues.params.test_id = uuid.v4().split('-')[0]; snapshotValues.params.test_id = uuid.v4().split('-')[0];
@ -41,12 +49,13 @@ module.exports = function(gulp, argv, buildConfig) {
return _.template(argument, snapshotValues); return _.template(argument, snapshotValues);
}); });
e2ePublish(snapshotValues.params.test_id); // e2ePublish(snapshotValues.params.test_id);
return protractor(done, [protractorConfigFile].concat(protractorArgs)); return protractor(done, [protractorConfigFile].concat(protractorArgs));
} }
function protractor(done, args) { function protractor(done, args) {
debugger;
var child = cp.spawn('protractor', args, { var child = cp.spawn('protractor', args, {
stdio: [process.stdin, process.stdout, 'pipe'] stdio: [process.stdin, process.stdout, 'pipe']
}); });