Skip to content

Commit

Permalink
feat: Use commits, lastRelease and nextRelease passed by `seman…
Browse files Browse the repository at this point in the history
…tic-release`

Use the same `commits`, `lastRelease` and `nextRelease` as other `semantic-release` plugin, instead of retrieving them locally with `conventional-commits-core`. That avoid discrepancy in the determination of next version, last release and commits to include in release note.

BREAKING CHANGE: Expect to be passed `commits`, `lastRelease` and `nextRelease` in `options`. Require `semantic-remantic` `>=9.0.0`.
BREAKING CHANGE: Do not wrap unexpected errors in a `@semantic-release/error`. This way, in case of unexpected error (missing preset for example) `semantic-release` will fail and return with an error code
  • Loading branch information
pvdlg authored and gr2m committed Oct 29, 2017
1 parent e5a3b5b commit e99fba3
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 143 deletions.
50 changes: 33 additions & 17 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
const {callbackify} = require('util');
const url = require('url');
const getStream = require('get-stream');
const SemanticReleaseError = require('@semantic-release/error');
const conventionalChangelog = require('conventional-changelog-core');
const intoStream = require('into-stream');
const hostedGitInfo = require('hosted-git-info');
const conventionalCommitsParser = require('conventional-commits-parser').sync;
const conventionalChangelogWriter = require('conventional-changelog-writer');
const loadChangelogConfig = require('./load/changelog-config');

/**
* @callback releaseNotesGeneratorCallback
* @param {Error} error error object.
* @param {string} changelog changelog generated by the plugin.
*/

/**
* Generate the changelog for all the commits since the last release.
*
Expand All @@ -18,14 +16,32 @@ const loadChangelogConfig = require('./load/changelog-config');
* @param {Object} pluginConfig.parserOpts additional `conventional-changelog-parser` options that will overwrite ones loaded by `preset` or `config`.
* @param {Object} pluginConfig.writerOpts additional `conventional-changelog-writer` options that will overwrite ones loaded by `preset` or `config`.
* @param {Object} options semantic-release options
* @param {releaseNotesGeneratorCallback} callback The callback called with the release note.
* @param {Object} options.pkg normalized `package.json`
* @param {Array<Object>} options.commits array of commits, each containing `hash` and `message`
* @param {Object>} options.lastRelease last release with `gitHead` corresponding to the commit hash used to make the last release
* @param {Object>} options.nextRelease next release with `gitHead` corresponding to the commit hash used to make the release and the release `version`
*/
module.exports = async (pluginConfig = {}, options, callback) => {
const cb = typeof options === 'function' ? options : callback;
async function releaseNotesGenerator(
pluginConfig,
{pkg, commits, lastRelease: {gitHead: previousTag}, nextRelease: {gitHead: currentTag, version}}
) {
const {parserOpts, writerOpts} = await loadChangelogConfig(pluginConfig);
commits = commits.map(rawCommit =>
Object.assign(rawCommit, conventionalCommitsParser(rawCommit.message, parserOpts))
);
const {default: protocol, domain: host, project: repository, user: owner} = hostedGitInfo.fromUrl(pkg.repository.url);
const context = {
version,
host: url.format({protocol, host}),
owner,
repository,
previousTag,
currentTag,
linkCompare: currentTag && previousTag,
packageData: pkg,
};

return getStream(intoStream.obj(commits).pipe(conventionalChangelogWriter(context, writerOpts)));
}

try {
cb(null, await getStream(conventionalChangelog({config: await loadChangelogConfig(pluginConfig)})));
} catch (err) {
cb(new SemanticReleaseError(`Error in conventional-changelog: ${err.message}`, err.code));
}
};
module.exports = callbackify(releaseNotesGenerator);
13 changes: 2 additions & 11 deletions lib/load/changelog-config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const importFrom = require('import-from');
const pify = require('pify');
const {mergeWith} = require('lodash');
const SemanticReleaseError = require('@semantic-release/error');
const conventionalChangelogAngular = require('conventional-changelog-angular');

/**
Expand All @@ -18,17 +17,9 @@ module.exports = async ({preset, config, parserOpts, writerOpts}) => {

if (preset) {
const presetPackage = `conventional-changelog-${preset.toLowerCase()}`;
try {
loadedConfig = importFrom.silent(__dirname, presetPackage) || importFrom(process.cwd(), presetPackage);
} catch (err) {
throw new SemanticReleaseError(`Preset: "${preset}" does not exist: ${err.message}`, err.code);
}
loadedConfig = importFrom.silent(__dirname, presetPackage) || importFrom(process.cwd(), presetPackage);
} else if (config) {
try {
loadedConfig = importFrom.silent(__dirname, config) || importFrom(process.cwd(), config);
} catch (err) {
throw new SemanticReleaseError(`Config: "${config}" does not exist: ${err.message}`, err.code);
}
loadedConfig = importFrom.silent(__dirname, config) || importFrom(process.cwd(), config);
} else if (!parserOpts || !writerOpts) {
loadedConfig = conventionalChangelogAngular;
}
Expand Down
12 changes: 5 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
}
},
"dependencies": {
"@semantic-release/error": "^2.0.0",
"conventional-changelog-angular": "^1.4.0",
"conventional-changelog-core": "^1.9.0",
"conventional-changelog-writer": "^2.0.1",
"conventional-commits-parser": "^2.0.0",
"get-stream": "^3.0.0",
"hosted-git-info": "^2.5.0",
"import-from": "^2.1.0",
"into-stream": "^3.1.0",
"lodash": "^4.17.4",
"pify": "^3.0.0"
},
Expand All @@ -43,14 +45,10 @@
"eslint-plugin-prettier": "^2.3.0",
"eslint-plugin-promise": "^3.5.0",
"eslint-plugin-standard": "^3.0.1",
"execa": "^0.8.0",
"fs-extra": "^4.0.1",
"nyc": "^11.1.0",
"p-each-series": "^1.0.0",
"prettier": "^1.7.2",
"rimraf": "^2.6.1",
"semantic-release": "^8.0.0",
"tempy": "^0.2.0"
"semantic-release": "^8.0.0"
},
"engines": {
"node": ">=4"
Expand Down
22 changes: 0 additions & 22 deletions test/helpers/commits.js

This file was deleted.

Loading

0 comments on commit e99fba3

Please sign in to comment.