Skip to content

Commit e385365

Browse files
committed
fix(postinstall): add robust error handling with specific error messages
Signed-off-by: leocavalcante <leo@cavalcante.dev>
1 parent c1b51f8 commit e385365

File tree

1 file changed

+61
-6
lines changed

1 file changed

+61
-6
lines changed

postinstall.mjs

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,32 @@ const __dirname = dirname(__filename)
1818
const AGENTS_SOURCE_DIR = join(__dirname, "agents")
1919
const AGENTS_TARGET_DIR = join(homedir(), ".config", "opencode", "agents")
2020

21+
/**
22+
* Returns a user-friendly error message based on the error code
23+
* @param {Error & {code?: string}} error - The error object
24+
* @param {string} file - The filename being copied
25+
* @param {string} targetPath - The target path for the file
26+
* @returns {string} A helpful error message
27+
*/
28+
function getErrorMessage(error, file, targetPath) {
29+
const code = error.code
30+
switch (code) {
31+
case "EACCES":
32+
return `Permission denied. Check write permissions for ${dirname(targetPath)}`
33+
case "ENOSPC":
34+
return "Disk full. Free up space and try again"
35+
case "ENOENT":
36+
return `Source file not found: ${file}`
37+
case "EROFS":
38+
return "Read-only file system. Cannot write to target directory"
39+
case "EMFILE":
40+
case "ENFILE":
41+
return "Too many open files. Close some applications and try again"
42+
default:
43+
return error.message || "Unknown error"
44+
}
45+
}
46+
2147
function main() {
2248
console.log("opencode-plugin-opencoder: Installing agents...")
2349

@@ -41,18 +67,47 @@ function main() {
4167
process.exit(1)
4268
}
4369

70+
const successes = []
71+
const failures = []
72+
4473
for (const file of files) {
4574
const sourcePath = join(AGENTS_SOURCE_DIR, file)
4675
const targetPath = join(AGENTS_TARGET_DIR, file)
4776

48-
copyFileSync(sourcePath, targetPath)
49-
console.log(` Installed: ${file}`)
77+
try {
78+
copyFileSync(sourcePath, targetPath)
79+
successes.push(file)
80+
console.log(` Installed: ${file}`)
81+
} catch (error) {
82+
const message = getErrorMessage(error, file, targetPath)
83+
failures.push({ file, message })
84+
console.error(` Failed: ${file} - ${message}`)
85+
}
5086
}
5187

52-
console.log(`\nopencode-plugin-opencoder: Successfully installed ${files.length} agent(s)`)
53-
console.log(` Location: ${AGENTS_TARGET_DIR}`)
54-
console.log("\nTo use the autonomous development loop, run:")
55-
console.log(" opencode @opencoder")
88+
// Print summary
89+
console.log("")
90+
if (successes.length > 0 && failures.length === 0) {
91+
console.log(`opencode-plugin-opencoder: Successfully installed ${successes.length} agent(s)`)
92+
console.log(` Location: ${AGENTS_TARGET_DIR}`)
93+
console.log("\nTo use the autonomous development loop, run:")
94+
console.log(" opencode @opencoder")
95+
} else if (successes.length > 0 && failures.length > 0) {
96+
console.log(
97+
`opencode-plugin-opencoder: Installed ${successes.length} of ${files.length} agent(s)`,
98+
)
99+
console.log(` Location: ${AGENTS_TARGET_DIR}`)
100+
console.error(`\n ${failures.length} file(s) failed to install:`)
101+
for (const { file, message } of failures) {
102+
console.error(` - ${file}: ${message}`)
103+
}
104+
} else {
105+
console.error("opencode-plugin-opencoder: Failed to install any agents")
106+
for (const { file, message } of failures) {
107+
console.error(` - ${file}: ${message}`)
108+
}
109+
process.exit(1)
110+
}
56111
}
57112

58113
main()

0 commit comments

Comments
 (0)