2022-05-08 20:44:40 +00:00
|
|
|
|
|
|
|
const MyVC = require('./myvc');
|
2022-12-21 12:34:17 +00:00
|
|
|
const Command = require('./lib/command');
|
2022-05-08 20:44:40 +00:00
|
|
|
const fs = require('fs-extra');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cleans old applied versions.
|
|
|
|
*/
|
2022-12-21 12:34:17 +00:00
|
|
|
class Clean extends Command {
|
|
|
|
static usage = {
|
|
|
|
description: 'Cleans old applied versions'
|
|
|
|
};
|
|
|
|
|
|
|
|
static localOpts = {
|
|
|
|
default: {
|
|
|
|
remote: 'production'
|
|
|
|
}
|
|
|
|
};
|
2022-05-08 20:44:40 +00:00
|
|
|
|
|
|
|
async run(myvc, opts) {
|
|
|
|
await myvc.dbConnect();
|
|
|
|
const version = await myvc.fetchDbVersion() || {};
|
|
|
|
const number = version.number;
|
|
|
|
|
|
|
|
const oldVersions = [];
|
2022-12-21 12:34:17 +00:00
|
|
|
const versionDirs = await fs.readdir(opts.versionsDir);
|
2022-05-08 20:44:40 +00:00
|
|
|
for (const versionDir of versionDirs) {
|
|
|
|
const dirVersion = myvc.parseVersionDir(versionDir);
|
|
|
|
if (!dirVersion) continue;
|
|
|
|
|
|
|
|
if (parseInt(dirVersion.number) < parseInt(number))
|
|
|
|
oldVersions.push(versionDir);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opts.maxOldVersions
|
|
|
|
&& oldVersions.length > opts.maxOldVersions) {
|
|
|
|
oldVersions.splice(-opts.maxOldVersions);
|
|
|
|
|
|
|
|
for (const oldVersion of oldVersions)
|
2022-12-21 12:34:17 +00:00
|
|
|
await fs.remove(`${opts.versionsDir}/${oldVersion}`,
|
2022-05-08 20:44:40 +00:00
|
|
|
{recursive: true});
|
|
|
|
|
|
|
|
console.log(`Old versions deleted: ${oldVersions.length}`);
|
|
|
|
} else
|
|
|
|
console.log(`No versions to delete.`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Clean;
|
|
|
|
|
|
|
|
if (require.main === module)
|
|
|
|
new MyVC().run(Clean);
|