-
Notifications
You must be signed in to change notification settings - Fork 321
refactor: extract server version helper #545
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import fs from "node:fs"; | ||
| import path, { dirname } from "node:path"; | ||
| import { fileURLToPath } from "node:url"; | ||
|
|
||
| const __filename = fileURLToPath(import.meta.url); | ||
| const __dirname = dirname(__filename); | ||
|
|
||
| const packageJsonPaths = [ | ||
| // Built file: build/server/version.js -> ../../package.json | ||
| path.resolve(__dirname, "../../package.json"), | ||
| // Source file under tsx/node:test: server/version.ts -> ../package.json | ||
| path.resolve(__dirname, "../package.json"), | ||
| ]; | ||
|
|
||
| export const SERVER_VERSION: string = (() => { | ||
| for (const packageJsonPath of packageJsonPaths) { | ||
| try { | ||
| if (fs.existsSync(packageJsonPath)) { | ||
| const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8")); | ||
| return packageJson.version || "unknown"; | ||
| } | ||
| } catch { | ||
| // Intentionally ignored: version read failure is non-critical. | ||
| } | ||
| } | ||
| return "unknown"; | ||
| })(); | ||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,10 @@ | ||||||||
| import { test } from "node:test"; | ||||||||
| import assert from "node:assert"; | ||||||||
| import fs from "node:fs"; | ||||||||
| import { SERVER_VERSION } from "../../server/version.js"; | ||||||||
|
|
||||||||
| const packageVersion = JSON.parse(fs.readFileSync("package.json", "utf8")).version; | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make the fixture path independent of process CWD. At Line 6, reading Suggested fix-const packageVersion = JSON.parse(fs.readFileSync("package.json", "utf8")).version;
+const packageJsonUrl = new URL("../../package.json", import.meta.url);
+const packageVersion = JSON.parse(fs.readFileSync(packageJsonUrl, "utf8")).version;📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||
|
|
||||||||
| test("SERVER_VERSION matches package.json", () => { | ||||||||
| assert.strictEqual(SERVER_VERSION, packageVersion); | ||||||||
| }); | ||||||||
|
cursor[bot] marked this conversation as resolved.
|
||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Potentially resolves the wrong
package.jsonin source runs.At Line 10,
../../package.jsonis tried before the source-local candidate. When running fromserver/version.ts, this can pick a parent workspacepackage.jsonand report an incorrectSERVER_VERSION.Suggested fix
📝 Committable suggestion
🤖 Prompt for AI Agents