From 8c3badaa7561e779080c7960026c151d1260199d Mon Sep 17 00:00:00 2001 From: Juan Ferrer Toribio Date: Sun, 8 May 2022 22:44:40 +0200 Subject: [PATCH] #3989 Added clean command --- README.md | 11 ++++++--- myvc-clean.js | 56 +++++++++++++++++++++++++++++++++++++++++++ myvc-dump.js | 2 +- myvc-version.js | 26 ++------------------ myvc.js | 5 ++-- package.json | 2 +- template/package.json | 2 +- 7 files changed, 72 insertions(+), 32 deletions(-) create mode 100644 myvc-clean.js diff --git a/README.md b/README.md index 55ad4d7..d5dcc64 100644 --- a/README.md +++ b/README.md @@ -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 [] [-o|--hold] +$ myvc version [] ``` -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 diff --git a/myvc-clean.js b/myvc-clean.js new file mode 100644 index 0000000..7da86b0 --- /dev/null +++ b/myvc-clean.js @@ -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); diff --git a/myvc-dump.js b/myvc-dump.js index 61a3bcf..8b51c9e 100644 --- a/myvc-dump.js +++ b/myvc-dump.js @@ -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) diff --git a/myvc-version.js b/myvc-version.js index c1e38d0..2606443 100644 --- a/myvc-version.js +++ b/myvc-version.js @@ -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}`); - } } } diff --git a/myvc.js b/myvc.js index 7ab7575..7bf8b3e 100755 --- a/myvc.js +++ b/myvc.js @@ -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 diff --git a/package.json b/package.json index 381081f..b06c9c0 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/template/package.json b/template/package.json index c0e4c20..7931ba4 100644 --- a/template/package.json +++ b/template/package.json @@ -8,6 +8,6 @@ "type": "git" }, "dependencies": { - "myvc": "^1.4.6" + "myvc": "^1.4.7" } }