From badb662c462084d2b2f8cd9b6e9f566c09d2e8f2 Mon Sep 17 00:00:00 2001 From: Austin Burdine Date: Sun, 28 Jun 2020 10:46:59 -0400 Subject: [PATCH] feat(update): reduce number of kept versions to 2 (#1238) refs https://github.com/TryGhost/Ghost-CLI/issues/201 --- lib/commands/update.js | 22 +++++++++++----------- test/unit/commands/update-spec.js | 17 +++++++---------- 2 files changed, 18 insertions(+), 21 deletions(-) diff --git a/lib/commands/update.js b/lib/commands/update.js index 8e9682388..88e65e91a 100644 --- a/lib/commands/update.js +++ b/lib/commands/update.js @@ -142,21 +142,21 @@ class UpdateCommand extends Command { return yarnInstall(ctx.ui, ctx.zip); } - removeOldVersions(ctx, task) { + async removeOldVersions({instance}, task) { const semver = require('semver'); - return fs.readdir(path.join(process.cwd(), 'versions')).then((versions) => { - versions = versions.filter(semver.valid).sort(semver.compare); - if (versions.length <= 5) { - task.skip(); - return; - } + const versionDirs = await fs.readdir(path.join(instance.dir, 'versions')); + const versions = versionDirs.filter(semver.valid).sort(semver.compare); - const promises = versions.slice(0, -5) - .map(version => fs.remove(path.join(process.cwd(), 'versions', version))); + if (versions.length <= 2) { + task.skip(); + return; + } - return Promise.all(promises); - }); + const promises = versions.slice(0, -2) + .map(version => fs.remove(path.join(instance.dir, 'versions', version))); + + await Promise.all(promises); } async version(context) { diff --git a/test/unit/commands/update-spec.js b/test/unit/commands/update-spec.js index 1df182606..6fb3915db 100644 --- a/test/unit/commands/update-spec.js +++ b/test/unit/commands/update-spec.js @@ -785,20 +785,17 @@ describe('Unit: Commands > Update', function () { }); describe('removeOldVersions', function () { - it('skips if there are 5 or fewer versions installed', async function () { + it('skips if there are 2 or fewer versions installed', async function () { const dirs = [ - 'versions/1.4.0', - 'versions/1.5.0', 'versions/1.5.1', 'versions/1.5.2' ]; const env = setupTestFolder({dirs: dirs}); const UpdateCommand = require(modulePath); const instance = new UpdateCommand({}, {}); - sinon.stub(process, 'cwd').returns(env.dir); const skipStub = sinon.stub(); - await instance.removeOldVersions({}, {skip: skipStub}); + await instance.removeOldVersions({instance: {dir: env.dir}}, {skip: skipStub}); expect(skipStub.calledOnce).to.be.true; dirs.forEach((version) => { @@ -825,9 +822,6 @@ describe('Unit: Commands > Update', function () { const instance = new UpdateCommand({}, {}); sinon.stub(process, 'cwd').returns(env.dir); const keptVersions = [ - '1.1.0', - '1.2.0', - '1.3.0', '1.4.0', '1.5.0' ]; @@ -835,10 +829,13 @@ describe('Unit: Commands > Update', function () { '1.0.0-beta.2', '1.0.0-RC.1', '1.0.0', - '1.0.2' + '1.0.2', + '1.1.0', + '1.2.0', + '1.3.0' ]; - await instance.removeOldVersions(); + await instance.removeOldVersions({instance: {dir: env.dir}}); keptVersions.forEach((version) => { expect(fs.existsSync(path.join(env.dir, 'versions', version))).to.be.true; });