Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ COMMANDS:
mcp [install] Start stdio MCP server or 1-click install for Claude Desktop, Cursor, Windsurf
init [--ide] Initialize workspace or generate AI agent guidelines (.cursorrules, CLAUDE.md)
completion <shell> Generate native shell tab auto-completion (bash, zsh, fish, powershell)
which <ecosystem> Show active CLI binary for an ecosystem (npm, jsr, pypi, conda, cargo, go, nuget, composer, gem, pub, conan, vcpkg, meson, julia, maven, gradle, sbt, hex, rebar3, swift, cocoapods, carthage)
which <ecosystem> Show active CLI binary for an ecosystem (npm, jsr, pypi, conda, cargo, go, nuget, composer, gem, pub, conan, vcpkg, meson, julia, maven, gradle, sbt, hex, rebar3, swift, cocoapods, carthage, hackage, cran, cpan, luarocks)

OPTIONS:
-D, --dev Install as a development dependency
Expand Down Expand Up @@ -1075,6 +1075,7 @@ async function main(): Promise<void> {
process.exit(1);
}

await engineRegistry.ensureLoaded(resolved);
const dispatcher = new PolyDispatcher();
const info = dispatcher.which(resolved, cwd);
if (isJson) {
Expand Down
2 changes: 2 additions & 0 deletions src/detector/tree-walker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from "node:fs";
import path from "node:path";
import { syncCustomEnginesInDir } from "../engines/custom.js";
import { engineRegistry } from "../engines/registry.js";
import type { Ecosystem, ManifestContext } from "../types.js";

Expand Down Expand Up @@ -58,6 +59,7 @@ export function detectContexts(startDir: string): ManifestContext[] {
const visitedEcosystems = new Set<Ecosystem>();

while (true) {
syncCustomEnginesInDir(current);
let dirEntriesCache: string[] | undefined;

for (const adapter of engineRegistry.getAdapters()) {
Expand Down
19 changes: 17 additions & 2 deletions src/dispatcher/executor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { detectContexts, resolvePrimaryContext } from "../detector/tree-walker.js";
import { type BaseEngine, engineRegistry } from "../engines/index.js";
import type { BaseEngine } from "../engines/base.js";
import { engineRegistry } from "../engines/registry.js";
import { parsePackageSpec } from "../purl/parser.js";
import { RegistryRouter } from "../registry/index.js";
import { evaluateSandboxGate, injectSandboxFlags } from "../sandbox/index.js";
Expand Down Expand Up @@ -62,7 +63,9 @@ export class PolyDispatcher {
}
}

// 3. Select native engine
// 3. Select native engine (lazy-loading only the needed ecosystem adapter)
await engineRegistry.ensureLoaded(pkg.ecosystem);
await engineRegistry.ensureLoaded(pkg.ecosystem);
const engine = this.getEngineForEcosystem(pkg.ecosystem, cwd);
const { command, args } = engine.resolveAddArgs(pkg);
const routedArgs = this.registryRouter.injectCliFlags(pkg.ecosystem, args, cwd);
Expand Down Expand Up @@ -107,6 +110,7 @@ export class PolyDispatcher {
pkg.ecosystem = await this.resolveEcosystemForPackage(pkg.name, cwd);
}

await engineRegistry.ensureLoaded(pkg.ecosystem);
const engine = this.getEngineForEcosystem(pkg.ecosystem, cwd);
const mutation = engine.applyManifestRemove?.(pkg, cwd);
if (mutation?.changed && !options?.silent && !options?.agent) {
Expand Down Expand Up @@ -141,6 +145,7 @@ export class PolyDispatcher {

const results: ExecutionResult[] = [];
for (const ctx of allContexts) {
await engineRegistry.ensureLoaded(ctx.ecosystem);
const engine = this.getEngineForEcosystem(ctx.ecosystem, ctx.directory);
const { command, args } = engine.resolveInstallArgs();
const routedArgs = this.registryRouter.injectCliFlags(ctx.ecosystem, args, ctx.directory);
Expand Down Expand Up @@ -176,6 +181,7 @@ export class PolyDispatcher {
throw new Error(`No project manifests found in '${cwd}' or parent directories.`);
}

await engineRegistry.ensureLoaded(targetEcosystem);
const engine = this.getEngineForEcosystem(targetEcosystem, cwd);
const { command, args } = engine.resolveInstallArgs();
const routedArgs = this.registryRouter.injectCliFlags(targetEcosystem, args, cwd);
Expand Down Expand Up @@ -209,6 +215,7 @@ export class PolyDispatcher {
pkg.ecosystem = await this.resolveEcosystemForPackage(pkg.name, cwd, options?.agent);
}

await engineRegistry.ensureLoaded(pkg.ecosystem);
const engine = this.getEngineForEcosystem(pkg.ecosystem, cwd);
const { command, args } = engine.resolveUpdateArgs(pkg);
const routedArgs = this.registryRouter.injectCliFlags(pkg.ecosystem, args, cwd);
Expand Down Expand Up @@ -243,6 +250,7 @@ export class PolyDispatcher {

const results: ExecutionResult[] = [];
for (const ctx of allContexts) {
await engineRegistry.ensureLoaded(ctx.ecosystem);
const engine = this.getEngineForEcosystem(ctx.ecosystem, ctx.directory);
const { command, args } = engine.resolveUpdateArgs();
const routedArgs = this.registryRouter.injectCliFlags(ctx.ecosystem, args, ctx.directory);
Expand Down Expand Up @@ -278,6 +286,7 @@ export class PolyDispatcher {
throw new Error(`No project manifests found in '${cwd}' or parent directories.`);
}

await engineRegistry.ensureLoaded(targetEcosystem);
const engine = this.getEngineForEcosystem(targetEcosystem, cwd);
const { command, args } = engine.resolveUpdateArgs();
const routedArgs = this.registryRouter.injectCliFlags(targetEcosystem, args, cwd);
Expand All @@ -302,6 +311,12 @@ export class PolyDispatcher {
* Inspects which CLI tool powers an ecosystem
*/
which(ecosystem: Ecosystem, cwd?: string): { engine: string; effectiveCli: string } {
if (!engineRegistry.isAdapterLoaded(ecosystem)) {
const meta = engineRegistry.getAdapter(ecosystem);
if (meta) {
return { engine: meta.id, effectiveCli: meta.defaultCli };
}
}
const engine = this.getEngineForEcosystem(ecosystem, cwd ?? process.cwd());
return { engine: engine.name, effectiveCli: engine.getEffectiveCli() };
}
Expand Down
35 changes: 35 additions & 0 deletions src/doctor/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,41 @@ export class PolyDoctor {
args: ["version"],
hint: "https://github.com/Carthage/Carthage",
},
{
id: "toolchain-cabal",
name: "Haskell Cabal",
cmd: "cabal",
args: ["--version"],
hint: "https://www.haskell.org/ghcup/",
},
{
id: "toolchain-stack",
name: "Haskell Stack",
cmd: "stack",
args: ["--version"],
hint: "https://docs.haskellstack.org",
},
{
id: "toolchain-rscript",
name: "R / Rscript",
cmd: "Rscript",
args: ["--version"],
hint: "https://cran.r-project.org",
},
{
id: "toolchain-cpanm",
name: "Perl cpanm",
cmd: "cpanm",
args: ["--version"],
hint: "https://cpanmin.us",
},
{
id: "toolchain-luarocks",
name: "LuaRocks",
cmd: "luarocks",
args: ["--version"],
hint: "https://luarocks.org",
},
];

for (const tc of toolchains) {
Expand Down
193 changes: 193 additions & 0 deletions src/engines/core.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
import type { EngineAdapter } from "./declarative.js";
import { GoEngine } from "./go.js";
import { NodeEngine } from "./node.js";
import { PythonEngine } from "./python.js";
import { RustEngine } from "./rust.js";

export const npmAdapter: EngineAdapter = {
id: "node",
ecosystem: "npm",
aliases: ["node"],
purlTypes: ["npm"],
manifests: ["package.json"],
lockfiles: ["pnpm-lock.yaml", "bun.lockb", "bun.lock", "yarn.lock", "package-lock.json"],
osvEcosystem: "npm",
sbomPurlType: "npm",
defaultCli: "npm",
supportedClis: ["pnpm", "bun", "yarn", "npm", "deno"],
createEngine: (ctx) => new NodeEngine(ctx),
resolveScriptCommand: (script) => ["npm", ["run", script]],
injectRegistryEnv: (endpoint, env) => {
env.NPM_CONFIG_REGISTRY = endpoint.url;
env.npm_config_registry = endpoint.url;
if (endpoint.caFile) env.NPM_CONFIG_CAFILE = endpoint.caFile;
},
injectRegistryFlags: (endpoint, args) => {
if (!args.includes("--registry")) {
args.push("--registry", endpoint.url);
}
},
injectSandboxFlags: (_command, args) => {
const modified = [...args];
if (!modified.includes("--ignore-scripts")) {
modified.push("--ignore-scripts");
}
return modified;
},
resolveEphemeralCommand: (spec, trailingArgs, commandExists) => {
const base = spec.namespace ? `${spec.namespace}/${spec.name}` : spec.name;
const fullPkg = spec.version ? `${base}@${spec.version}` : base;
if (commandExists("bun")) {
return { command: "bunx", args: [fullPkg, ...trailingArgs] };
}
return { command: "npx", args: ["--yes", fullPkg, ...trailingArgs] };
},
};

export const jsrAdapter: EngineAdapter = {
id: "deno",
ecosystem: "jsr",
aliases: ["deno"],
purlTypes: ["jsr"],
manifests: ["deno.json", "deno.jsonc"],
lockfiles: ["deno.lock"],
osvEcosystem: "npm",
sbomPurlType: "jsr",
defaultCli: "deno",
supportedClis: ["deno", "pnpm", "bun", "npm", "yarn"],
createEngine: (ctx) => new NodeEngine({ ...ctx, ecosystem: "jsr" }),
resolveScriptCommand: (script) => ["deno", ["task", script]],
injectSandboxFlags: (command, args) => {
if (command !== "deno") {
const modified = [...args];
if (!modified.includes("--ignore-scripts")) modified.push("--ignore-scripts");
return modified;
}
return args;
},
resolveEphemeralCommand: (spec, trailingArgs, commandExists) => {
const base = spec.namespace ? `${spec.namespace}/${spec.name}` : spec.name;
const fullPkg = spec.version ? `${base}@${spec.version}` : base;
if (commandExists("deno")) {
return { command: "deno", args: ["run", `jsr:${fullPkg}`, ...trailingArgs] };
}
return { command: "npx", args: ["--yes", "jsr", "run", fullPkg, ...trailingArgs] };
},
};

export const pypiAdapter: EngineAdapter = {
id: "python",
ecosystem: "pypi",
aliases: ["python", "pip", "pipenv", "uv", "poetry"],
purlTypes: ["pypi"],
manifests: ["pyproject.toml", "requirements.txt", "Pipfile", "setup.py"],
lockfiles: ["uv.lock", "poetry.lock", "Pipfile.lock"],
osvEcosystem: "PyPI",
sbomPurlType: "pypi",
defaultCli: "uv",
supportedClis: ["uv", "poetry", "pipenv", "pip"],
createEngine: (ctx) => new PythonEngine(ctx),
resolveScriptCommand: (script) => ["python", ["-m", script]],
injectRegistryEnv: (endpoint, env) => {
env.UV_INDEX_URL = endpoint.url;
env.PIP_INDEX_URL = endpoint.url;
if (endpoint.trustedHost) env.PIP_TRUSTED_HOST = endpoint.trustedHost;
},
injectRegistryFlags: (endpoint, args) => {
if (!args.includes("--index-url")) {
args.push("--index-url", endpoint.url);
}
},
injectSandboxFlags: (command, args) => {
if (command === "pip" && !args.includes("--no-build-isolation")) {
return [...args, "--no-build-isolation"];
}
return args;
},
};

export const condaAdapter: EngineAdapter = {
id: "conda",
ecosystem: "conda",
aliases: ["anaconda", "mamba", "micromamba"],
purlTypes: ["conda"],
manifests: ["environment.yml", "environment.yaml"],
lockfiles: ["conda-lock.yml"],
osvEcosystem: undefined,
sbomPurlType: "conda",
defaultCli: "conda",
supportedClis: ["mamba", "micromamba", "conda"],
createEngine: (ctx) => new PythonEngine({ ...ctx, ecosystem: "conda" }),
resolveScriptCommand: (script) => [
"conda",
["run", "--no-capture-output", "python", "-m", script],
],
injectRegistryEnv: (endpoint, env) => {
env.CONDA_CHANNELS = endpoint.url;
},
resolveEphemeralCommand: (spec, trailingArgs, commandExists) => {
const runner = commandExists("mamba") ? "mamba" : commandExists("conda") ? "conda" : null;
if (!runner) return null;
return {
command: runner,
args: ["run", spec.name, ...trailingArgs],
};
},
};

export const cargoAdapter: EngineAdapter = {
id: "rust",
ecosystem: "cargo",
aliases: ["rust"],
purlTypes: ["cargo"],
manifests: ["Cargo.toml"],
lockfiles: ["Cargo.lock"],
osvEcosystem: "crates.io",
sbomPurlType: "cargo",
defaultCli: "rust",
supportedClis: ["cargo"],
createEngine: () => new RustEngine(),
resolveScriptCommand: (script) => {
if (script === "dev" || script === "start") return ["cargo", ["run"]];
if (script === "test") return ["cargo", ["test"]];
return ["cargo", [script]];
},
injectRegistryEnv: (endpoint, env) => {
env.CARGO_REGISTRIES_CRATES_IO_INDEX = endpoint.url;
},
};

export const goAdapter: EngineAdapter = {
id: "go",
ecosystem: "go",
aliases: ["golang"],
purlTypes: ["golang", "go"],
manifests: ["go.mod"],
lockfiles: ["go.sum"],
osvEcosystem: "Go",
sbomPurlType: "golang",
defaultCli: "go",
supportedClis: ["go"],
createEngine: () => new GoEngine(),
resolveScriptCommand: (script) => {
if (script === "dev" || script === "start") return ["go", ["run", "."]];
if (script === "test") return ["go", ["test", "./..."]];
return ["go", [script]];
},
injectRegistryEnv: (endpoint, env) => {
env.GOPROXY = endpoint.url;
},
resolveEphemeralCommand: (spec, trailingArgs) => {
const target = spec.version ? `${spec.name}@${spec.version}` : `${spec.name}@latest`;
return { command: "go", args: ["run", target, ...trailingArgs] };
},
};

export const CORE_ADAPTERS: readonly EngineAdapter[] = [
npmAdapter,
jsrAdapter,
pypiAdapter,
condaAdapter,
cargoAdapter,
goAdapter,
];
Loading
Loading