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

feat(update): reduce number of kept versions to 2 #1238

Merged
merged 1 commit into from
Jun 28, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
22 changes: 11 additions & 11 deletions lib/commands/update.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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