Skip to content

Commit

Permalink
Add support for specifying release branch (#558)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <[email protected]>
  • Loading branch information
MunifTanjim and sindresorhus authored Jul 24, 2020
1 parent 6be4223 commit b8b1976
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 deletions.
4 changes: 3 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
## Why

- [Interactive UI](#interactive-ui)
- Ensures you are publishing from the `master` branch
- Ensures you are publishing from your release branch (`main` and `master` by default)
- Ensures the working directory is clean and that there are no unpulled changes
- Reinstalls dependencies to ensure your project works with the latest dependency tree
- Ensures your Node.js and npm versions are supported by the project and its dependencies
Expand Down Expand Up @@ -51,6 +51,7 @@ $ np --help
Options
--any-branch Allow publishing from any branch
--branch Name of the release branch (default: master)
--no-cleanup Skips cleanup of node_modules
--no-tests Skips tests
--yolo Skips cleanup and testing
Expand Down Expand Up @@ -82,6 +83,7 @@ Run `np` without arguments to launch the interactive UI that guides you through
Currently, these are the flags you can configure:

- `anyBranch` - Allow publishing from any branch (`false` by default).
- `branch` - Name of the release branch (`master` by default).
- `cleanup` - Cleanup `node_modules` (`true` by default).
- `tests` - Run `npm test` (`true` by default).
- `yolo` - Skip cleanup and testing (`false` by default).
Expand Down
4 changes: 4 additions & 0 deletions source/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const cli = meow(`
Options
--any-branch Allow publishing from any branch
--branch Name of the release branch (default: master)
--no-cleanup Skips cleanup of node_modules
--no-tests Skips tests
--yolo Skips cleanup and testing
Expand All @@ -44,6 +45,9 @@ const cli = meow(`
anyBranch: {
type: 'boolean'
},
branch: {
type: 'string'
},
cleanup: {
type: 'boolean'
},
Expand Down
2 changes: 1 addition & 1 deletion source/git-tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = options => {
const tasks = [
{
title: 'Check current branch',
task: () => git.verifyCurrentBranchIsMaster()
task: () => git.verifyCurrentBranchIsReleaseBranch(options.branch)
},
{
title: 'Check local working tree',
Expand Down
8 changes: 5 additions & 3 deletions source/git-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ exports.currentBranch = async () => {
return stdout;
};

exports.verifyCurrentBranchIsMaster = async () => {
if (await exports.currentBranch() !== 'master') {
throw new Error('Not on `master` branch. Use --any-branch to publish anyway.');
exports.verifyCurrentBranchIsReleaseBranch = async releaseBranch => {
const allowedBranches = releaseBranch ? [releaseBranch] : ['main', 'master'];
const currentBranch = await exports.currentBranch();
if (!allowedBranches.includes(currentBranch)) {
throw new Error(`Not on ${allowedBranches.map(branch => `\`${branch}\``).join('/')} branch. Use --any-branch to publish anyway, or set a different release branch using --branch.`);
}
};

Expand Down
17 changes: 15 additions & 2 deletions test/git-tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ test.beforeEach(() => {
execaStub.resetStub();
});

test.serial('should fail when current branch not master and publishing from any branch not permitted', async t => {
test.serial('should fail when release branch is not specified, current branch is not main/master and publishing from any branch not permitted', async t => {
execaStub.createStub([
{
command: 'git symbolic-ref --short HEAD',
Expand All @@ -33,7 +33,20 @@ test.serial('should fail when current branch not master and publishing from any
}
]);
await t.throwsAsync(run(testedModule({})),
{message: 'Not on `master` branch. Use --any-branch to publish anyway.'});
{message: 'Not on `main`/`master` branch. Use --any-branch to publish anyway, or set a different release branch using --branch.'});
t.true(SilentRenderer.tasks.some(task => task.title === 'Check current branch' && task.hasFailed()));
});

test.serial('should fail when current branch is not the specified release branch and publishing from any branch not permitted', async t => {
execaStub.createStub([
{
command: 'git symbolic-ref --short HEAD',
exitCode: 0,
stdout: 'feature'
}
]);
await t.throwsAsync(run(testedModule({branch: 'release'})),
{message: 'Not on `release` branch. Use --any-branch to publish anyway, or set a different release branch using --branch.'});
t.true(SilentRenderer.tasks.some(task => task.title === 'Check current branch' && task.hasFailed()));
});

Expand Down

0 comments on commit b8b1976

Please sign in to comment.