Skip to content

Commit

Permalink
chore: add separate script for creating new versions (#222)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni authored May 18, 2022
1 parent f32fc99 commit 69cb903
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
51 changes: 51 additions & 0 deletions scripts/add-new-version.js
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 69cb903

Please sign in to comment.