Skip to content

Commit 9ac58fe

Browse files
committed
refactor: extract shared path constants to src/paths.mjs
Signed-off-by: leocavalcante <[email protected]>
1 parent e29bb3a commit 9ac58fe

File tree

3 files changed

+44
-13
lines changed

3 files changed

+44
-13
lines changed

postinstall.mjs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,12 @@
88
*/
99

1010
import { copyFileSync, existsSync, mkdirSync, readdirSync } from "node:fs"
11-
import { homedir } from "node:os"
1211
import { dirname, join } from "node:path"
13-
import { fileURLToPath } from "node:url"
1412

15-
const __filename = fileURLToPath(import.meta.url)
16-
const __dirname = dirname(__filename)
13+
import { AGENTS_TARGET_DIR, getAgentsSourceDir, getPackageRoot } from "./src/paths.mjs"
1714

18-
const AGENTS_SOURCE_DIR = join(__dirname, "agents")
19-
const AGENTS_TARGET_DIR = join(homedir(), ".config", "opencode", "agents")
15+
const packageRoot = getPackageRoot(import.meta.url)
16+
const AGENTS_SOURCE_DIR = getAgentsSourceDir(packageRoot)
2017

2118
/**
2219
* Returns a user-friendly error message based on the error code

preuninstall.mjs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,12 @@
88
*/
99

1010
import { existsSync, readdirSync, unlinkSync } from "node:fs"
11-
import { homedir } from "node:os"
12-
import { dirname, join } from "node:path"
13-
import { fileURLToPath } from "node:url"
11+
import { join } from "node:path"
1412

15-
const __filename = fileURLToPath(import.meta.url)
16-
const __dirname = dirname(__filename)
13+
import { AGENTS_TARGET_DIR, getAgentsSourceDir, getPackageRoot } from "./src/paths.mjs"
1714

18-
const AGENTS_SOURCE_DIR = join(__dirname, "agents")
19-
const AGENTS_TARGET_DIR = join(homedir(), ".config", "opencode", "agents")
15+
const packageRoot = getPackageRoot(import.meta.url)
16+
const AGENTS_SOURCE_DIR = getAgentsSourceDir(packageRoot)
2017

2118
function main() {
2219
console.log("opencode-plugin-opencoder: Removing agents...")

src/paths.mjs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Shared path constants for agent installation/uninstallation scripts.
3+
*
4+
* This module provides the common directory paths used by postinstall.mjs
5+
* and preuninstall.mjs to locate agent files.
6+
*/
7+
8+
import { homedir } from "node:os"
9+
import { dirname, join } from "node:path"
10+
import { fileURLToPath } from "node:url"
11+
12+
/**
13+
* Get the package root directory from a module's import.meta.url
14+
* @param {string} importMetaUrl - The import.meta.url of the calling module
15+
* @returns {string} The package root directory path
16+
*/
17+
export function getPackageRoot(importMetaUrl) {
18+
const __filename = fileURLToPath(importMetaUrl)
19+
const __dirname = dirname(__filename)
20+
// Both postinstall.mjs and preuninstall.mjs are in the package root
21+
return __dirname
22+
}
23+
24+
/**
25+
* Get the source directory containing agent markdown files.
26+
* @param {string} packageRoot - The package root directory
27+
* @returns {string} Path to the agents source directory
28+
*/
29+
export function getAgentsSourceDir(packageRoot) {
30+
return join(packageRoot, "agents")
31+
}
32+
33+
/**
34+
* The target directory where agents are installed.
35+
* Located at ~/.config/opencode/agents/
36+
*/
37+
export const AGENTS_TARGET_DIR = join(homedir(), ".config", "opencode", "agents")

0 commit comments

Comments
 (0)