myt/myt-clean.js

150 lines
4.4 KiB
JavaScript
Raw Normal View History

2022-12-21 13:17:50 +00:00
const Myt = require('./myt');
const Command = require('./lib/command');
2022-05-08 20:44:40 +00:00
const fs = require('fs-extra');
const path = require('path');
2022-05-08 20:44:40 +00:00
/**
* Cleans old applied versions.
*/
class Clean extends Command {
static usage = {
description: 'Cleans old applied versions',
params: {
purge: 'Wether to remove non-existent scripts from DB log'
}
};
static opts = {
alias: {
purge: 'p'
},
boolean: [
'purge'
],
default: {
remote: 'production'
}
};
2022-05-08 20:44:40 +00:00
2022-12-21 13:17:50 +00:00
async run(myt, opts) {
const conn = await myt.dbConnect();
const archiveDir = path.join(opts.versionsDir, '.archive');
const dbVersion = await myt.fetchDbVersion() || {};
const number = parseInt(dbVersion.number);
2022-05-08 20:44:40 +00:00
const oldVersions = [];
const versionDirs = await fs.readdir(opts.versionsDir);
2022-05-08 20:44:40 +00:00
for (const versionDir of versionDirs) {
const version = await myt.loadVersion(versionDir);
const shouldArchive = version
&& !version.apply
&& parseInt(version.number) < number;
2022-05-08 20:44:40 +00:00
if (shouldArchive)
2022-05-08 20:44:40 +00:00
oldVersions.push(versionDir);
}
if (opts.maxOldVersions
&& oldVersions.length > opts.maxOldVersions) {
oldVersions.splice(-opts.maxOldVersions);
if (!await fs.pathExists(archiveDir))
await fs.mkdir(archiveDir);
for (const oldVersion of oldVersions) {
const srcDir = path.join(opts.versionsDir, oldVersion);
const dstDir = path.join(archiveDir, oldVersion);
if (!await fs.pathExists(dstDir))
await fs.mkdir(dstDir);
const scripts = await fs.readdir(srcDir);
for (const script of scripts) {
await fs.move(
path.join(srcDir, script),
path.join(dstDir, script),
{overwrite: true}
);
}
await fs.rmdir(srcDir);
}
2022-05-08 20:44:40 +00:00
console.log(` -> ${oldVersions.length} versions archived.`);
2022-05-08 20:44:40 +00:00
} else
console.log(` -> No versions archived.`);
if (opts.purge) {
const versionDb = new VersionDb(myt, opts.versionsDir);
versionDb.load();
const archiveDb = new VersionDb(myt, archiveDir);
archiveDb.load();
const [res] = await conn.query(
`SELECT number, file FROM versionLog
WHERE code = ?
ORDER BY number, file`,
[opts.code]
);
let nPurged = 0;
for (const script of res) {
const hasVersion = await versionDb.hasScript(script);
const hasArchive = await archiveDb.hasScript(script);
if (!hasVersion && !hasArchive) {
await conn.query(
`DELETE FROM versionLog
WHERE code = ? AND number = ? AND file = ?`,
[opts.code, script.number, script.file]
);
nPurged++;
}
}
if (nPurged)
console.log(` -> ${nPurged} versions purged from log.`);
else
console.log(` -> No versions purged from log.`);
}
}
}
class VersionDb {
constructor(myt, baseDir) {
Object.assign(this, {myt, baseDir});
}
async load() {
const map = this.map = new Map();
if (await fs.pathExists(this.baseDir)) {
const dirs = await fs.readdir(this.baseDir);
for (const dir of dirs) {
const version = this.myt.parseVersionDir(dir);
if (!version) continue;
let subdirs = map.get(version.number);
if (!subdirs) map.set(version.number, subdirs = []);
subdirs.push(dir);
}
}
return map;
}
async hasScript(script) {
const dirs = this.map.get(script.number);
if (dirs)
for (const dir of dirs) {
const scriptPath = path.join(this.baseDir, dir, script.file);
if (await fs.pathExists(scriptPath)) return true;
}
return false;
2022-05-08 20:44:40 +00:00
}
}
module.exports = Clean;
if (require.main === module)
2022-12-21 13:17:50 +00:00
new Myt().run(Clean);