This commit is contained in:
opencode-agent[bot] 2025-12-23 15:41:49 +08:00 committed by GitHub
commit 063031f4a0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 55 additions and 0 deletions

View file

@ -5,6 +5,29 @@ const fs = require("fs")
const path = require("path")
const os = require("os")
function cleanupTempDirectories() {
if (os.platform() !== "win32") return
try {
const globalNodeModules = path.join(process.env.APPDATA || "", "npm", "node_modules")
if (!fs.existsSync(globalNodeModules)) return
const entries = fs.readdirSync(globalNodeModules, { withFileTypes: true })
for (const entry of entries) {
if (entry.isDirectory() && entry.name.startsWith(".opencode-ai-")) {
const tempDir = path.join(globalNodeModules, entry.name)
try {
fs.rmSync(tempDir, { recursive: true, force: true })
} catch (cleanupError) {
// Silently ignore cleanup errors
}
}
}
} catch (error) {
// Silently ignore cleanup errors
}
}
function run(target) {
const result = childProcess.spawnSync(target, process.argv.slice(2), {
stdio: "inherit",
@ -19,6 +42,8 @@ function run(target) {
const envPath = process.env.OPENCODE_BIN_PATH
if (envPath) {
// Clean up temp directories before running
cleanupTempDirectories()
run(envPath)
}
@ -81,4 +106,7 @@ if (!resolved) {
process.exit(1)
}
// Clean up temp directories before running
cleanupTempDirectories()
run(resolved)

View file

@ -97,12 +97,39 @@ function symlinkBinary(sourcePath, binaryName) {
}
}
function cleanupTempDirectories() {
if (os.platform() !== "win32") return
try {
const globalNodeModules = path.join(process.env.APPDATA || "", "npm", "node_modules")
if (!fs.existsSync(globalNodeModules)) return
const entries = fs.readdirSync(globalNodeModules, { withFileTypes: true })
for (const entry of entries) {
if (entry.isDirectory() && entry.name.startsWith(".opencode-ai-")) {
const tempDir = path.join(globalNodeModules, entry.name)
try {
fs.rmSync(tempDir, { recursive: true, force: true })
console.log(`Cleaned up temporary directory: ${entry.name}`)
} catch (cleanupError) {
console.warn(`Failed to clean up ${entry.name}:`, cleanupError.message)
}
}
}
} catch (error) {
console.warn("Failed to cleanup temporary directories:", error.message)
}
}
async function main() {
try {
if (os.platform() === "win32") {
// On Windows, the .exe is already included in the package and bin field points to it
// No postinstall setup needed
console.log("Windows detected: binary setup not needed (using packaged .exe)")
// Clean up any leftover temporary directories from previous installations
cleanupTempDirectories()
return
}