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

refactor: change order for publishing #6

Merged
merged 1 commit into from
Jan 30, 2018
Merged
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
89 changes: 66 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,83 @@
#!/usr/bin/env node
require('shelljs/global');

// require npm user
// bump package version
// commit
// create tag
// push commit & tag
// publish
var path = require('path');

// npm version (bump version, commit, make tag)
// echo npm publish
// - if successful, push commit & tag
// - if failed, undo commit & tag

function usage() {
echo("");
echo(" Usage: node " + process.argv[1] + " <major|minor|patch>");
echo('');
echo(' Usage: node ' + process.argv[1] + ' <major|minor|patch>');
}

config.silent = true;
function run(version) {
var npm_user = exec('npm whoami').trimRight();
var is_collaborator = exec('npm access ls-collaborators').grep('.*' + npm_user + '.*:.*write.*').trimRight();
var is_owner = exec('npm owner ls').grep('.*' + npm_user + ' <.*').trimRight();

if (npm_user) {
config.silent = false;
if (is_collaborator || is_owner) {
echo('Publishing new ' + version + ' version as ' + npm_user + '.');
config.silent = false;
config.fatal = true;
try {
// TODO(nfischer): only allow releases from master branch (issue #4)

Choose a reason for hiding this comment

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

This can be checked easily:

if (exec('git rev-parse --abbrev-ref HEAD').trim() !== 'master') {
    echo('');
    echo('Releases are permitted only from the master branch.');
    exit(1);
}

Copy link
Member Author

Choose a reason for hiding this comment

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

Let's keep it in a separate PR

echo('Publishing new ' + version + ' version');
echo('');
exec('npm version ' + version);
} catch (e) {
echo('');
echo('Unable to bump version, is your repo clean?');
exit(1);
}

try {
exec('npm publish');
} catch (e) {
config.fatal = false;
echo('');
echo('Unable to publish, restoring previous repo state');

// Clean up
var newVersion = require(path.resolve('.', 'package.json')).version;
var tagName = 'v' + newVersion;

// Delete the tag and undo the commit
echo('Removing git tag...');
exec('git tag -d ' + tagName);
echo('Removing git commit...');
exec('git reset --hard HEAD~1');
var npm_user = exec('npm whoami', { silent: true }).trimRight();
if (npm_user.toString() === '') {
config.silent = false;
echo('');
exec('npm version ' + version);
exec('git push');
exec('git push --follow-tags');
exec('npm publish');
} else {
echo('You must be logged in to NPM to publish, run "npm login" first.');
exit(1);
}
config.silent = true;
var is_collaborator = exec('npm access ls-collaborators')
.grep('.*' + npm_user + '.*:.*write.*')
.trimRight();
var is_owner = exec('npm owner ls').grep('.*' + npm_user + ' <.*')
.trimRight();
if (is_collaborator + is_owner === '') {
// Neither collaborator nor owner
config.silent = false;
echo(npm_user + ' does not have NPM write access. Request access from one of these fine folk:');
echo('');
exec('npm owner ls');
exit(1);
}
} else {
echo('You must be logged in to NPM to publish, run "npm login" first.');

echo('Unknown error: ' + e);
}

config.silent = false;
try {
// TODO(nfischer): this currently requires upstream tracking (issue #2)
exec('git push');
exec('git push --tags');
} catch (e) {
echo('');
echo('Version has been released, but commit/tag could not be pushed.');
echo('Please push these manually.');
}
}

Expand Down