build: move changelog script to gulp task (#5472)

* Moves the changelog script to a gulp task that includes better instructions and error reporting.
This commit is contained in:
Paul Gschwendtner
2017-08-09 08:18:38 +02:00
committed by mmalerba
parent 333ec80750
commit fc98a1d0cf
5 changed files with 765 additions and 396 deletions
+703 -316
View File
File diff suppressed because it is too large Load Diff
+1 -2
View File
@@ -59,7 +59,6 @@
"axe-core": "^2.3.1",
"axe-webdriverjs": "^1.1.1",
"chalk": "^1.1.3",
"conventional-changelog": "^1.1.3",
"dgeni": "^0.4.7",
"dgeni-packages": "^0.19.1",
"firebase": "^4.0.0",
@@ -73,6 +72,7 @@
"gulp-clean-css": "^3.3.1",
"gulp-cli": "^1.3.0",
"gulp-connect": "^5.0.0",
"gulp-conventional-changelog": "^1.1.3",
"gulp-dom": "^0.9.17",
"gulp-flatten": "^0.3.1",
"gulp-highlight-files": "^0.0.4",
@@ -98,7 +98,6 @@
"karma-sourcemap-loader": "^0.3.7",
"madge": "^1.6.0",
"magic-string": "^0.21.3",
"merge2": "^1.0.3",
"minimatch": "^3.0.4",
"minimist": "^1.2.0",
"node-sass": "^4.5.3",
-78
View File
@@ -1,78 +0,0 @@
#!/usr/bin/env node
'use strict';
/*
* Creates a conventional changelog from the current git repository / metadata.
*/
const fs = require('fs');
const merge2 = require('merge2');
const changelog = require('conventional-changelog');
const spawnSync = require('child_process').spawnSync;
const npmVersion = require('../../package.json').version;
/**
* When the command line argument `--force` is provided, then the full changelog will created and overwritten.
* By default, it will only create the changelog from the latest tag to head and prepends it to the changelog.
*/
const isForce = process.argv.indexOf('--force') !== -1;
const previousChangelog = fs.createReadStream('CHANGELOG.md');
const gitTags = getAvailableTags();
// Whether the npm version is later than the most recent tag.
const isNpmLatest = npmVersion !== gitTags[0];
// When the npm version is the latest, use the npm version, otherwise use the latest tag.
const currentTag = isNpmLatest ? npmVersion : gitTags[0];
// When the npm version is the latest use the most recent tag. Otherwise use the previous tag.
const previousTag = isNpmLatest ? gitTags[0] : gitTags[1];
if (!isForce) {
previousChangelog.on('error', function(err) {
console.error('An error occurred, while reading the previous changelog file.\n' +
'If there is changelog file, you should create an empty file or use the `--force` flag.\n' + err);
process.exit(1);
});
}
const config = {
preset: 'angular',
releaseCount: isForce ? 0 : 1
};
const context = {
currentTag: currentTag,
previousTag: previousTag
};
let stream = changelog(config, context).on('error', function(err) {
console.error('An error occurred while generating the changelog: ' + err);
});
if (!isForce) {
// Append the previous changelog to the new generated one.
stream = merge2(stream, previousChangelog);
} else {
stream.pipe(getOutputStream())
}
// When we are pre-pending the new changelog, then we need to wait for the input stream to be ending,
// otherwise we can't write into the same destination.
if (!isForce) {
previousChangelog.on('end', function() {
stream.pipe(getOutputStream());
});
}
function getOutputStream() {
return fs.createWriteStream('CHANGELOG.md');
}
/**
* Resolves available tags over all branches from the repository metadata.
* @returns {Array.<String>} Array of available tags.
*/
function getAvailableTags() {
return spawnSync('git', ['tag']).stdout.toString().trim().split('\n').reverse();
}
+1
View File
@@ -22,3 +22,4 @@ import './tasks/coverage';
import './tasks/material-release';
import './tasks/universal';
import './tasks/validate-release';
import './tasks/changelog';
+60
View File
@@ -0,0 +1,60 @@
import {task, src, dest} from 'gulp';
import {buildConfig} from 'material2-build-tools';
import {join} from 'path';
import {yellow, red} from 'chalk';
// This imports lack of type definitions.
const gulpChangelog = require('gulp-conventional-changelog');
const gitSemverTags = require('git-semver-tags');
/** Path to the changelog markdown file of the project. */
const changelogFile = join(buildConfig.projectDir, 'CHANGELOG.md');
/** Default changelog options that are passed to gulp-conventional-changelog. */
const changelogOptions = { preset: 'angular' };
/** Task that generates a new changelog section from the latest tag to HEAD. */
task('changelog', async () => {
// Show the instructions for the changelog generation.
showChangelogInstructions();
// Cancel the generation when the latest tag is the same as the version from the "package.json".
if (await getLatestSemverTag() === buildConfig.projectVersion) {
console.error(red('Warning: Changelog won\'t change because the "package.json" version is ' +
'equal to the latest Git tag.\n'));
return;
}
return src(changelogFile)
.pipe(gulpChangelog(changelogOptions))
.pipe(dest('./'));
});
/** Task that re-generates the full changelog. */
task('changelog:full', () => {
return src(changelogFile)
.pipe(gulpChangelog({ ...changelogOptions, releaseCount: 0 }))
.pipe(dest('./'));
});
/** Prints a message that gives instructions about generating a changelog section. */
function showChangelogInstructions() {
console.info(`
${yellow('Changelog Instructions')}
When running this command, the changelog from the latest tag to HEAD will be generated.
The name of the new changelog section will be taken from the "package.json" version.
The recommended steps when creating a new changelog section:
1. Bump the version in the "package.json"
2. Run the changelog gulp task
3. Tweak the changelog manually.
`);
}
/** Returns the latest Semver Tag from the project Git repository */
function getLatestSemverTag(): Promise<string> {
return new Promise((resolve, reject) => {
return gitSemverTags((err: Error, tags: string[]) => err ? reject(err) : resolve(tags[0]));
});
}