Skip to content

Commit

Permalink
Add ability to publish a subdirectory (#263)
Browse files Browse the repository at this point in the history
  • Loading branch information
willmendesneto authored and sindresorhus committed May 26, 2018
1 parent 3f1c66e commit 334a18d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
5 changes: 5 additions & 0 deletions cli.js
Expand Up @@ -22,12 +22,14 @@ const cli = meow(`
--no-publish Skips publishing
--tag Publish under a given dist-tag
--no-yarn Don't use Yarn
--contents Subdirectory to publish
Examples
$ np
$ np patch
$ np 1.0.2
$ np 1.0.2-beta.3 --tag=beta
$ np 1.0.2-beta.3 --tag=beta --contents=dist
`, {
flags: {
anyBranch: {
Expand All @@ -50,6 +52,9 @@ const cli = meow(`
yarn: {
type: 'boolean',
default: hasYarn()
},
contents: {
type: 'string'
}
}
});
Expand Down
10 changes: 8 additions & 2 deletions index.js
Expand Up @@ -127,7 +127,13 @@ module.exports = (input, opts) => {
}
},
task: () => {
const args = ['publish', '--new-version', input];
const args = ['publish'];

if (opts.contents) {
args.push(opts.contents);
}

args.push('--new-version', input);

if (opts.tag) {
args.push('--tag', opts.tag);
Expand All @@ -144,7 +150,7 @@ module.exports = (input, opts) => {
return 'Private package: not publishing to npm.';
}
},
task: (ctx, task) => publish(task, opts.tag)
task: (ctx, task) => publish(task, opts)
}
]);
}
Expand Down
14 changes: 9 additions & 5 deletions lib/publish.js
Expand Up @@ -7,6 +7,10 @@ const chalk = require('chalk');
const npmPublish = options => {
const args = ['publish'];

if (options.contents) {
args.push(options.contents);
}

if (options.tag) {
args.push('--tag', options.tag);
}
Expand All @@ -18,23 +22,23 @@ const npmPublish = options => {
return execa('npm', args);
};

const handleError = (task, err, tag, message) => {
const handleError = (task, err, options, message) => {
if (err.stderr.includes('one-time pass')) {
const {title} = task;
task.title = `${title} ${chalk.yellow('(waiting for input…)')}`;

return listrInput(message || 'Enter OTP:', {
done: otp => {
task.title = title;
return npmPublish({tag, otp});
return npmPublish(Object.assign({otp}, options));
}
}).catch(err => handleError(task, err, tag, 'OTP was incorrect, try again:'));
}).catch(err => handleError(task, err, options, 'OTP was incorrect, try again:'));
}

return Observable.throw(err);
};

const publish = (task, tag) => Observable.fromPromise(npmPublish({tag}))
.catch(err => handleError(task, err, tag));
const publish = (task, options) => Observable.fromPromise(npmPublish(options))
.catch(err => handleError(task, err, options));

module.exports = publish;
2 changes: 2 additions & 0 deletions readme.md
Expand Up @@ -46,12 +46,14 @@ $ np --help
--no-publish Skips publishing
--tag Publish under a given dist-tag
--no-yarn Don't use Yarn
--contents Subdirectory to publish
Examples
$ np
$ np patch
$ np 1.0.2
$ np 1.0.2-beta.3 --tag=beta
$ np 1.0.2-beta.3 --tag=beta --contents=dist
```


Expand Down

0 comments on commit 334a18d

Please sign in to comment.