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 3 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
42 changes: 40 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 execa.stdout('git', ['log', 'HEAD^..HEAD', '--pretty=%B']);
itaisteinherz marked this conversation as resolved.
Show resolved Hide resolved
try {
if (lastCommit === util.readPkg().version && lastCommit !== pkg.version) {
await execa('git', ['tag', '--delete', options.version]);
await execa('git', ['reset', '--hard', 'HEAD~1']);
}

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,14 @@ module.exports = async (input = 'patch', options) => {
return `Private package: not publishing to ${pkgManagerName}.`;
}
},
task: (context, task) => publishTaskHelper(pkgManager, task, options, input)
task: async (context, task) => {
try {
await publishTaskHelper(pkgManager, task, options, input);
itaisteinherz marked this conversation as resolved.
Show resolved Hide resolved
isPublished = true;
} catch (_) {
rollback();
itaisteinherz marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
]);
}
Expand All @@ -156,7 +190,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