Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --branch flag for specifying default branch #476

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ $ np --help

Options
--any-branch Allow publishing from any branch
--branch Branch name to set as default branch [Default: master]
--no-cleanup Skips cleanup of node_modules
--no-tests Skips tests
--yolo Skips cleanup and testing
Expand Down
5 changes: 5 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 Branch name to set as default branch [Default: master]
--no-cleanup Skips cleanup of node_modules
--no-tests Skips tests
--yolo Skips cleanup and testing
Expand All @@ -43,6 +44,10 @@ const cli = meow(`
anyBranch: {
type: 'boolean'
},
defaultBranch: {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
defaultBranch: {
branch: {

type: 'string',
default: 'master'
},
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.verifyCurrentBranchIsDefault(options.defaultBranch)
},
{
title: 'Check local working tree',
Expand Down
4 changes: 2 additions & 2 deletions source/git-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ exports.currentBranch = async () => {
return stdout;
};

exports.verifyCurrentBranchIsMaster = async () => {
if (await exports.currentBranch() !== 'master') {
exports.verifyCurrentBranchIsDefault = async (defaultBranch = 'master') => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to define a default value here, as it's set by meow (see this line).

if (await exports.currentBranch() !== defaultBranch) {
throw new Error('Not on `master` branch. Use --any-branch to publish anyway.');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
throw new Error('Not on `master` branch. Use --any-branch to publish anyway.');
throw new Error(`Not on \`{defaultBranch}\` branch. Use --any-branch to publish anyway, or define a different default branch using --branch.`);

}
};
Expand Down
31 changes: 31 additions & 0 deletions test/git-util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import test from 'ava';
import sinon from 'sinon';
import gitUtil from '../source/git-util';

const sandbox = sinon.createSandbox();
const currentBranchStub = sinon.stub(gitUtil, 'currentBranch');

test.afterEach(() => {
sandbox.restore();
});

test('verifyCurrentBranchIsDefault doesn\'t throw if current branch is master', async t => {
currentBranchStub.returns('master');
await t.notThrowsAsync(gitUtil.verifyCurrentBranchIsDefault);
});

test('verifyCurrentBranchIsDefault throws if current branch is not master', async t => {
currentBranchStub.returns('not master');
await t.throwsAsync(gitUtil.verifyCurrentBranchIsDefault);
});

test('verifyCurrentBranchIsDefault doesn\'t throw if current branch the specified default branch', async t => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
test('verifyCurrentBranchIsDefault doesn\'t throw if current branch the specified default branch', async t => {
test('verifyCurrentBranchIsDefault doesn\'t throw if current branch is the specified default branch', async t => {

const currentBranchName = 'bestBranch';
currentBranchStub.returns(currentBranchName);
await t.notThrowsAsync(gitUtil.verifyCurrentBranchIsDefault(currentBranchName));
});

test('verifyCurrentBranchIsDefault throws if current branch is not default branch', async t => {
currentBranchStub.returns('unicorn-wrangler');
await t.throwsAsync(gitUtil.verifyCurrentBranchIsDefault('unicorn'));
});