forked from angular-fullstack/generator-angular-fullstack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
176 lines (151 loc) · 5.36 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
'use strict';
const fs = require('fs');
const path = require('path');
const exec = require('child_process').exec;
const _ = require('lodash');
const Promise = require('bluebird');
const gulp = require('gulp');
const gutil = require('gulp-util');
const babel = require('gulp-babel');
const gulpMocha = require('gulp-mocha');
const plumber = require('gulp-plumber');
const gulpIf = require('gulp-if');
const del = require('del');
const lazypipe = require('lazypipe');
const runSequence = require('run-sequence');
const merge = require('merge-stream');
const shell = require('shelljs');
var watching = false;
const mocha = lazypipe()
.pipe(gulpMocha, {
reporter: 'spec',
timeout: 120000,
slow: 500,
globals: {
should: require('should')
},
require: [
'./mocha.conf'
]
});
const transpile = lazypipe()
.pipe(babel);
gulp.task('clean', () => {
return del(['generators/**/*', './test/(**|!fixtures/node_modules|!fixtures/bower_components)/*']);
});
gulp.task('babel', () => {
let generators = gulp.src(['src/generators/**/*.js'])
.pipe(gulpIf(watching, plumber()))
.pipe(transpile())
.pipe(gulp.dest('generators'));
let test = gulp.src(['src/test/**/*.js'])
.pipe(gulpIf(watching, plumber()))
.pipe(transpile())
.pipe(gulp.dest('test'));
return merge(generators, test);
});
gulp.task('watch', () => {
watching = true;
return gulp.watch('src/**/*.js', ['babel']);
});
gulp.task('copy', () => {
let nonJsGen = gulp.src(['src/generators/**/*', '!src/generators/**/*.js'], {dot: true})
.pipe(gulp.dest('generators'));
let nonJsTest = gulp.src(['src/test/**/*', '!src/test/**/*.js'], {dot: true})
.pipe(gulp.dest('test'));
return merge(nonJsGen, nonJsTest);
});
gulp.task('build', cb => {
return runSequence(
'clean',
'babel',
'copy',
cb
);
});
var processJson = function(src, dest, opt) {
return new Promise((resolve, reject) => {
// read file, strip all ejs conditionals, and parse as json
fs.readFile(path.resolve(src), 'utf8', (err, data) => {
if(err) return reject(err);
var json = JSON.parse(data.replace(/<%(.*)%>/g, ''));
if(/package.json/g.test(src) && opt.test) {
delete json.scripts.postinstall;
json.scripts['update-webdriver'] = 'node node_modules/grunt-protractor-runner/node_modules/protractor/bin/webdriver-manager update || node node_modules/protractor/bin/webdriver-manager update';
}
// set properties
json.name = opt.appName;
json.description = opt.private
? null
: 'The purpose of this repository is to track all the possible dependencies of an application created by generator-angular-fullstack.';
json.version = opt.genVer;
json.private = opt.private;
// stringify json and write it to the destination
fs.writeFile(path.resolve(dest), JSON.stringify(json, null, 2), err => {
if(err) reject(err);
else resolve();
});
});
});
};
function updateFixtures(target) {
const deps = target === 'deps';
const test = target === 'test';
const genVer = require('./package.json').version;
const dest = __dirname + (deps ? '/angular-fullstack-deps/' : '/test/fixtures/');
const appName = deps ? 'angular-fullstack-deps' : 'tempApp';
return Promise.all([
processJson('templates/app/_package.json', dest + 'package.json', {appName, genVer, private: !deps, test: test}),
processJson('templates/app/_bower.json', dest + 'bower.json', {appName, genVer, private: !deps, test: test})
]);
}
gulp.task('updateFixtures', cb => {
return runSequence(['updateFixtures:test', 'updateFixtures:deps'], cb);
});
gulp.task('updateFixtures:test', () => {
return updateFixtures('test');
});
gulp.task('updateFixtures:deps', () => {
return updateFixtures('deps');
});
function execAsync(cmd, opt) {
return new Promise((resolve, reject) => {
exec(cmd, opt, (err, stdout, stderr) => {
if(err) {
console.log(`stderr: ${stderr}`);
return reject(err);
}
return resolve(stdout);
})
});
}
gulp.task('installFixtures', function() {
gutil.log('installing npm & bower dependencies for generated app');
let progress = setInterval(() => {
process.stdout.write('.');
}, 1 * 1000);
shell.cd('test/fixtures');
return Promise.all([
execAsync('npm install --quiet', {cwd: '../fixtures'}),
execAsync('bower install', {cwd: '../fixtures'})
]).then(() => {
process.stdout.write('\n');
if(!process.env.SAUCE_USERNAME) {
gutil.log('running npm run-script update-webdriver');
return execAsync('npm run-script update-webdriver').then(() => {
clearInterval(progress);
process.stdout.write('\n');
shell.cd('../../');
});
} else {
clearInterval(progress);
process.stdout.write('\n');
shell.cd('../../');
return Promise.resolve();
}
});
});
gulp.task('test', () => {
return gulp.src(['test/pre.test.js', 'test/*.test.js'])
.pipe(mocha());
});