From 787fca348dc72959ea09b93ec21b7f1eec41a39e Mon Sep 17 00:00:00 2001 From: AlCalzone Date: Wed, 30 Oct 2024 13:24:18 +0100 Subject: [PATCH] fix: compile `zwave-js` version into source (#7344) --- .github/workflows/test-and-release.yml | 2 ++ maintenance/build.ts | 2 +- packages/zwave-js/maintenance/codegen.ts | 19 +++++++++++++++++++ packages/zwave-js/package.json | 1 + packages/zwave-js/src/lib/_version.ts | 3 +++ packages/zwave-js/src/lib/driver/Driver.ts | 16 +++++++--------- 6 files changed, 33 insertions(+), 10 deletions(-) create mode 100644 packages/zwave-js/maintenance/codegen.ts create mode 100644 packages/zwave-js/src/lib/_version.ts diff --git a/.github/workflows/test-and-release.yml b/.github/workflows/test-and-release.yml index 74b09e7a3809..9372f178cf1a 100644 --- a/.github/workflows/test-and-release.yml +++ b/.github/workflows/test-and-release.yml @@ -115,6 +115,8 @@ jobs: echo "Testing version exports..." node -e 'assert.equal(require("@zwave-js/config").PACKAGE_VERSION, require("@zwave-js/config/package.json").version)' + node -e 'assert.equal(require("zwave-js").libVersion, require("zwave-js/package.json").version)' + node -e 'assert.equal(require("zwave-js").libName, require("zwave-js/package.json").name)' # =================== diff --git a/maintenance/build.ts b/maintenance/build.ts index cf06caf5a5fb..444b5fcb42d5 100644 --- a/maintenance/build.ts +++ b/maintenance/build.ts @@ -18,7 +18,7 @@ const needsNoCodegen = [ "@zwave-js/transformers", ]; -const hasCodegen = ["@zwave-js/cc", "@zwave-js/config"]; +const hasCodegen = ["@zwave-js/cc", "@zwave-js/config", "zwave-js"]; // zwave-js is the main entry point, but there are projects that depend on it const dependsOnZwaveJs = [ diff --git a/packages/zwave-js/maintenance/codegen.ts b/packages/zwave-js/maintenance/codegen.ts new file mode 100644 index 000000000000..f258145614ed --- /dev/null +++ b/packages/zwave-js/maintenance/codegen.ts @@ -0,0 +1,19 @@ +import fs from "node:fs"; +import path from "node:path"; + +// +// Generate PACKAGE_VERSION module +// + +const sourceDir = path.join(__dirname, "../src"); + +const { version: PACKAGE_VERSION, name: PACKAGE_NAME } = JSON.parse( + fs.readFileSync(path.join(__dirname, "../package.json"), "utf8"), +); +const versionFileName = path.join(sourceDir, "lib", "_version.ts"); +const code = `// This file is auto-generated by the codegen maintenance script +export const PACKAGE_VERSION = "${PACKAGE_VERSION}"; +export const PACKAGE_NAME = "${PACKAGE_NAME}"; +`; + +fs.writeFileSync(versionFileName, code, "utf8"); diff --git a/packages/zwave-js/package.json b/packages/zwave-js/package.json index 74dd5f50ddc9..79870eca0354 100644 --- a/packages/zwave-js/package.json +++ b/packages/zwave-js/package.json @@ -85,6 +85,7 @@ }, "scripts": { "ts": "node -r esbuild-register --conditions=@@dev", + "codegen": "yarn ts maintenance/codegen.ts", "build": "tsc -b tsconfig.build.json --pretty", "clean": "del-cli build/ \"*.tsbuildinfo\"", "extract-api": "yarn api-extractor run", diff --git a/packages/zwave-js/src/lib/_version.ts b/packages/zwave-js/src/lib/_version.ts new file mode 100644 index 000000000000..1faddcbe66e9 --- /dev/null +++ b/packages/zwave-js/src/lib/_version.ts @@ -0,0 +1,3 @@ +// This file is auto-generated by the codegen maintenance script +export const PACKAGE_VERSION = "14.0.0-beta.0"; +export const PACKAGE_NAME = "zwave-js"; diff --git a/packages/zwave-js/src/lib/driver/Driver.ts b/packages/zwave-js/src/lib/driver/Driver.ts index 9094a9b2692c..577d9f3c5106 100644 --- a/packages/zwave-js/src/lib/driver/Driver.ts +++ b/packages/zwave-js/src/lib/driver/Driver.ts @@ -222,6 +222,7 @@ import { containsSerializedCC, isCommandRequest, } from "@zwave-js/serial/serialapi"; +import { PACKAGE_NAME, PACKAGE_VERSION } from "../_version"; import { type ZWaveNodeBase } from "../node/mixins/00_Base"; import { type NodeWakeup } from "../node/mixins/30_Wakeup"; import { type NodeValues } from "../node/mixins/40_Values"; @@ -272,13 +273,8 @@ import type { } from "./ZWaveOptions"; import { discoverRemoteSerialPorts } from "./mDNSDiscovery"; -const packageJsonPath = require.resolve("zwave-js/package.json"); - -// eslint-disable-next-line @typescript-eslint/no-require-imports -const packageJson = require(packageJsonPath); -const libraryRootDir = path.dirname(packageJsonPath); -export const libVersion: string = packageJson.version; -export const libName: string = packageJson.name; +export const libVersion: string = PACKAGE_VERSION; +export const libName: string = PACKAGE_NAME; // This is made with cfonts: const libNameString = ` @@ -342,7 +338,7 @@ const defaultOptions: ZWaveOptions = { return fs.writeFile(file, data, options); }, }, - cacheDir: path.resolve(libraryRootDir, "cache"), + cacheDir: path.join(process.cwd(), "cache"), lockDir: process.env.ZWAVEJS_LOCK_DIRECTORY, throttle: "normal", }, @@ -904,7 +900,9 @@ export class Driver extends TypedEventEmitter public get configVersion(): string { return ( this.configManager?.configVersion - ?? packageJson?.dependencies?.["@zwave-js/config"] + // eslint-disable-next-line @typescript-eslint/no-require-imports + ?? require("zwave-js/package.json")?.dependencies + ?.["@zwave-js/config"] ?? libVersion ); }