diff --git a/docs/DeveloperNotes.md b/docs/DeveloperNotes.md index b8239b65..a8d0af5e 100644 --- a/docs/DeveloperNotes.md +++ b/docs/DeveloperNotes.md @@ -25,7 +25,7 @@ typescript-json-schema "lib/notes/interfaces/ExistingNote.ts" ExistingNote --req ## Publishing a release -1. Update the version number in `package.json` and in `frontent/app/config.tsx` +1. Run `node tools/updateVersion.js {major,minor,patch}` 2. Commit this with the commit message `release: vX.Y.Z` (replace X.Y.Z with actual version number) 3. Add a tag to this commit: `git tag vX.Y.Z` 4. Push commit to remote (if required via PR) diff --git a/tools/updateVersion.js b/tools/updateVersion.js new file mode 100755 index 00000000..83fa8257 --- /dev/null +++ b/tools/updateVersion.js @@ -0,0 +1,90 @@ +import { readFile, writeFile } from "fs/promises"; +import * as path from "path"; +import * as url from "url"; + +const __dirname = path.dirname(url.fileURLToPath(import.meta.url)); + +const increaseVersion = (version, versionType) => { + if (versionType === "major") { + return [ + version[0] + 1, + 0, + 0, + ]; + } else if (versionType === "minor") { + return [ + version[0], + version[1] + 1, + 0, + ]; + } else if (versionType === "patch") { + return [ + version[0], + version[1], + version[2] + 1, + ]; + } else { + throw new Error("Unexpected version type: " + versionType); + } +}; + +const parseVersion = (versionString) => { + return versionString.matchAll(/(\d+).(\d+).(\d+)/g) + .next().value.slice(1) + .map((v) => parseInt(v)); +}; + +const serializeVersion = ([major, minor, patch]) => { + return `${major}.${minor}.${patch}`; +}; + + +const replaceVersionInFile = async ( + filepath, + oldVersionString, + newVersionString, +) => { + const fileContent = await readFile(filepath, { encoding: "utf8" }); + const newFileContent = fileContent.replace( + oldVersionString, + newVersionString, + ); + await writeFile(filepath, newFileContent); +}; + + +const updateConfigFile = async (oldVersionString, newVersionString) => { + const configFilePath = path.join( + __dirname, "..", "frontend", "app", "config.tsx", + ); + + await replaceVersionInFile( + configFilePath, oldVersionString, newVersionString, + ); +}; + + +const updatePackageJson = async (oldVersionString, newVersionString) => { + const filepath = path.join(__dirname, "..", "package.json"); + await replaceVersionInFile(filepath, oldVersionString, newVersionString); +}; + + +const readCurrentVersion = async () => { + const filepath = path.join(__dirname, "..", "package.json"); + const fileContent = await readFile(filepath, { encoding: "utf8" }); + return JSON.parse(fileContent).version; +}; + + +const currentVersionString = await readCurrentVersion(); +console.log("Current version: " + currentVersionString); +const currentVersion = parseVersion(currentVersionString); +const versionType = process.argv[2]; +const newVersion = increaseVersion(currentVersion, versionType); +const newVersionString = serializeVersion(newVersion); +console.log("New version: " + newVersionString); + +await updatePackageJson(currentVersionString, newVersionString); +await updateConfigFile(currentVersionString, newVersionString); +