#3989 Added clean command

This commit is contained in:
Juan Ferrer 2022-05-08 22:44:40 +02:00
parent 726e979544
commit 8c3badaa75
7 changed files with 72 additions and 32 deletions

View File

@ -190,11 +190,16 @@ Creates a new version folder, when name is not specified it generates a random
name mixing a color with a plant name.
```text
$ myvc version [<name>] [-o|--hold]
$ myvc version [<name>]
```
By default it also cleans all already applied versions older than
*maxOldVersions*, you can provide de *--hold* option to prevent this behavior.
### clean
Cleans all already applied versions older than *maxOldVersions*.
```text
$ myvc clean
```
## Local server commands

56
myvc-clean.js Normal file
View File

@ -0,0 +1,56 @@
const MyVC = require('./myvc');
const fs = require('fs-extra');
/**
* Cleans old applied versions.
*/
class Clean {
get usage() {
return {
description: 'Cleans old applied versions'
};
}
get localOpts() {
return {
default: {
remote: 'production'
}
};
}
async run(myvc, opts) {
await myvc.dbConnect();
const version = await myvc.fetchDbVersion() || {};
const number = version.number;
const verionsDir =`${opts.myvcDir}/versions`;
const oldVersions = [];
const versionDirs = await fs.readdir(verionsDir);
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)
await fs.remove(`${verionsDir}/${oldVersion}`,
{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);

View File

@ -75,7 +75,7 @@ class Dump {
await dumpStream.end();
const version = await myvc.fetchDbVersion();
if (version){
if (version) {
await fs.writeFile(
`${dumpDir}/.dump.json`,
JSON.stringify(version)

View File

@ -10,8 +10,7 @@ class Version {
return {
description: 'Creates a new version',
params: {
name: 'Name for the new version',
hold: 'Do not clean old versions'
name: 'Name for the new version'
},
operand: 'name'
};
@ -20,15 +19,11 @@ class Version {
get localOpts() {
return {
alias: {
name: 'n',
hold: 'o'
name: 'n'
},
string: [
'name'
],
boolean: [
'hold'
],
default: {
remote: 'production'
}
@ -38,7 +33,6 @@ class Version {
async run(myvc, opts) {
let newVersionDir;
const verionsDir =`${opts.myvcDir}/versions`;
const oldVersions = [];
// Fetch last version number
@ -88,9 +82,6 @@ class Version {
const dirVersion = myvc.parseVersionDir(versionDir);
if (!dirVersion) continue;
versionNames.add(dirVersion.name);
if (parseInt(dirVersion.number) < parseInt(number))
oldVersions.push(versionDir);
}
if (!versionName) {
@ -140,19 +131,6 @@ class Version {
await fs.remove(newVersionDir, {recursive: true});
throw err;
}
// Remove old versions
if (opts.maxOldVersions && !opts.hold
&& oldVersions.length > opts.maxOldVersions) {
oldVersions.splice(-opts.maxOldVersions);
for (const oldVersion of oldVersions)
await fs.remove(`${verionsDir}/${oldVersion}`,
{recursive: true});
console.log(`Old versions deleted: ${oldVersions.length}`);
}
}
}

View File

@ -60,6 +60,7 @@ class MyVC {
'pull',
'push',
'version',
'clean',
'dump',
'start',
'run'
@ -125,11 +126,11 @@ class MyVC {
depVersion[1] === myVersion[1] &&
depVersion[2] === myVersion[2];
if (!isSameVersion)
throw new Error(`MyVC version differs a lot from package.json, please run 'npm i' first to install the proper version`);
throw new Error(`MyVC version differs a lot from package.json, please run 'npm i' first to install the proper version.`);
const isSameMinor = depVersion[3] === myVersion[3];
if (!isSameMinor)
console.warn(`Warning! MyVC minor version differs from package.json, maybe you shoud run 'npm i' to install the proper version`.yellow);
console.warn(`Warning! MyVC minor version differs from package.json, maybe you shoud run 'npm i' to install the proper version.`.yellow);
}
// Load method

View File

@ -1,6 +1,6 @@
{
"name": "myvc",
"version": "1.4.6",
"version": "1.4.7",
"author": "Verdnatura Levante SL",
"description": "MySQL Version Control",
"license": "GPL-3.0",

View File

@ -8,6 +8,6 @@
"type": "git"
},
"dependencies": {
"myvc": "^1.4.6"
"myvc": "^1.4.7"
}
}