Skip to content

Commit

Permalink
fix(diff): added prepare script to update the version number in the cli
Browse files Browse the repository at this point in the history
  • Loading branch information
GarthDB committed Oct 9, 2024
1 parent 1f153f0 commit c957785
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 16 deletions.
3 changes: 2 additions & 1 deletion tools/diff-generator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
16 changes: 2 additions & 14 deletions tools/diff-generator/src/lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,20 @@ 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();

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")
Expand Down
2 changes: 1 addition & 1 deletion tools/diff-generator/test/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
27 changes: 27 additions & 0 deletions tools/diff-generator/update-version.js
Original file line number Diff line number Diff line change
@@ -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}"`,
);

0 comments on commit c957785

Please sign in to comment.