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

Rollback Git operations when publish fails and on termination #334

Merged
merged 15 commits into from
Mar 31, 2019
Merged
Show file tree
Hide file tree
Changes from 6 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 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"meow": "^5.0.0",
"new-github-release-url": "^0.1.0",
"npm-name": "^5.0.1",
"onetime": "^3.0.0",
"opn": "^5.4.0",
"ow": "^0.10.0",
"p-memoize": "^2.1.0",
Expand Down
5 changes: 0 additions & 5 deletions source/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,6 @@ const cli = meow(`

updateNotifier({pkg: cli.pkg}).notify();

process.on('SIGINT', () => {
console.log('\nAborted!');
process.exit(1);
});

(async () => {
const pkg = util.readPkg();

Expand Down
10 changes: 10 additions & 0 deletions source/git-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,13 @@ exports.verifyTagDoesNotExistOnRemote = async tagName => {
exports.commitLogFromRevision = revision => execa.stdout('git', ['log', '--format=%s %h', `${revision}..HEAD`]);

exports.push = () => execa('git', ['push', '--follow-tags']);

exports.getLastCommit = () => execa.stdout('git', ['log', '--max-count=1', '--pretty=%B']);

exports.deleteTag = async tagName => {
await execa('git', ['tag', '--delete', tagName]);
};

exports.removeLastCommit = async () => {
await execa('git', ['reset', '--hard', 'HEAD~1']);
};
47 changes: 45 additions & 2 deletions source/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const readPkgUp = require('read-pkg-up');
const hasYarn = require('has-yarn');
const pkgDir = require('pkg-dir');
const hostedGitInfo = require('hosted-git-info');
const onetime = require('onetime');
const prerequisiteTasks = require('./prerequisite-tasks');
const gitTasks = require('./git-tasks');
const publishTaskHelper = require('./publish-task-helper');
Expand Down Expand Up @@ -57,6 +58,32 @@ module.exports = async (input = 'patch', options) => {
const hasLockFile = fs.existsSync(path.resolve(rootDir, 'package-lock.json')) || fs.existsSync(path.resolve(rootDir, 'npm-shrinkwrap.json'));
const isOnGitHub = options.repoUrl && hostedGitInfo.fromUrl(options.repoUrl).type === 'github';

let isPublished = false;

const rollback = onetime(async () => {
itaisteinherz marked this conversation as resolved.
Show resolved Hide resolved
console.log('\nPublish failed. Rolling back to the previous state…');

const lastCommit = await git.getLastCommit();
try {
if (lastCommit === util.readPkg().version && lastCommit !== pkg.version) { // Verify that the package's version has been bumped before deleting the last tag and commit.
await git.deleteTag(options.version);
await git.removeLastCommit();
}

console.log('Successfully rolled back the project to its previous state.');
} catch (error) {
console.log(`Couldn't rollback because of the following error:\n${error}`);
}
});

process.on('SIGINT', async () => {
itaisteinherz marked this conversation as resolved.
Show resolved Hide resolved
if (!isPublished && runPublish) {
await rollback();
}

process.exit(0);
});

const tasks = new Listr([
{
title: 'Prerequisite check',
Expand Down Expand Up @@ -147,7 +174,19 @@ module.exports = async (input = 'patch', options) => {
return `Private package: not publishing to ${pkgManagerName}.`;
}
},
task: (context, task) => publishTaskHelper(pkgManager, task, options, input)
task: (context, task) => {
try {
return publishTaskHelper(pkgManager, task, options, input)
.pipe(
catchError(rollback)
)
.subscribe(() => {
itaisteinherz marked this conversation as resolved.
Show resolved Hide resolved
isPublished = true;
});
} catch (_) {
return rollback();
}
}
}
]);
}
Expand All @@ -156,7 +195,11 @@ module.exports = async (input = 'patch', options) => {
title: 'Pushing tags',
skip: async () => {
if (!(await git.hasUpstream())) {
return 'Upstream branch not found: not pushing.';
return 'Upstream branch not found; not pushing.';
}

if (!isPublished && runPublish) {
return 'Couldn\'t publish package to npm; not pushing.';
}
},
task: () => git.push()
Expand Down