Skip to content

Commit

Permalink
feat(update): reduce number of kept versions to 2 (#1238)
Browse files Browse the repository at this point in the history
refs #201
  • Loading branch information
acburdine committed Jun 28, 2020
1 parent 6281eaa commit badb662
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 21 deletions.
22 changes: 11 additions & 11 deletions lib/commands/update.js
Expand Up @@ -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) {
Expand Down
17 changes: 7 additions & 10 deletions test/unit/commands/update-spec.js
Expand Up @@ -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) => {
Expand All @@ -825,20 +822,20 @@ 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'
];
const removedVersions = [
'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;
});
Expand Down

0 comments on commit badb662

Please sign in to comment.