Skip to content

Commit 53ba8a7

Browse files
committed
feat(install): cleanup install directory on error
1 parent 0f4fb4c commit 53ba8a7

File tree

2 files changed

+40
-24
lines changed

2 files changed

+40
-24
lines changed

lib/commands/install.js

+35-24
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const fs = require('fs-extra');
12
const path = require('path');
23
const Command = require('../command');
34
const symlinkSync = require('symlink-or-copy').sync;
@@ -49,31 +50,36 @@ class InstallCommand extends Command {
4950
local
5051
});
5152

52-
await this.ui.listr([{
53-
title: 'Checking for latest Ghost version',
54-
task: this.version
55-
}, {
56-
title: 'Setting up install directory',
57-
task: ensureStructure
58-
}, {
59-
title: 'Downloading and installing Ghost',
60-
task: (ctx, task) => {
61-
task.title = `Downloading and installing Ghost v${ctx.version}`;
62-
return yarnInstall(ctx.ui, ctx.argv.zip);
63-
}
64-
}, {
65-
title: 'Finishing install process',
66-
task: () => this.ui.listr([{
67-
title: 'Linking latest Ghost and recording versions',
68-
task: this.link.bind(this)
53+
try {
54+
await this.ui.listr([{
55+
title: 'Checking for latest Ghost version',
56+
task: this.version
6957
}, {
70-
title: 'Linking latest Casper',
71-
task: this.casper
72-
}], false)
73-
}], {
74-
argv: {...argv, version},
75-
cliVersion: this.system.cliVersion
76-
});
58+
title: 'Setting up install directory',
59+
task: ensureStructure
60+
}, {
61+
title: 'Downloading and installing Ghost',
62+
task: (ctx, task) => {
63+
task.title = `Downloading and installing Ghost v${ctx.version}`;
64+
return yarnInstall(ctx.ui, ctx.argv.zip);
65+
}
66+
}, {
67+
title: 'Finishing install process',
68+
task: () => this.ui.listr([{
69+
title: 'Linking latest Ghost and recording versions',
70+
task: this.link.bind(this)
71+
}, {
72+
title: 'Linking latest Casper',
73+
task: this.casper
74+
}], false)
75+
}], {
76+
argv: {...argv, version},
77+
cliVersion: this.system.cliVersion
78+
});
79+
} catch (error) {
80+
this.cleanInstallDirectory();
81+
throw error;
82+
}
7783

7884
if (!argv.setup) {
7985
return;
@@ -140,6 +146,11 @@ class InstallCommand extends Command {
140146
instance.cliVersion = this.system.cliVersion;
141147
instance.channel = ctx.argv.channel || 'stable';
142148
}
149+
150+
cleanInstallDirectory() {
151+
const cwd = process.cwd();
152+
fs.readdirSync(cwd).map(file => fs.removeSync(file));
153+
}
143154
}
144155

145156
InstallCommand.global = true;

test/unit/commands/install-spec.js

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const sinon = require('sinon');
33
const proxyquire = require('proxyquire').noCallThru();
44
const Promise = require('bluebird');
55
const path = require('path');
6+
const fs = require('fs-extra');
67

78
const modulePath = '../../../lib/commands/install';
89
const errors = require('../../../lib/errors');
@@ -34,6 +35,10 @@ describe('Unit: Commands > Install', function () {
3435
sinon.restore();
3536
});
3637

38+
beforeEach(() => {
39+
sinon.stub(fs, 'removeSync');
40+
});
41+
3742
it('rejects if directory is not empty', function () {
3843
const dirEmptyStub = sinon.stub().returns(false);
3944

0 commit comments

Comments
 (0)