Skip to content

Commit

Permalink
Add support for specifying release branch
Browse files Browse the repository at this point in the history
  • Loading branch information
MunifTanjim committed Jul 20, 2020
1 parent 6be4223 commit 8ebbe07
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 9 deletions.
9 changes: 7 additions & 2 deletions 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 (`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 All @@ -95,6 +97,7 @@ Currently, these are the flags you can configure:
For example, this configures `np` to never use Yarn and to use `dist` as the subdirectory to publish:

`package.json`

```json
{
"name": "superb-package",
Expand All @@ -106,6 +109,7 @@ For example, this configures `np` to never use Yarn and to use `dist` as the sub
```

`.np-config.json`

```json
{
"yarn": false,
Expand All @@ -114,10 +118,11 @@ For example, this configures `np` to never use Yarn and to use `dist` as the sub
```

`.np-config.js`

```js
module.exports = {
yarn: false,
contents: 'dist'
contents: "dist"
};
```

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
6 changes: 3 additions & 3 deletions source/git-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ 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 = 'master') => {
if (await exports.currentBranch() !== releaseBranch) {
throw new Error(`Not on \`${releaseBranch}\` branch. Use --any-branch to publish anyway, or set a different release branch using --branch.`);
}
};

Expand Down
6 changes: 3 additions & 3 deletions test/git-tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ 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 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({})),
{message: 'Not on `master` branch. Use --any-branch to publish anyway.'});
await t.throwsAsync(run(testedModule({branch: 'main'})),
{message: 'Not on `main` 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 8ebbe07

Please sign in to comment.