From c95778540a7fdb93477dde585a18aa32dd245a50 Mon Sep 17 00:00:00 2001 From: Garth Braithwaite Date: Wed, 9 Oct 2024 17:05:27 -0600 Subject: [PATCH] fix(diff): added prepare script to update the version number in the cli --- tools/diff-generator/package.json | 3 ++- tools/diff-generator/src/lib/cli.js | 16 ++------------- tools/diff-generator/test/cli.test.js | 2 +- tools/diff-generator/update-version.js | 27 ++++++++++++++++++++++++++ 4 files changed, 32 insertions(+), 16 deletions(-) create mode 100644 tools/diff-generator/update-version.js diff --git a/tools/diff-generator/package.json b/tools/diff-generator/package.json index 067fda72..465026f6 100644 --- a/tools/diff-generator/package.json +++ b/tools/diff-generator/package.json @@ -4,7 +4,8 @@ "description": "Creates diffs for tokens", "type": "module", "scripts": { - "test": "c8 ava" + "test": "c8 ava", + "prepare": "node update-version.js" }, "repository": { "type": "git", diff --git a/tools/diff-generator/src/lib/cli.js b/tools/diff-generator/src/lib/cli.js index 14d379b0..84a5e6fa 100755 --- a/tools/diff-generator/src/lib/cli.js +++ b/tools/diff-generator/src/lib/cli.js @@ -20,19 +20,13 @@ import * as emoji from "node-emoji"; import { Command } from "commander"; -import { readFileSync } from "fs"; - const yellow = chalk.hex("F3EE7E"); const red = chalk.hex("F37E7E"); const green = chalk.hex("7EF383"); const white = chalk.white; -// Read the content of package.json -const packageJsonContent = readFileSync("./package.json", "utf8"); -// Parse the JSON data -const packageInfo = JSON.parse(packageJsonContent); -// Access the version property -const version = packageInfo.version; +/* this is updated by the npm prepare script using update-version.js */ +const version = "1.1.1"; const program = new Command(); @@ -40,12 +34,6 @@ program .name("tdiff") .description("CLI to a Spectrum token diff generator") .version(version); -program - .command("version") - .description("Returns the current package version number for tdiff") - .action(() => { - console.log(version); - }); program .command("report") .description("Generates a diff report for two inputted schema") diff --git a/tools/diff-generator/test/cli.test.js b/tools/diff-generator/test/cli.test.js index dc4d72fb..2ebbf302 100644 --- a/tools/diff-generator/test/cli.test.js +++ b/tools/diff-generator/test/cli.test.js @@ -28,7 +28,7 @@ test("cli should return correct version number", async (t) => { .expect((result) => { t.true(result.stdout.includes(packageJSON.version)); }) - .run("./src/lib/cli.js version") + .run("./src/lib/cli.js --version") .end(resolve); } catch (error) { reject(error); diff --git a/tools/diff-generator/update-version.js b/tools/diff-generator/update-version.js new file mode 100644 index 00000000..ff3a659a --- /dev/null +++ b/tools/diff-generator/update-version.js @@ -0,0 +1,27 @@ +import { readFile, writeFile } from "fs/promises"; +import { resolve, relative } from "path"; +import * as url from "url"; + +export const __dirname = url.fileURLToPath(new URL(".", import.meta.url)); + +const readJson = async (fileName) => + JSON.parse(await readFile(fileName, "utf8")); + +const cliFilePath = resolve(__dirname, "src", "lib", "cli.js"); + +const [packageFile, cliFile] = await Promise.all([ + readJson(resolve(__dirname, "package.json")), + readFile(cliFilePath, "utf8"), +]); + +await writeFile( + cliFilePath, + cliFile.replace( + /const version = \"([^\"]*)\";/, + `const version = "${packageFile.version}";`, + ), +); + +console.log( + `Version updated in CLI file ${relative(__dirname, cliFilePath)} to "${packageFile.version}"`, +);