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
12 changes: 6 additions & 6 deletions source/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ 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 published = false;
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', '-1', '--pretty=%B']);
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', '-d', options.version]);
await execa('git', ['tag', '--delete', options.version]);
await execa('git', ['reset', '--hard', 'HEAD~1']);
}

Expand All @@ -77,7 +77,7 @@ module.exports = async (input = 'patch', options) => {
});

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

Expand Down Expand Up @@ -177,7 +177,7 @@ module.exports = async (input = 'patch', options) => {
task: async (context, task) => {
try {
await publishTaskHelper(pkgManager, task, options, input);
itaisteinherz marked this conversation as resolved.
Show resolved Hide resolved
published = true;
isPublished = true;
} catch (_) {
rollback();
itaisteinherz marked this conversation as resolved.
Show resolved Hide resolved
}
Expand All @@ -193,7 +193,7 @@ module.exports = async (input = 'patch', options) => {
return 'Upstream branch not found; not pushing.';
}

if (!published && runPublish) {
if (!isPublished && runPublish) {
return 'Couldn\'t publish package to npm; not pushing.';
}
},
Expand Down