-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
gulpfile.ts
255 lines (216 loc) · 8.32 KB
/
gulpfile.ts
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
import fs from 'fs';
import path from 'path';
import {exec, ExecOptions} from 'child_process';
import gulp from 'gulp';
import gutil from 'gulp-util';
import gulpBabel from 'gulp-babel';
import gulpMocha from 'gulp-mocha';
import plumber from 'gulp-plumber';
import gulpIf from 'gulp-if';
import del from 'del';
import lazypipe from 'lazypipe';
import merge from 'merge-stream';
import shell from 'shelljs';
import ghPages from 'gulp-gh-pages';
import conventionalChangelog, {Commit, Context, GulpConventionalChangelogOptions} from 'gulp-conventional-changelog';
let watching = false;
const mocha = lazypipe()
.pipe(gulpMocha, {
reporter: 'spec',
timeout: 120000,
slow: 500,
require: [
'./mocha.conf',
'should'
],
compilers: ['js:babel-core/register']
});
const transpile = lazypipe()
.pipe(gulpBabel);
export function clean() {
return del(['generators/**/*', './test/(**|!fixtures/node_modules)/*']);
}
export function 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);
}
export function watch() {
watching = true;
return gulp.watch('src/**/*.js', babel);
}
export function 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);
}
export const build = gulp.series(
clean,
babel,
copy
);
function processJson(src: string, dest: string, opt: {appName: string, genVer: string, private: boolean, test: boolean}) {
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);
const 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/gulp-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: 'deps'|'test') {
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 processJson('templates/app/_package.json', dest + 'package.json', {appName, genVer, private: !deps, test: test});
}
gulp.task('updateFixtures', cb => {
return gulp.series(gulp.parallel('updateFixtures:test', 'updateFixtures:deps'), cb);
});
gulp.task('updateFixtures:test', () => {
return updateFixtures('test');
});
gulp.task('updateFixtures:deps', () => {
return updateFixtures('deps');
});
function execAsync(cmd: string, opt?: ExecOptions) {
return new Promise((resolve, reject) => {
exec(cmd, opt, (err, stdout, stderr) => {
if(err) {
console.log(`stderr: ${stderr}`);
return reject(err);
}
return resolve(stdout);
});
});
}
export function installFixtures() {
gutil.log('installing npm dependencies for generated app');
let progress = setInterval(() => {
process.stdout.write('.');
}, 1000);
shell.cd('test/fixtures');
let installCommand;
if(process.platform === 'win32') {
installCommand = 'yarn --version >nul 2>&1 && ( yarn install ) || ( npm install --quiet )';
} else {
installCommand = 'type yarn &> /dev/null | yarn install || npm install --quiet';
}
return execAsync(installCommand, {
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());
});
gulp.task('updateSubmodules', () => console.log('TODO'));
gulp.task('changelog', () => console.log('TODO'));
gulp.task('generateDemo', () => console.log('TODO'));
gulp.task('demo', () => console.log('TODO')); // ['clean:demo', 'generateDemo']
gulp.task('releaseDemo', () => console.log('TODO')); //['demo', 'releaseDemoBuild', 'buildcontrol:release']
gulp.task('releaseDemoBuild', () => console.log('TODO'));
gulp.task('deps', () => console.log('TODO')); // updateFixtures, david
gulp.task('release', () => console.log('TODO'));
gulp.task('lint', () => console.log('TODO')); // ['gulpfile.js', 'src/**/*.js']
gulp.task('copy_docs_images', () => {
return gulp.src('./media/svg/*')
.pipe(gulp.dest('./static/'));
});
gulp.task('gh-pages', () => {
return gulp.src('./static/**/*')
.pipe(ghPages());
});
gulp.task('docs', cb => {
return gulp.series('daux', 'copy_docs_images', 'gh-pages', cb);
});
function finalizeContext(context: Context, opts: GulpConventionalChangelogOptions, commits: Commit[], keyCommit: Commit) {
const {gitSemverTags, commitGroups} = context;
if (!gitSemverTags) return context;
if((!context.currentTag || !context.previousTag) && keyCommit) {
const match = /tag:\s*(.+?)[,)]/gi.exec(keyCommit.gitTags);
context.currentTag = context.currentTag || match ? match![1] : null;
const index = gitSemverTags.indexOf(context.currentTag || '');
const previousTag = context.previousTag = gitSemverTags[index + 1];
if(!previousTag) {
if(opts.append) {
context.previousTag = context.previousTag || commits[0] ? commits[0].hash : null;
} else {
context.previousTag = context.previousTag || commits[commits.length - 1] ? commits[commits.length - 1].hash : null;
}
}
} else {
context.previousTag = context.previousTag || gitSemverTags[0];
context.currentTag = context.currentTag || 'v' + context.version;
}
if(!Array.isArray(commitGroups)) {
return context;
}
for(const commitGroup of commitGroups) {
const commits = commitGroup.commits;
if(!Array.isArray(commits)) {
return context;
}
for(let n = 1; n < commits.length; n++) {
const commit = commits[n];
const prevCommit = commits[n - 1];
if(commit.scope && commit.scope === prevCommit.scope) {
commit.subScope = true;
if(prevCommit.scope && !prevCommit.subScope) {
prevCommit.leadScope = true;
}
}
}
}
return context;
}
let commitPartial = fs.readFileSync(path.resolve(__dirname, 'task-utils/changelog-templates/commit.hbs')).toString();
export function changelog() {
return gulp.src('CHANGELOG.md', {buffer: false})
.pipe(conventionalChangelog({preset: 'angular'}, {}, {}, {}, {
finalizeContext,
commitPartial
}))
.pipe(gulp.dest('./'));
}