diff --git a/npm/yaci-devkit/package-lock.json b/npm/yaci-devkit/package-lock.json index 0a3c828..7751393 100644 --- a/npm/yaci-devkit/package-lock.json +++ b/npm/yaci-devkit/package-lock.json @@ -1,16 +1,16 @@ { "name": "@bloxbean/yaci-devkit", - "version": "0.10.0", + "version": "0.0.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@bloxbean/yaci-devkit", - "version": "0.10.0", + "version": "0.0.1", "license": "MIT", "dependencies": { - "@bloxbean/yaci-devkit-linux-x64": "file:../yaci-devkit-linux-x64", - "@bloxbean/yaci-devkit-macos-arm64": "file:../yaci-devkit-macos-arm64" + "@bloxbean/yaci-devkit-linux-x64": "0.0.1", + "@bloxbean/yaci-devkit-macos-arm64": "0.0.1" }, "bin": { "yaci-devkit": "start.mjs" @@ -25,7 +25,7 @@ }, "../yaci-devkit-linux-x64": { "name": "@bloxbean/yaci-devkit-linux-x64", - "version": "0.10.0", + "version": "0.0.1", "license": "MIT", "bin": { "yaci-devkit-linux-x64": "yaci-cli" @@ -36,7 +36,7 @@ }, "../yaci-devkit-macos-arm64": { "name": "@bloxbean/yaci-devkit-macos-arm64", - "version": "0.10.0", + "version": "0.0.1", "license": "MIT", "bin": { "yaci-devkit-linux-x64": "yaci-cli" diff --git a/npm/yaci-devkit/start.mjs b/npm/yaci-devkit/start.mjs index d8c0e47..3de7f14 100755 --- a/npm/yaci-devkit/start.mjs +++ b/npm/yaci-devkit/start.mjs @@ -1,9 +1,11 @@ #!/usr/bin/env node import { spawn } from "node:child_process"; -import { platform } from "node:os"; -import { fileURLToPath } from 'node:url'; -import { dirname, resolve } from 'node:path'; +import { platform, homedir } from "node:os"; +import { fileURLToPath } from "node:url"; +import { dirname, resolve, join } from "node:path"; +import { existsSync, readFileSync, writeFileSync, unlinkSync, mkdirSync } from "node:fs"; + // import { createHash } from "node:crypto"; const osType = platform(); @@ -38,6 +40,42 @@ const devkitDir = dirname(binPath); const configPath = resolve(devkitDir, 'config'); +// Determine the path to the user's `.yaci-cli` directory +const yaciCliHome = join(homedir(), ".yaci-cli"); +if (!existsSync(yaciCliHome)) { + mkdirSync(yaciCliHome, { recursive: true }); +} + +// Path for the PID file +const pidFilePath = join(yaciCliHome, "yaci-cli.pid"); + +function killProcessTree(pid) { + try { + if (osType === "linux" || osType === "darwin") { + // Use `pkill` to kill the entire process tree + spawn("pkill", ["-TERM", "-P", pid], { stdio: "ignore" }); + process.kill(pid, "SIGKILL"); // Kill the main process + } else { + console.error("Unsupported platform for process termination."); + } + } catch (err) { + console.error(`Failed to kill process tree for PID ${pid}: ${err.message}`); + } +} + +if (existsSync(pidFilePath)) { + try { + const existingPid = parseInt(readFileSync(pidFilePath, "utf-8"), 10); + if (!isNaN(existingPid)) { + console.log(`Killing existing yaci-cli process and its subprocesses with PID: ${existingPid}`); + killProcessTree(existingPid); // Kill the process tree + unlinkSync(pidFilePath); // Remove the stale PID file + } + } catch (err) { + console.error(`Failed to clean up existing yaci-cli process: ${err.message}`); + } +} + // const tmpSuffix = createHash('md5').update(workDir).digest("hex"); // const yaciCLIHome = resolve("/tmp", ".yaci-cli" + tmpSuffix ) @@ -55,6 +93,17 @@ const child = spawn(binPath, [additionalConfigArg, ...process.argv.slice(2)], { }); + +writeFileSync(pidFilePath, child.pid.toString()); +console.log(`yaci-cli process started with PID: ${child.pid}`); + +// Handle process exit child.on("close", (code) => { + console.log(`yaci-cli process exited with code: ${code}`); + try { + unlinkSync(pidFilePath); + } catch (err) { + console.error(`Failed to remove PID file: ${err.message}`); + } process.exit(code ?? 0); });