Skip to content

Commit

Permalink
ci: create update version script
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Zimmer authored and Sebastian Zimmer committed Sep 21, 2022
1 parent 55cbe3a commit 51adf97
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/DeveloperNotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
90 changes: 90 additions & 0 deletions tools/updateVersion.js
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit 51adf97

Please sign in to comment.