Skip to content

Commit

Permalink
fix: compile zwave-js version into source (#7344)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlCalzone authored Oct 30, 2024
1 parent e13a1de commit 787fca3
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test-and-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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)'
# ===================

Expand Down
2 changes: 1 addition & 1 deletion maintenance/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
19 changes: 19 additions & 0 deletions packages/zwave-js/maintenance/codegen.ts
Original file line number Diff line number Diff line change
@@ -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");
1 change: 1 addition & 0 deletions packages/zwave-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions packages/zwave-js/src/lib/_version.ts
Original file line number Diff line number Diff line change
@@ -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";
16 changes: 7 additions & 9 deletions packages/zwave-js/src/lib/driver/Driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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 = `
Expand Down Expand Up @@ -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",
},
Expand Down Expand Up @@ -904,7 +900,9 @@ export class Driver extends TypedEventEmitter<DriverEventCallbacks>
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
);
}
Expand Down

0 comments on commit 787fca3

Please sign in to comment.