-
Notifications
You must be signed in to change notification settings - Fork 8
/
gulpfile.js
504 lines (435 loc) · 15 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
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
'use strict';
const ansiColors = require('ansi-colors');
const chug = require('gulp-chug');
const debug = require('gulp-debug');
const fs = require('fs-extra');
const gulp = require('gulp');
const log = require('fancy-log');
const path = require('path');
const plumber = require('gulp-plumber');
const semver = require('semver');
const spawnSync = require('child_process').spawnSync;
const tmp = require('tmp');
const { series } = gulp;
const { red, yellow, green, cyan, magenta, gray } = ansiColors;
const nodeInfo = exports['node-info'] = async function nodeInfo() {
log(`Node.js ${process.version} (${process.platform})`);
log(process.env);
};
// checks to see if a package requires a node version that is less than a dependencies node requirement
exports['check-engines'] = async function checkEngines() {
const cache = {};
const issues = [];
const checkPackage = (pkgPath, depth = 0) => {
const pkgJsonPath = path.join(pkgPath, 'package.json');
if (!fs.existsSync(pkgJsonPath)) {
return false;
}
if (cache[pkgPath]) {
return cache[pkgPath];
}
const pkgJson = require(pkgJsonPath);
const info = pkgJson.engines && pkgJson.engines.node && semver.coerce(pkgJson.engines.node);
const node = cache[pkgPath] = info ? info.version : null;
// console.log(`${' '.repeat(depth)}${green(pkgJson.name)}${node ? ` (${node})` : ''}`);
if (pkgJson.dependencies) {
for (const dep of Object.keys(pkgJson.dependencies)) {
for (let wd = pkgPath; true; wd = path.dirname(wd)) {
const depNode = checkPackage(path.join(wd, 'node_modules', dep), depth + 1);
if (!cache[pkgPath] || (depNode && semver.gt(depNode, cache[pkgPath]))) {
cache[pkgPath] = depNode;
}
if (/^@axway/.test(pkgJson.name) && node && depNode && semver.lt(node, depNode)) {
issues.push({
name: pkgJson.name,
node,
dep,
depNode
});
break;
}
if (depNode !== false) {
// depNode is null (no node version) or a string
break;
}
if (wd === __dirname) {
throw new Error(`Unable to find dependency "${dep}"`);
}
}
}
}
return cache[pkgPath];
};
const nodeVer = checkPackage(`${__dirname}/packages/axway-cli`);
console.log(`\nMinimum Node version should be ${cyan(nodeVer)}\n`);
if (issues.length) {
console.log(`Found ${issues.length} issue${issues.length === 1 ? '' : 's'}`);
console.log(issues.map(({ name, node, dep, depNode }) => ` ${green(name)} requires ${node}\n ${cyan(dep)} requires ${depNode}`).join('\n') + '\n');
}
};
/*
* lint tasks
*/
exports.lint = function lint() {
return gulp
.src([
path.join(__dirname, 'packages/*/gulpfile.js')
])
.pipe(debug({ title: 'Linting project:' }))
.pipe(plumber())
.pipe(chug({ tasks: [ 'lint' ] }));
};
/*
* build tasks
*/
const build = exports.build = async function build() {
return runLernaBuild();
};
/*
* test tasks
*/
let origHomeDir = process.env.HOME;
let tmpHomeDir = null;
async function runTests(cover, all) {
try {
await fs.remove(path.join(__dirname, '.nyc_output'));
await fs.remove(path.join(__dirname, 'coverage'));
if (all) {
process.env.AXWAY_TEST = '1'; // allow telemetry tests to run
}
process.env.AXWAY_TEST_GLOBAL_PACKAGE_DIR = path.join(__dirname, 'packages');
process.env.SPAWN_WRAP_SHIM_ROOT = origHomeDir;
process.env.NODE_ENV = 'test'; // disables the update check
// process.env.SNOOPLOGG = '*';
tmpHomeDir = tmp.dirSync({
mode: '755',
prefix: 'axway-cli-test-home-',
unsafeCleanup: true
}).name;
log(`Protecting home directory, overriding HOME with temp dir: ${cyan(tmpHomeDir)}`);
process.env.HOME = process.env.USERPROFILE = tmpHomeDir;
if (process.platform === 'win32') {
process.env.HOMEDRIVE = path.parse(tmpHomeDir).root.replace(/[\\/]/g, '');
process.env.HOMEPATH = tmpHomeDir.replace(process.env.HOMEDRIVE, '');
}
const runner = require('@axway/gulp-tasks/src/test-runner');
await runner.runTests({
all,
cover,
projectDir: __dirname,
root: __dirname,
slow: 15000,
timeout: 40000
});
} finally {
// restore home directory so that we can delete the temp one
if (tmpHomeDir) {
log(`Removing temp home directory: ${cyan(tmpHomeDir)}`);
try {
fs.removeSync(tmpHomeDir);
} catch (err) {
log(`Failed to remove temp home directory: ${err.toString()}`);
}
}
log(`Restoring home directory: ${cyan(origHomeDir)}`);
process.env.HOME = origHomeDir;
}
}
exports.integration = series(nodeInfo, build, function test() { return runTests(true); });
exports['integration-only'] = series(nodeInfo, function test() { return runTests(true); });
exports.test = series(nodeInfo, build, function coverage() { return runTests(true, true); });
/*
* watch task
*/
exports.watch = series(build, async function watch() {
const srcWatcher = gulp
.watch(`${__dirname}/packages/*/src/**/*.js`)
.on('all', (type, path) => {
// FIXME: There's almost certainly a better way of doing this than replacing \\ with /
path = path.replace(/\\/g, '/');
const m = path.match(new RegExp(`^(${__dirname.replace(/\\/g, '/')}/(packages/([^\/]+)))`));
if (m) {
log(`Detected change: ${cyan(path)}`);
const pkgJson = path.join(m[1], 'package.json');
try {
runLernaBuild(JSON.parse(fs.readFileSync(pkgJson)).name);
} catch (e) {
log(`Failed to read/parse ${pkgJson}: ${e.toString()}`);
}
}
});
let stopping = false;
return new Promise(resolve => {
process.on('SIGINT', () => {
if (!stopping) {
stopping = true;
srcWatcher.close();
resolve();
}
});
});
});
async function runLernaBuild(scope) {
let { execPath } = process;
const args = [ './node_modules/.bin/lerna', 'run', 'build', '--parallel' ];
if (process.platform === 'win32') {
args.shift();
execPath = path.join(__dirname, 'node_modules', '.bin', 'lerna.cmd');
}
if (scope) {
args.push('--scope', scope);
}
log(`Running ${execPath} ${args.join(' ')}`);
const { status } = spawnSync(execPath, args, { stdio: 'inherit' });
if (status) {
throw new Error(`lerna build failed ${scope ? `for ${scope}` : ''}`);
}
}
exports['release-notes'] = async function releaseNotes() {
const { cyan } = require('ansi-colors');
const https = require('https');
const semver = require('semver');
const tar = require('tar-stream');
const zlib = require('zlib');
const packages = {};
const re = /^@axway\//;
const tempDir = tmp.dirSync({
mode: '755',
prefix: 'axway-cli-release-notes-',
unsafeCleanup: true
}).name;
const cacheDir = path.join(__dirname, '.npm-info');
await fs.mkdirs(cacheDir);
const fetch = async name => {
const cacheFile = path.join(cacheDir, `${name}.json`);
await fs.mkdirs(path.dirname(cacheFile));
let info;
if (fs.existsSync(cacheFile)) {
log(`Fetching ${cyan(name)} from cache`);
const s = fs.readFileSync(cacheFile, 'utf8');
info = s ? JSON.parse(s) : null;
} else {
log(`Fetching ${cyan(name)}`);
const { status, stdout, stderr } = spawnSync('npm', [ 'view', name, '--json' ]);
if (status) {
console.error('Failed to get package info:');
console.error(stdout.toString());
console.error(stderr.toString());
process.exit(1);
}
const s = stdout.toString();
fs.writeFileSync(cacheFile, s);
info = s ? JSON.parse(s) : null;
}
// if more than one is returned, get the latest
if (Array.isArray(info)) {
let pkg;
for (const i of info) {
if (!pkg || semver.gt(i.version, pkg.version)) {
pkg = i;
}
}
info = pkg;
}
return info;
};
const getPackageInfo = async (name, ver) => {
const info = await fetch(`${name}@${ver}`);
if (!info || packages[name]) {
return info;
}
ver = info.version;
log(`Initializing new package ${name}`);
packages[name] = { latest: null, versions: {} };
log(` Versions: ${info.versions.join(', ')}`);
for (const version of info.versions) {
if (!packages[name].versions[version] && semver.valid(version) && semver.gt(version, '0.0.0')) {
const { prerelease } = semver.parse(version);
if (!prerelease || !prerelease.length) {
log(` Initializing pacakge ${name}@${version}`);
const verInfo = await fetch(`${name}@${version}`);
if (verInfo) {
packages[name].versions[version] = { changelog: null, ts: info.time[version], version };
for (const type of [ 'dependencies', 'devDependencies' ]) {
if (verInfo[type]) {
for (const [ dep, range ] of Object.entries(verInfo[type])) {
if (re.test(dep)) {
await getPackageInfo(dep, range);
}
}
}
}
}
}
}
}
return info;
};
const processChangelog = (name, changelog) => {
const changes = changelog.split('\n\n#').map((s, i) => `${i ? '#' : ''}${s}`.trim());
for (const chunk of changes) {
const m = chunk.match(/^# v?([^\s\n]*)[^\n]*\n+(.+)$/s);
if (!m) {
continue;
}
const { version } = semver.coerce(m[1]);
if (packages[name].versions[m[1]]) {
packages[name].versions[m[1]].changelog = m[2];
} else if (packages[name].versions[version]) {
packages[name].versions[version].changelog = m[2];
} else {
log(red(`Package ${name} does not have a version ${m[1]}! (${Object.keys(packages[name].versions).join(', ')})`));
}
}
};
try {
// Step 1: get all the `axway` releases and their `@axway/*` dependencies
const { versions } = await fetch('axway');
for (const ver of versions) {
if (semver.valid(ver) && semver.gt(ver, '0.0.0')) {
const { prerelease } = semver.parse(ver);
if (!prerelease || !prerelease.length) {
await getPackageInfo('axway', ver);
}
}
}
// Step 2: add in the local packages
const local = {};
for (const subdir of fs.readdirSync(path.join(__dirname, 'packages'))) {
try {
const pkgJson = fs.readJsonSync(path.join(__dirname, 'packages', subdir, 'package.json'));
let { name, version } = pkgJson;
local[name] = pkgJson;
const changelogFile = path.join(__dirname, 'packages', subdir, 'CHANGELOG.md');
const changelog = fs.existsSync(changelogFile) ? fs.readFileSync(changelogFile, 'utf8') : null;
let ts = null;
const m = changelog && changelog.match(/^# v([^\s]+)/);
if (m && m[1] !== version) {
// set release timestamp to now unless package is axway, then make it 10 seconds older
ts = new Date(Date.now() + (name === 'axway' || name === '@axway/amplify-cli' ? 10000 : 0));
pkgJson.version = version = m[1];
}
// TEMP: another v2 prerelease hack
version = semver.coerce(version).version;
if (name === '@axway/amplify-cli') {
name = 'axway';
}
if (!packages[name]) {
packages[name] = { latest: null, versions: {} };
}
if (!packages[name] || !packages[name].versions[version]) {
packages[name].local = true;
packages[name].versions[version] = { changelog: null, local: true, ts, version };
}
if (changelog) {
processChangelog(name, changelog);
}
} catch (e) {}
}
// Step 3: for each non-local package, fetch the latest npm package and extract the changelog
for (const [ pkg, info ] of Object.entries(packages)) {
if (!packages[pkg].latest) {
packages[pkg].latest = Object.keys(info.versions).sort(semver.compare).pop();
}
if (info.local) {
continue;
}
const changelogFile = path.join(cacheDir, `${pkg}@${info.latest}_CHANGELOG.md`);
if (fs.existsSync(changelogFile)) {
processChangelog(pkg, fs.readFileSync(changelogFile, 'utf8'));
} else {
const url = `https://registry.npmjs.org/${pkg}/-/${path.basename(pkg)}-${info.latest}.tgz`;
const file = path.join(tempDir, `${path.basename(pkg)}-${info.latest}.tgz`);
await new Promise((resolve, reject) => {
const dest = fs.createWriteStream(file);
dest.on('finish', () => dest.close(resolve));
log(`Downloading ${cyan(url)}`);
https.get(url, response => response.pipe(dest))
.on('error', reject);
});
await new Promise((resolve, reject) => {
const gunzip = zlib.createGunzip();
const extract = tar.extract();
extract.on('entry', (header, stream, next) => {
if (header.name !== 'package/CHANGELOG.md') {
stream.resume();
return next();
}
let changelog = '';
stream
.on('data', chunk => changelog += chunk)
.on('end', () => {
fs.writeFileSync(changelogFile, changelog, 'utf8');
processChangelog(pkg, changelog);
next();
})
.on('error', reject)
.resume();
});
extract.on('finish', resolve);
extract.on('error', reject);
log(`Extract changelog from ${cyan(file)}`);
fs.createReadStream(file).pipe(gunzip).pipe(extract);
});
}
}
} finally {
fs.removeSync(tempDir);
}
const axwayCli = packages['axway'];
delete packages['axway'];
const pkgs = Object.keys(packages).sort();
// Step 4: loop over every `axway` release and generate the changelog
for (const ver of Object.keys(axwayCli.versions).sort(semver.compare)) {
const { raw } = semver.coerce(ver);
if (semver.lte(raw, '2.0.0')) {
continue;
}
const { major, minor, patch } = semver.parse(ver);
const cleanVersion = `${major}.${minor}.${patch}`;
const dest = path.join(__dirname, 'docs', 'Release Notes', `Axway CLI ${raw}.md`);
const { changelog, local, ts } = axwayCli.versions[ver];
const dt = ts ? new Date(ts) : new Date();
const rd = ts && dt.toDateString().split(' ').slice(1);
let s = `# Axway CLI ${raw}\n\n## ${local ? 'Unreleased' : `${rd[0]} ${rd[1]}, ${rd[2]}`}\n\n`;
if (patch === 0) {
if (minor === 0) {
s += 'This is a major release with breaking changes, new features, bug fixes, and dependency updates.\n\n';
} else {
s += 'This is a minor release with new features, bug fixes, and dependency updates.\n\n';
}
} else {
s += 'This is a patch release with bug fixes and minor dependency updates.\n\n';
}
s += `### Installation\n\n\`\`\`\nnpm i -g axway@${cleanVersion}\n\`\`\`\n\n`
if (changelog) {
s += '### axway\n\n';
s += ` * **v${cleanVersion}**${ts ? ` - ${dt.toLocaleDateString()}` : ''}\n\n`;
s += `${changelog.split('\n').map(s => ` ${s}`).join('\n')}\n\n`;
}
for (const pkg of pkgs) {
// the AMPLIFY CLI and Auth SDK are deprecated, so ignore them
if (pkg === '@axway/amplify-cli' || pkg === '@axway/amplify-auth-sdk') {
continue;
}
const vers = Object.keys(packages[pkg].versions).filter(ver => {
const { ts } = packages[pkg].versions[ver];
return !ts || new Date(ts) < dt;
}).sort(semver.rcompare);
let vs = '';
for (const v of vers) {
if (packages[pkg].versions[v].changelog) {
const pts = new Date(packages[pkg].versions[v].ts);
vs += ` * **v${v}** - ${pts.toLocaleDateString()}\n\n`;
vs += `${packages[pkg].versions[v].changelog.split('\n').map(s => ` ${s}`).join('\n')}\n\n`;
}
delete packages[pkg].versions[v];
}
if (vs) {
s += `### ${pkg.replace(/@.+\//, '')}\n\n${vs}`;
}
}
log(`Writing release notes ${cyan(dest)}`);
fs.outputFileSync(dest, s.trim());
}
};