|
| 1 | +'use strict'; |
| 2 | +const fs = require('fs'); |
| 3 | +const path = require('path'); |
| 4 | +const exec = require('child_process').exec; |
| 5 | +const _ = require('lodash'); |
| 6 | +const Promise = require('bluebird'); |
| 7 | +const gulp = require('gulp'); |
| 8 | +const gutil = require('gulp-util'); |
| 9 | +const babel = require('gulp-babel'); |
| 10 | +const gulpMocha = require('gulp-mocha'); |
| 11 | +const plumber = require('gulp-plumber'); |
| 12 | +const gulpIf = require('gulp-if'); |
| 13 | +const del = require('del'); |
| 14 | +const lazypipe = require('lazypipe'); |
| 15 | +const runSequence = require('run-sequence'); |
| 16 | +const merge = require('merge-stream'); |
| 17 | +const shell = require('shelljs'); |
| 18 | + |
| 19 | +var watching = false; |
| 20 | + |
| 21 | +const mocha = lazypipe() |
| 22 | + .pipe(gulpMocha, { |
| 23 | + reporter: 'spec', |
| 24 | + timeout: 120000, |
| 25 | + slow: 500, |
| 26 | + globals: { |
| 27 | + should: require('should') |
| 28 | + }, |
| 29 | + require: [ |
| 30 | + './mocha.conf' |
| 31 | + ] |
| 32 | + }); |
| 33 | + |
| 34 | +const transpile = lazypipe() |
| 35 | + .pipe(babel); |
| 36 | + |
| 37 | +gulp.task('clean', () => { |
| 38 | + return del(['generators/**/*', './test/(**|!fixtures/node_modules|!fixtures/bower_components)/*']); |
| 39 | +}); |
| 40 | + |
| 41 | +gulp.task('babel', () => { |
| 42 | + let generators = gulp.src(['src/generators/**/*.js']) |
| 43 | + .pipe(gulpIf(watching, plumber())) |
| 44 | + .pipe(transpile()) |
| 45 | + .pipe(gulp.dest('generators')); |
| 46 | + |
| 47 | + let test = gulp.src(['src/test/**/*.js']) |
| 48 | + .pipe(gulpIf(watching, plumber())) |
| 49 | + .pipe(transpile()) |
| 50 | + .pipe(gulp.dest('test')); |
| 51 | + |
| 52 | + return merge(generators, test); |
| 53 | +}); |
| 54 | + |
| 55 | +gulp.task('watch', () => { |
| 56 | + watching = true; |
| 57 | + return gulp.watch('src/**/*.js', ['babel']); |
| 58 | +}); |
| 59 | + |
| 60 | +gulp.task('copy', () => { |
| 61 | + let nonJsGen = gulp.src(['src/generators/**/*', '!src/generators/**/*.js'], {dot: true}) |
| 62 | + .pipe(gulp.dest('generators')); |
| 63 | + |
| 64 | + let nonJsTest = gulp.src(['src/test/**/*', '!src/test/**/*.js'], {dot: true}) |
| 65 | + .pipe(gulp.dest('test')); |
| 66 | + |
| 67 | + return merge(nonJsGen, nonJsTest); |
| 68 | +}); |
| 69 | + |
| 70 | +gulp.task('build', cb => { |
| 71 | + return runSequence( |
| 72 | + 'clean', |
| 73 | + 'babel', |
| 74 | + 'copy', |
| 75 | + cb |
| 76 | + ); |
| 77 | +}); |
| 78 | + |
| 79 | +var processJson = function(src, dest, opt) { |
| 80 | + return new Promise((resolve, reject) => { |
| 81 | + // read file, strip all ejs conditionals, and parse as json |
| 82 | + fs.readFile(path.resolve(src), 'utf8', (err, data) => { |
| 83 | + if(err) return reject(err); |
| 84 | + |
| 85 | + var json = JSON.parse(data.replace(/<%(.*)%>/g, '')); |
| 86 | + |
| 87 | + // set properties |
| 88 | + json.name = opt.appName; |
| 89 | + json.description = opt.private |
| 90 | + ? null |
| 91 | + : 'The purpose of this repository is to track all the possible dependencies of an application created by generator-angular-fullstack.'; |
| 92 | + json.version = opt.genVer; |
| 93 | + json.private = opt.private; |
| 94 | + |
| 95 | + // stringify json and write it to the destination |
| 96 | + fs.writeFile(path.resolve(dest), JSON.stringify(json, null, 2), err => { |
| 97 | + if(err) reject(err); |
| 98 | + else resolve(); |
| 99 | + }); |
| 100 | + }); |
| 101 | + }); |
| 102 | +}; |
| 103 | + |
| 104 | +function updateFixtures(target) { |
| 105 | + const deps = target === 'deps'; |
| 106 | + const genVer = require('./package.json').version; |
| 107 | + const dest = __dirname + (deps ? '/angular-fullstack-deps/' : '/test/fixtures/'); |
| 108 | + const appName = deps ? 'angular-fullstack-deps' : 'tempApp'; |
| 109 | + |
| 110 | + return Promise.all([ |
| 111 | + processJson('templates/app/_package.json', dest + 'package.json', {appName, genVer, private: !deps}), |
| 112 | + processJson('templates/app/_bower.json', dest + 'bower.json', {appName, genVer, private: !deps}) |
| 113 | + ]); |
| 114 | +} |
| 115 | + |
| 116 | +gulp.task('updateFixtures', cb => { |
| 117 | + return runSequence(['updateFixtures:test', 'updateFixtures:deps'], cb); |
| 118 | +}); |
| 119 | +gulp.task('updateFixtures:test', () => { |
| 120 | + return updateFixtures('test'); |
| 121 | +}); |
| 122 | +gulp.task('updateFixtures:deps', () => { |
| 123 | + return updateFixtures('deps'); |
| 124 | +}); |
| 125 | + |
| 126 | +function execAsync(cmd, opt) { |
| 127 | + return new Promise((resolve, reject) => { |
| 128 | + exec(cmd, opt, (err, stdout, stderr) => { |
| 129 | + if(err) { |
| 130 | + console.log(`stderr: ${stderr}`); |
| 131 | + return reject(err); |
| 132 | + } |
| 133 | + |
| 134 | + return resolve(stdout); |
| 135 | + }) |
| 136 | + }); |
| 137 | +} |
| 138 | + |
| 139 | +gulp.task('installFixtures', function() { |
| 140 | + gutil.log('installing npm & bower dependencies for generated app'); |
| 141 | + let progress = setInterval(() => { |
| 142 | + process.stdout.write('.'); |
| 143 | + }, 1 * 1000); |
| 144 | + shell.cd('test/fixtures'); |
| 145 | + |
| 146 | + return Promise.all([ |
| 147 | + execAsync('npm install --quiet', {cwd: '../fixtures'}), |
| 148 | + execAsync('bower install', {cwd: '../fixtures'}) |
| 149 | + ]).then(() => { |
| 150 | + process.stdout.write('\n'); |
| 151 | + if(!process.env.SAUCE_USERNAME) { |
| 152 | + gutil.log('running npm run-script update-webdriver'); |
| 153 | + return execAsync('npm run-script update-webdriver').then(() => { |
| 154 | + clearInterval(progress); |
| 155 | + process.stdout.write('\n'); |
| 156 | + shell.cd('../../'); |
| 157 | + }); |
| 158 | + } else { |
| 159 | + clearInterval(progress); |
| 160 | + process.stdout.write('\n'); |
| 161 | + shell.cd('../../'); |
| 162 | + return Promise.resolve(); |
| 163 | + } |
| 164 | + }); |
| 165 | +}); |
| 166 | + |
| 167 | +gulp.task('test', () => { |
| 168 | + return gulp.src(['test/pre.test.js', 'test/*.test.js']) |
| 169 | + .pipe(mocha()); |
| 170 | +}); |
0 commit comments