diff --git a/.npm/postinstall.js b/.npm/postinstall.js new file mode 100644 index 0000000..812eb36 --- /dev/null +++ b/.npm/postinstall.js @@ -0,0 +1,61 @@ +#!/usr/bin/env node + +const { spawn } = require("child_process"); +const { existsSync } = require("fs"); +const path = require("path"); + +const pkgRoot = path.join(__dirname, ".."); +const rootPkg = require(path.join(pkgRoot, "package.json")); +const optionalDependencies = rootPkg.optionalDependencies || {}; +const platforms = rootPkg.customerioCli?.platforms || []; +const platform = platforms.find( + (candidate) => candidate.os === process.platform && candidate.cpu === process.arch +); + +if (!platform) { + console.log("[cio postinstall] No matching platform found for", process.platform, process.arch); + process.exit(0); +} + +const platformPackage = Object.keys(optionalDependencies).find((packageName) => + packageName.endsWith(`-${platform.npm}`) +); + +if (!platformPackage) { + console.log("[cio postinstall] No platform package found for", platform.npm); + process.exit(0); +} + +let platformPackageRoot; +try { + platformPackageRoot = path.dirname( + require.resolve(`${platformPackage}/package.json`, { paths: [pkgRoot] }) + ); +} catch { + console.log("[cio postinstall] Could not resolve", platformPackage); + process.exit(0); +} + +const binPath = path.join(platformPackageRoot, "bin", `cio${platform.ext || ""}`); + +if (!existsSync(binPath)) { + console.log("[cio postinstall] Binary not found at", binPath); + process.exit(0); +} + +console.log("[cio postinstall] Running: cio skills install"); +const child = spawn(binPath, ["skills", "install"], { + stdio: "inherit", +}); + +child.on("error", (err) => { + console.log("[cio postinstall] Failed to run cio skills install:", err.message); + process.exit(0); +}); + +child.on("close", (code) => { + if (code !== 0) { + console.log("[cio postinstall] cio skills install exited with code", code); + } + process.exit(0); +}); diff --git a/package.json b/package.json index f8b1f56..bec2768 100644 --- a/package.json +++ b/package.json @@ -5,8 +5,12 @@ "bin": { "cio": ".npm/run.js" }, + "scripts": { + "postinstall": "node .npm/postinstall.js" + }, "files": [ ".npm/run.js", + ".npm/postinstall.js", "LICENSE", "README.md" ],