Skip to content

Commit

Permalink
Add support for a custom publishing command, fix postpublish script (#54
Browse files Browse the repository at this point in the history
)

* Add support for a custom publishing command, fix postpublish script

* Import Promise from pinkie
  • Loading branch information
AndreyBelym authored and inikulin committed Mar 28, 2018
1 parent d496045 commit 6e5bea1
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -58,6 +58,7 @@ npm run publish-please

- **prePublishScript** - Specifies command that will be run before publish (e.g. `npm test`). Use it for builds and tests. Default: `npm test`.
- **postPublishScript** - Specifies command that will be run after successful publishing. Use it for release announcements, creating a GitHub release, uploading binaries, etc. Default: `` (no command).
- **publishCommand** - Specifies publishing command which will be used to publish the package. Default: `npm publish`.
- **publishTag** - Specifies tag with which package will be published. See [npm publish docs](https://docs.npmjs.com/cli/publish) for more info. Default: `latest`.
- **confirm** - Ask for the confirmation before publishing. Default: `true`.

Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "publish-please",
"version": "2.3.1",
"version": "2.4.0",
"description": "Safe and highly functional replacement for `npm publish`.",
"main": "./lib/index.js",
"bin": {
Expand Down
5 changes: 5 additions & 0 deletions src/config.js
Expand Up @@ -28,6 +28,11 @@ const optionsConfigurators = {
currentVal
),

publishCommand: currentVal => input(
'Specify publishing command which will be used to publish your package',
currentVal
),

publishTag: currentVal => input(
'Specify release tag with which you package will be published',
currentVal
Expand Down
1 change: 1 addition & 0 deletions src/default-options.js
Expand Up @@ -2,6 +2,7 @@ module.exports = {
validations: require('./validations').DEFAULT_OPTIONS,

confirm: true,
publishCommand: 'npm publish',
publishTag: 'latest',
prePublishScript: 'npm test',
postPublishScript: ''
Expand Down
14 changes: 8 additions & 6 deletions src/publish.js
@@ -1,6 +1,7 @@
'use strict';

const readFile = require('fs').readFileSync;
const Promise = require('pinkie-promise');
const chalk = require('chalk');
const semver = require('semver');
const defaults = require('lodash/defaultsDeep');
Expand Down Expand Up @@ -54,13 +55,14 @@ function runScript (command, scriptType) {
});
}

function publish (publishTag) {
const command = `npm publish --tag ${publishTag} --with-publish-please`;
function publish (publishCommand, publishTag) {
const command = `${publishCommand} --tag ${publishTag} --with-publish-please`;

if (!module.exports.testMode)
return spawn(command).then(() => console.log('\n', emoji.tada, emoji.tada, emoji.tada));
const spawnPromise = module.exports.testMode ?
Promise.resolve() :
spawn(command).then(() => console.log('\n', emoji.tada, emoji.tada, emoji.tada));

return command;
return spawnPromise.then(() => command);
}

function getOptions (opts) {
Expand Down Expand Up @@ -114,7 +116,7 @@ module.exports = function (opts) {
.then(() => validate(opts.validations, pkgInfo))
.then(() => !module.exports.testMode && printReleaseInfo(pkgInfo.cfg.version, opts.publishTag))
.then(() => opts.confirm ? confirm('Are you sure you want to publish this version to npm?', false) : true)
.then(ok => ok && publish(opts.publishTag) || '')
.then(ok => ok && publish(opts.publishCommand, opts.publishTag) || '')
.then(command => {
if (!command || !opts.postPublishScript)
return command;
Expand Down
13 changes: 13 additions & 0 deletions test/test.js
Expand Up @@ -118,6 +118,7 @@ describe('.publishrc', () => {
assert(!opts.confirm);
assert.strictEqual(opts.prePublishScript, 'npm test');
assert.strictEqual(opts.postPublishScript, '');
assert.strictEqual(opts.publishCommand, 'npm publish');
assert.strictEqual(opts.publishTag, 'latest');
assert.strictEqual(opts.validations.branch, 'master');
assert(!opts.validations.uncommittedChanges);
Expand Down Expand Up @@ -379,6 +380,18 @@ describe('Postpublish script', () => {

});

describe('Custom publish command', () => {
it('Should execute a custom publish command if it is specified', () =>
exec('git checkout master')
.then(() => publish(getTestOptions({ set: { publishCommand: 'gulp publish' }, remove: 'publishTag' })))
.then(npmCmd => assert.strictEqual(npmCmd, 'gulp publish --tag latest --with-publish-please')));

it('Should execute a custom publish command with a custom tag', () =>
exec('git checkout master')
.then(() => publish(getTestOptions({ set: { publishCommand: 'gulp publish', publishTag: 'alpha' } })))
.then(npmCmd => assert.strictEqual(npmCmd, 'gulp publish --tag alpha --with-publish-please')));
});

describe('Publish tag', () => {
it('Should publish with the given tag', () =>
exec('git checkout master')
Expand Down

0 comments on commit 6e5bea1

Please sign in to comment.