Skip to content

Commit ea1d79d

Browse files
committed
refactor: extract OPENCODE_VERSION to shared paths.mjs module
Signed-off-by: leocavalcante <leo@cavalcante.dev>
1 parent e103278 commit ea1d79d

File tree

4 files changed

+68
-6
lines changed

4 files changed

+68
-6
lines changed

postinstall.mjs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,13 @@ import {
1717
getAgentsSourceDir,
1818
getErrorMessage,
1919
getPackageRoot,
20+
OPENCODE_VERSION,
2021
parseCliFlags,
2122
parseFrontmatter,
2223
retryOnTransientError,
2324
validateAgentContent,
2425
} from "./src/paths.mjs"
2526

26-
/**
27-
* Mock OpenCode version for compatibility checking.
28-
* In production, this would be obtained from the OpenCode CLI or environment.
29-
*/
30-
const OPENCODE_VERSION = "0.1.0"
31-
3227
const packageRoot = getPackageRoot(import.meta.url)
3328
const AGENTS_SOURCE_DIR = getAgentsSourceDir(packageRoot)
3429

src/paths.d.mts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,24 @@ export {
1010
parseVersion,
1111
} from "./semver.mjs"
1212

13+
/**
14+
* Mock OpenCode version for compatibility checking.
15+
*
16+
* This version string is used to check agent compatibility requirements
17+
* during installation. In production, this would ideally be obtained
18+
* from the OpenCode CLI or environment, but for now it serves as a
19+
* fallback/default version.
20+
*
21+
* Format: Semantic versioning (MAJOR.MINOR.PATCH)
22+
*
23+
* @example
24+
* import { OPENCODE_VERSION, checkVersionCompatibility } from "./paths.mjs"
25+
*
26+
* // Check if an agent requiring ">=0.1.0" is compatible
27+
* const isCompatible = checkVersionCompatibility(">=0.1.0", OPENCODE_VERSION)
28+
*/
29+
export declare const OPENCODE_VERSION: string
30+
1331
/**
1432
* List of expected agent names (without .md extension).
1533
* This is the single source of truth for agent filenames.

src/paths.mjs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,24 @@ export {
1616
parseVersion,
1717
} from "./semver.mjs"
1818

19+
/**
20+
* Mock OpenCode version for compatibility checking.
21+
*
22+
* This version string is used to check agent compatibility requirements
23+
* during installation. In production, this would ideally be obtained
24+
* from the OpenCode CLI or environment, but for now it serves as a
25+
* fallback/default version.
26+
*
27+
* Format: Semantic versioning (MAJOR.MINOR.PATCH)
28+
*
29+
* @example
30+
* import { OPENCODE_VERSION, checkVersionCompatibility } from "./paths.mjs"
31+
*
32+
* // Check if an agent requiring ">=0.1.0" is compatible
33+
* const isCompatible = checkVersionCompatibility(">=0.1.0", OPENCODE_VERSION)
34+
*/
35+
export const OPENCODE_VERSION = "0.1.0"
36+
1937
/**
2038
* List of expected agent names (without .md extension).
2139
* This is the single source of truth for agent filenames.

tests/paths.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
getPackageRoot,
1212
isTransientError,
1313
MIN_CONTENT_LENGTH,
14+
OPENCODE_VERSION,
1415
parseCliFlags,
1516
parseFrontmatter,
1617
REQUIRED_FRONTMATTER_FIELDS,
@@ -417,6 +418,36 @@ describe("paths.mjs exports", () => {
417418
expect(AGENT_NAMES.length).toBe(originalLength)
418419
})
419420

421+
describe("OPENCODE_VERSION", () => {
422+
it("should be a string in semantic version format (MAJOR.MINOR.PATCH)", () => {
423+
expect(typeof OPENCODE_VERSION).toBe("string")
424+
// Semantic version format: X.Y.Z where X, Y, Z are non-negative integers
425+
const semverRegex = /^\d+\.\d+\.\d+$/
426+
expect(OPENCODE_VERSION).toMatch(semverRegex)
427+
})
428+
429+
it("should have the expected default value", () => {
430+
expect(OPENCODE_VERSION).toBe("0.1.0")
431+
})
432+
433+
it("should be usable with checkVersionCompatibility", () => {
434+
// Verify it works with the version compatibility checker
435+
expect(checkVersionCompatibility(">=0.1.0", OPENCODE_VERSION)).toBe(true)
436+
expect(checkVersionCompatibility("0.1.0", OPENCODE_VERSION)).toBe(true)
437+
expect(checkVersionCompatibility(">=0.2.0", OPENCODE_VERSION)).toBe(false)
438+
})
439+
440+
it("should consist of three numeric parts separated by dots", () => {
441+
const parts = OPENCODE_VERSION.split(".")
442+
expect(parts).toHaveLength(3)
443+
for (const part of parts) {
444+
const num = Number.parseInt(part, 10)
445+
expect(Number.isNaN(num)).toBe(false)
446+
expect(num).toBeGreaterThanOrEqual(0)
447+
}
448+
})
449+
})
450+
420451
it("should export MIN_CONTENT_LENGTH as a number", () => {
421452
expect(typeof MIN_CONTENT_LENGTH).toBe("number")
422453
expect(MIN_CONTENT_LENGTH).toBe(100)

0 commit comments

Comments
 (0)