Skip to content

Commit e4b7934

Browse files
committed
refactor: move getErrorMessage to shared paths.mjs for consistency
Signed-off-by: leocavalcante <leo@cavalcante.dev>
1 parent b68037c commit e4b7934

3 files changed

Lines changed: 57 additions & 46 deletions

File tree

postinstall.mjs

Lines changed: 7 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -8,55 +8,18 @@
88
*/
99

1010
import { copyFileSync, existsSync, mkdirSync, readdirSync } from "node:fs"
11-
import { dirname, join } from "node:path"
11+
import { join } from "node:path"
1212

13-
import { AGENTS_TARGET_DIR, getAgentsSourceDir, getPackageRoot } from "./src/paths.mjs"
13+
import {
14+
AGENTS_TARGET_DIR,
15+
getAgentsSourceDir,
16+
getErrorMessage,
17+
getPackageRoot,
18+
} from "./src/paths.mjs"
1419

1520
const packageRoot = getPackageRoot(import.meta.url)
1621
const AGENTS_SOURCE_DIR = getAgentsSourceDir(packageRoot)
1722

18-
/**
19-
* Returns a user-friendly error message based on the error code.
20-
*
21-
* Translates Node.js filesystem error codes into human-readable messages
22-
* that help users understand and resolve installation issues.
23-
*
24-
* @param {Error & {code?: string}} error - The error object from a failed fs operation
25-
* @param {string} file - The filename being copied
26-
* @param {string} targetPath - The target path for the file
27-
* @returns {string} A helpful error message describing the issue and potential solution
28-
*
29-
* @example
30-
* // Permission denied error
31-
* const err = Object.assign(new Error(), { code: 'EACCES' })
32-
* getErrorMessage(err, 'agent.md', '/home/user/.config/opencode/agents/agent.md')
33-
* // Returns: "Permission denied. Check write permissions for /home/user/.config/opencode/agents"
34-
*
35-
* @example
36-
* // File not found error
37-
* const err = Object.assign(new Error(), { code: 'ENOENT' })
38-
* getErrorMessage(err, 'missing.md', '/target/missing.md')
39-
* // Returns: "Source file not found: missing.md"
40-
*/
41-
function getErrorMessage(error, file, targetPath) {
42-
const code = error.code
43-
switch (code) {
44-
case "EACCES":
45-
return `Permission denied. Check write permissions for ${dirname(targetPath)}`
46-
case "ENOSPC":
47-
return "Disk full. Free up space and try again"
48-
case "ENOENT":
49-
return `Source file not found: ${file}`
50-
case "EROFS":
51-
return "Read-only file system. Cannot write to target directory"
52-
case "EMFILE":
53-
case "ENFILE":
54-
return "Too many open files. Close some applications and try again"
55-
default:
56-
return error.message || "Unknown error"
57-
}
58-
}
59-
6023
/**
6124
* Main entry point for the postinstall script.
6225
*

preuninstall.mjs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@
1010
import { existsSync, readdirSync, unlinkSync } from "node:fs"
1111
import { join } from "node:path"
1212

13-
import { AGENTS_TARGET_DIR, getAgentsSourceDir, getPackageRoot } from "./src/paths.mjs"
13+
import {
14+
AGENTS_TARGET_DIR,
15+
getAgentsSourceDir,
16+
getErrorMessage,
17+
getPackageRoot,
18+
} from "./src/paths.mjs"
1419

1520
const packageRoot = getPackageRoot(import.meta.url)
1621
const AGENTS_SOURCE_DIR = getAgentsSourceDir(packageRoot)
@@ -66,7 +71,8 @@ function main() {
6671
console.log(` Removed: ${file}`)
6772
removedCount++
6873
} catch (err) {
69-
const message = err instanceof Error ? err.message : String(err)
74+
const error = err instanceof Error ? err : new Error(String(err))
75+
const message = getErrorMessage(error, file, targetPath)
7076
console.error(` Warning: Could not remove ${file}: ${message}`)
7177
}
7278
}

src/paths.mjs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,45 @@ export function getAgentsSourceDir(packageRoot) {
3535
* Located at ~/.config/opencode/agents/
3636
*/
3737
export const AGENTS_TARGET_DIR = join(homedir(), ".config", "opencode", "agents")
38+
39+
/**
40+
* Returns a user-friendly error message based on the error code.
41+
*
42+
* Translates Node.js filesystem error codes into human-readable messages
43+
* that help users understand and resolve installation issues.
44+
*
45+
* @param {Error & {code?: string}} error - The error object from a failed fs operation
46+
* @param {string} file - The filename being processed
47+
* @param {string} targetPath - The target path for the file
48+
* @returns {string} A helpful error message describing the issue and potential solution
49+
*
50+
* @example
51+
* // Permission denied error
52+
* const err = Object.assign(new Error(), { code: 'EACCES' })
53+
* getErrorMessage(err, 'agent.md', '/home/user/.config/opencode/agents/agent.md')
54+
* // Returns: "Permission denied. Check write permissions for /home/user/.config/opencode/agents"
55+
*
56+
* @example
57+
* // File not found error
58+
* const err = Object.assign(new Error(), { code: 'ENOENT' })
59+
* getErrorMessage(err, 'missing.md', '/target/missing.md')
60+
* // Returns: "Source file not found: missing.md"
61+
*/
62+
export function getErrorMessage(error, file, targetPath) {
63+
const code = error.code
64+
switch (code) {
65+
case "EACCES":
66+
return `Permission denied. Check write permissions for ${dirname(targetPath)}`
67+
case "ENOSPC":
68+
return "Disk full. Free up space and try again"
69+
case "ENOENT":
70+
return `Source file not found: ${file}`
71+
case "EROFS":
72+
return "Read-only file system. Cannot write to target directory"
73+
case "EMFILE":
74+
case "ENFILE":
75+
return "Too many open files. Close some applications and try again"
76+
default:
77+
return error.message || "Unknown error"
78+
}
79+
}

0 commit comments

Comments
 (0)