Reorganize repo structure and standards references
This commit is contained in:
1 parent
3cb93a81be
commit
be2054274d
5885 files changed
+2588
-195
No files matched your search
@@ -0,0 +1,119 @@
|
||||
/**
|
||||
* `plugins build` — Phase 06 #23 — orchestrate the wasm build.
|
||||
*
|
||||
* The actual ts→wasm compilation is operator-toolchain-specific (the
|
||||
* supported chains today are AssemblyScript via `asc`, TinyGo via
|
||||
* `tinygo build -target=wasi`, and Rust via `cargo build --target
|
||||
* wasm32-wasi`). The CLI is intentionally pure orchestration: it
|
||||
* detects which toolchain the plugin is using by reading its
|
||||
* package.json / Cargo.toml / asconfig.json and prints the canonical
|
||||
* build command to run. When no toolchain marker is found, the command
|
||||
* surfaces a clear next-step rather than guessing.
|
||||
*
|
||||
* Why no auto-exec: spawning a heterogeneous compiler chain from this
|
||||
* CLI silently hides errors and surprises authors with global-binary
|
||||
* dependencies. Printing the command keeps the build path transparent
|
||||
* and lets CI scripts wire it in their own way.
|
||||
*/
|
||||
import { defineCommand } from "citty";
|
||||
import consola from "consola";
|
||||
import { readFile, stat } from "node:fs/promises";
|
||||
import { join, resolve } from "node:path";
|
||||
|
||||
export type DetectedToolchain = "assemblyscript" | "tinygo" | "rust-cargo" | "unknown";
|
||||
|
||||
export interface DetectToolchainInput {
|
||||
readonly cwd: string;
|
||||
readonly readFile: (p: string) => Promise<string>;
|
||||
readonly exists: (p: string) => Promise<boolean>;
|
||||
}
|
||||
|
||||
export async function detectToolchain(input: DetectToolchainInput): Promise<DetectedToolchain> {
|
||||
if (await input.exists(join(input.cwd, "asconfig.json"))) {
|
||||
return "assemblyscript";
|
||||
}
|
||||
if (await input.exists(join(input.cwd, "Cargo.toml"))) {
|
||||
return "rust-cargo";
|
||||
}
|
||||
if (await input.exists(join(input.cwd, "go.mod"))) {
|
||||
return "tinygo";
|
||||
}
|
||||
// Also accept package.json with AS / wasi deps.
|
||||
if (await input.exists(join(input.cwd, "package.json"))) {
|
||||
try {
|
||||
const pkg = JSON.parse(await input.readFile(join(input.cwd, "package.json"))) as Record<string, unknown>;
|
||||
const deps = {
|
||||
...((pkg.dependencies as Record<string, string> | undefined) ?? {}),
|
||||
...((pkg.devDependencies as Record<string, string> | undefined) ?? {}),
|
||||
};
|
||||
if (typeof deps["assemblyscript"] === "string") {
|
||||
return "assemblyscript";
|
||||
}
|
||||
} catch {
|
||||
// Fall through to unknown.
|
||||
}
|
||||
}
|
||||
return "unknown";
|
||||
}
|
||||
|
||||
export function buildCommandForToolchain(
|
||||
toolchain: DetectedToolchain,
|
||||
options: { output: string }
|
||||
): { command: string; description: string } {
|
||||
switch (toolchain) {
|
||||
case "assemblyscript":
|
||||
return {
|
||||
command: `npx asc src/index.ts --target release --outFile ${options.output}`,
|
||||
description: "AssemblyScript via `asc` (release target).",
|
||||
};
|
||||
case "tinygo":
|
||||
return {
|
||||
command: `tinygo build -target=wasi -o ${options.output} ./...`,
|
||||
description: "TinyGo with the wasi target.",
|
||||
};
|
||||
case "rust-cargo":
|
||||
return {
|
||||
command: `cargo build --release --target wasm32-wasi && cp target/wasm32-wasi/release/*.wasm ${options.output}`,
|
||||
description: "Rust via cargo + wasm32-wasi target.",
|
||||
};
|
||||
case "unknown":
|
||||
default:
|
||||
return {
|
||||
command: "<install a wasm toolchain — see docs/plugins/cli-reference.md#build>",
|
||||
description: "No toolchain marker (asconfig.json / Cargo.toml / go.mod) detected.",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export const pluginsBuildCommand = defineCommand({
|
||||
meta: {
|
||||
name: "build",
|
||||
description: "Detect the plugin's wasm toolchain and print the canonical build command.",
|
||||
},
|
||||
args: {
|
||||
cwd: { type: "string", description: "Plugin source directory (default: process cwd)." },
|
||||
output: { type: "string", description: "Output wasm filename (default: plugin.wasm)." },
|
||||
},
|
||||
async run({ args }) {
|
||||
const cwd = args.cwd ? resolve(String(args.cwd)) : process.cwd();
|
||||
const output = args.output ? String(args.output) : "plugin.wasm";
|
||||
const toolchain = await detectToolchain({
|
||||
cwd,
|
||||
readFile: async (p) => await readFile(p, "utf8"),
|
||||
exists: async (p) => {
|
||||
try {
|
||||
await stat(p);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
},
|
||||
});
|
||||
const { command, description } = buildCommandForToolchain(toolchain, { output });
|
||||
consola.info(`Detected toolchain: ${toolchain} (${description})`);
|
||||
consola.log(command);
|
||||
if (toolchain === "unknown") {
|
||||
process.exitCode = 1;
|
||||
}
|
||||
},
|
||||
});
|
||||
Reference in new issue
Block a user