akm-cli
Version:
akm (Agent Knowledge Manager) — a portable, local-first capability library for AI agents. Discover, load, share, and improve reusable skills, scripts, workflows, and knowledge across any shell-capable coding agent, including Claude Code, OpenCode, and Cur
68 lines (67 loc) • 3.83 kB
JavaScript
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
import { spawnSync } from "node:child_process";
import fs from "node:fs";
import { fileURLToPath } from "node:url";
import { NotFoundError } from "../core/errors.js";
function migrationEntryPoint() {
const candidates = [
fileURLToPath(new URL(process.versions.bun ? "../scripts/akm-migrate.js" : "../scripts/akm-migrate-node.js", import.meta.url)),
...(process.versions.bun ? [fileURLToPath(new URL("../../scripts/akm-migrate.ts", import.meta.url))] : []),
];
// asset-io: shipped akm-migrate script
return candidates.find((candidate) => fs.existsSync(candidate));
}
/**
* Set by {@link runMigrationTool}: the child ends its stderr with one {@link MIGRATE_OUTCOME_PREFIX} line holding why it
* failed, for akm's usage report. The line carries only akm's closed-list error code and notes, never a message.
*/
export const MIGRATE_OUTCOME_ENV = "AKM_MIGRATE_OUTCOME";
export const MIGRATE_OUTCOME_PREFIX = "akm-migrate-outcome: ";
/** Split the child's outcome line from the stderr it shows: the line is for akm, not the user. */
export function takeMigrationOutcome(stderr) {
const at = stderr.lastIndexOf(MIGRATE_OUTCOME_PREFIX);
if (at === -1 || (at > 0 && stderr[at - 1] !== "\n"))
return { stderr };
const end = stderr.indexOf("\n", at);
const raw = stderr.slice(at + MIGRATE_OUTCOME_PREFIX.length, end === -1 ? undefined : end);
const rest = stderr.slice(0, at) + (end === -1 ? "" : stderr.slice(end + 1));
try {
const parsed = JSON.parse(raw);
const notes = Array.isArray(parsed.notes) ? parsed.notes.filter((note) => typeof note === "string") : [];
return { stderr: rest, outcome: { code: typeof parsed.code === "string" ? parsed.code : undefined, notes } };
}
catch {
return { stderr: rest };
}
}
/**
* Spawns the standalone `akm-migrate` tool and returns its captured exit
* status plus stdout/stderr — never writes them itself. `migrate-cli.ts` uses
* this packaging boundary to render the task-only plan through the normal
* output pipeline.
*/
export async function runMigrationTool(args) {
const entry = migrationEntryPoint();
if (!entry && process.env.AKM_MIGRATE_ENTRY === "1") {
// Re-exec loop guard: we ARE the marked child, yet no migrator entry
// resolved and the standalone wrapper did not intercept the marker — this
// binary was compiled without `scripts/akm-standalone.ts`.
throw new NotFoundError("This binary was built without the embedded akm-migrate tool.", "FILE_NOT_FOUND", "Rebuild from scripts/akm-standalone.ts, or run akm-migrate from a source/npm install.");
}
// Compiled standalone: no scripts/ tree exists on disk (`import.meta.url`
// resolves inside the binary's virtual filesystem). Release binaries embed
// the task migrator in `scripts/akm-standalone.ts` and re-exec themselves
// with the internal marker.
// (src must not import scripts/: the dist build's tsc has `rootDir: src`.)
const result = spawnSync(process.execPath, entry ? [entry, ...args] : [...args], {
encoding: "utf8",
env: { ...process.env, [MIGRATE_OUTCOME_ENV]: "1", ...(entry ? {} : { AKM_MIGRATE_ENTRY: "1" }) },
maxBuffer: 16 * 1024 * 1024,
});
if (result.error) {
throw new NotFoundError(`Cannot start the standalone akm-migrate tool: ${result.error.message}`, "FILE_NOT_FOUND", "Reinstall akm-cli, or use a runtime-free standalone release binary.");
}
return { status: result.status ?? 1, stdout: result.stdout ?? "", ...takeMigrationOutcome(result.stderr ?? "") };
}