Skip to content

Commit

Permalink
refactor: pass full semantic-release context to hooks implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
pvdlg authored and gr2m committed Feb 12, 2018
1 parent 86a2e90 commit 375292e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 28 deletions.
11 changes: 6 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const publishGit = require('./lib/publish');

let verified;

async function verifyConditions(pluginConfig, {options, logger}) {
async function verifyConditions(pluginConfig, context) {
const {options} = context;
// If the Git publish plugin is used and has `assets` or `message` configured, validate them now in order to prevent any release if the configuration is wrong
if (options.publish) {
const publishPlugin =
Expand All @@ -13,16 +14,16 @@ async function verifyConditions(pluginConfig, {options, logger}) {
pluginConfig.assets = pluginConfig.assets || publishPlugin.assets;
pluginConfig.message = pluginConfig.message || publishPlugin.message;
}
await verifyGit(pluginConfig, options, logger);
await verifyGit(pluginConfig);
verified = true;
}

async function publish(pluginConfig, {options, lastRelease, nextRelease, logger}) {
async function publish(pluginConfig, context) {
if (!verified) {
await verifyGit(pluginConfig, options, logger);
await verifyGit(pluginConfig);
verified = true;
}
await publishGit(pluginConfig, options, lastRelease, nextRelease, logger);
await publishGit(pluginConfig, context);
}

module.exports = {verifyConditions, publish};
18 changes: 5 additions & 13 deletions lib/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,13 @@ const SKW_JSON = 'npm-shrinkwrap.json';
* @param {String} [pluginConfig.message] The message for the release commit.
* @param {String} [pluginConfig.gitUserName] The username to use for commiting (git `user.name` config).
* @param {String} [pluginConfig.gitUserEmail] The email to use for commiting (git `user.email` config).
* @param {Object} options `semantic-release` configuration.
* @param {String} options.repositoryUrl The remote git repository URL.
* @param {String} options.branch The remote branch to publish to.
* @param {Object} logger Global logger.
* @param {Object} lastRelease The last release.
* @param {String} lastRelease.version The last release version.
* @param {String} lastRelease.gitHead The commit sha corresponding to the last release.
* @param {String} lastRelease.gitTag The tag corresponding to the last release.
* @param {Object} nextRelease The next release.
* @param {String} nextRelease.version The next release version.
* @param {String} nextRelease.gitHead The commit sha of the head commit.
* @param {String} nextRelease.gitTag The git tag to use to make the next release.
* @param {Object} context semantic-release context.
* @param {Object} context.options `semantic-release` configuration.
* @param {Object} context.lastRelease The last release.
* @param {Object} context.nextRelease The next release.
* @param {Object} logger Global logger.
*/
module.exports = async (pluginConfig, {branch, repositoryUrl}, lastRelease, nextRelease, logger) => {
module.exports = async (pluginConfig, {options: {branch, repositoryUrl}, lastRelease, nextRelease, logger}) => {
const {gitUserEmail, gitUserName, message, assets} = resolveConfig(pluginConfig);
const patterns = [];
const modifiedFiles = await getModifiedFiles();
Expand Down
20 changes: 10 additions & 10 deletions test/publish.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ test.serial(
await outputFile('package-lock.json', "{name: 'test-package'}");
await outputFile('npm-shrinkwrap.json', "{name: 'test-package'}");

await publish(pluginConfig, t.context.options, lastRelease, nextRelease, t.context.logger);
await publish(pluginConfig, {options: t.context.options, lastRelease, nextRelease, logger: t.context.logger});

// Verify the remote repo has a the version referencing the same commit sha at the local head
const [commit] = await gitGetCommits();
Expand Down Expand Up @@ -75,7 +75,7 @@ test.serial(
await outputFile('package-lock.json', "{name: 'test-package'}");
await outputFile('npm-shrinkwrap.json', "{name: 'test-package'}");

await publish(pluginConfig, t.context.options, lastRelease, nextRelease, t.context.logger);
await publish(pluginConfig, {options: t.context.options, lastRelease, nextRelease, logger: t.context.logger});

// Verify no files have been commited
t.deepEqual(await gitCommitedFiles(), []);
Expand All @@ -95,7 +95,7 @@ Last release: \${lastRelease.version}
const nextRelease = {version: '2.0.0', gitTag: 'v2.0.0', notes: 'Test release note'};
await outputFile('CHANGELOG.md', 'Initial CHANGELOG');

await publish(pluginConfig, t.context.options, lastRelease, nextRelease, t.context.logger);
await publish(pluginConfig, {options: t.context.options, lastRelease, nextRelease, logger: t.context.logger});

// Verify the files that have been commited
t.deepEqual(await gitCommitedFiles(), ['CHANGELOG.md']);
Expand All @@ -122,7 +122,7 @@ test.serial('Commit files matching the patterns in "assets"', async t => {
await outputFile('dir2/file6.js', 'Test content');
await outputFile('dir2/file7.css', 'Test content');

await publish(pluginConfig, t.context.options, lastRelease, nextRelease, t.context.logger);
await publish(pluginConfig, {options: t.context.options, lastRelease, nextRelease, logger: t.context.logger});

// Verify file2 and file1 have been commited
// file4.js is excluded as no glob matching
Expand Down Expand Up @@ -151,7 +151,7 @@ test.serial('Commit files matching the patterns in "assets" as Objects', async t
await outputFile('dir2/file6.js', 'Test content');
await outputFile('dir2/file7.css', 'Test content');

await publish(pluginConfig, t.context.options, lastRelease, nextRelease, t.context.logger);
await publish(pluginConfig, {options: t.context.options, lastRelease, nextRelease, logger: t.context.logger});

// Verify file2 and file1 have been commited
// file4.js is excluded as no glob matching
Expand All @@ -170,7 +170,7 @@ test.serial('Commit files matching the patterns in "assets" as single glob', asy
await outputFile('dist/file1.js', 'Test content');
await outputFile('dist/file2.css', 'Test content');

await publish(pluginConfig, t.context.options, lastRelease, nextRelease, t.context.logger);
await publish(pluginConfig, {options: t.context.options, lastRelease, nextRelease, logger: t.context.logger});

t.deepEqual(await gitCommitedFiles(), ['dist/file1.js']);

Expand All @@ -184,7 +184,7 @@ test.serial('Commit files matching the patterns in "assets", including dot files

await outputFile('dist/.dotfile', 'Test content');

await publish(pluginConfig, t.context.options, lastRelease, nextRelease, t.context.logger);
await publish(pluginConfig, {options: t.context.options, lastRelease, nextRelease, logger: t.context.logger});

t.deepEqual(await gitCommitedFiles(), ['dist/.dotfile']);

Expand All @@ -198,7 +198,7 @@ test.serial('Skip negated pattern if its alone in its group', async t => {

await outputFile('file.js', 'Test content');

await publish(pluginConfig, t.context.options, lastRelease, nextRelease, t.context.logger);
await publish(pluginConfig, {options: t.context.options, lastRelease, nextRelease, logger: t.context.logger});

t.deepEqual(await gitCommitedFiles(), ['file.js']);

Expand All @@ -210,7 +210,7 @@ test.serial('Skip commit if there is no files to commit', async t => {
const lastRelease = {};
const nextRelease = {version: '2.0.0', gitTag: 'v2.0.0', notes: 'Test release note'};

await publish(pluginConfig, t.context.options, lastRelease, nextRelease, t.context.logger);
await publish(pluginConfig, {options: t.context.options, lastRelease, nextRelease, logger: t.context.logger});

// Verify the files that have been commited
t.deepEqual(await gitCommitedFiles(), []);
Expand All @@ -226,7 +226,7 @@ test.serial('Skip commit if all the modified files are in .gitignore', async t =
await outputFile('dist/files1.js', 'Test content');
await outputFile('.gitignore', 'dist/**/*');

await publish(pluginConfig, t.context.options, lastRelease, nextRelease, t.context.logger);
await publish(pluginConfig, {options: t.context.options, lastRelease, nextRelease, logger: t.context.logger});

// Verify the files that have been commited
t.deepEqual(await gitCommitedFiles(), []);
Expand Down

0 comments on commit 375292e

Please sign in to comment.