From 69cb9038d6b686a719dc659b7dd804ef5e617361 Mon Sep 17 00:00:00 2001 From: Jonas Lagoni Date: Wed, 18 May 2022 15:22:23 +0200 Subject: [PATCH] chore: add separate script for creating new versions (#222) --- package.json | 2 +- scripts/add-new-version.js | 51 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 scripts/add-new-version.js diff --git a/package.json b/package.json index 564d621a..b1c70593 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "generate:assets": "npm run build", "prepublishOnly": "npm run build", "bundle": "cd tools/bundler && npm i && npm run bundle", - "startNewVersion": "newVersion=$npm_config_new_version && latestVersion=$(ls -d ./definitions/* | sort -V -r | head -1 | xargs -n 1 basename) && [ -d ./definitions/${newVersion} ] && echo Directory ./definitions/$newVersion already exist and cannot be overwritten. Please create a different version. || (cp -R ./definitions/$latestVersion ./definitions/$newVersion && find ./definitions/$newVersion -name '*.json' -exec sed -i '' \"s+definitions/$latestVersion+definitions/$newVersion+g\" {} +)", + "startNewVersion": "newVersion=$npm_config_new_version node scripts/add-new-version.js", "lint": "echo 'No linter integrated yet'", "bump:version": "npm --no-git-tag-version --allow-same-version version $VERSION" }, diff --git a/scripts/add-new-version.js b/scripts/add-new-version.js new file mode 100644 index 00000000..8d2f2f49 --- /dev/null +++ b/scripts/add-new-version.js @@ -0,0 +1,51 @@ +/** + * This script adds a new version of the spec, by copying the latest one as baseline. + */ +const exec = require('child_process').exec; +const fs = require('fs'); +const inputNewVersion = process.env.newVersion; +//Regex taken from https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string +const versionRegex = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/g; //NOSONAR + +/** + * Promise based function to execute commands + * + * @param {string} command to execute + * @returns + */ +function execute(command) { + return new Promise((resolve, reject) => { + exec(command, function (error, stdout, stderr) { //NOSONAR + if (!error) resolve(stdout); + console.error(stderr); + reject(error); + }); + }); +} + +async function addNewVersion(newVersion) { + const newVersionDir = `./definitions/${newVersion}`; + + try { + fs.accessSync(newVersionDir); + console.error(`Directory ${newVersionDir} already exist and cannot be overwritten. Please create a different version.`) + return process.exit(1); + } catch (err) { } + + //Use the newest version as baseline for the new one + const latestVersion = (await execute('ls -d ./definitions/* | sort -V -r | head -1 | xargs -n 1 basename')).trim(); + await execute(`cp -R ./definitions/${latestVersion} ${newVersionDir}`); + + // Replace old version numbers with new + await execute(`find ${newVersionDir} -name '*.json' -exec sed -i '' \"s+${latestVersion}+${newVersion}+g\" {} +`); + + console.log(`New version added to ${newVersionDir}`) +} + +const versionMatch = inputNewVersion.match(versionRegex); +if (!versionMatch) { + console.error(`The new version ${inputNewVersion} must use semver versioning. `) + process.exit(1); +} else { + addNewVersion(inputNewVersion); +}