mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-14 07:18:35 +00:00
refactor(cli): extract plugin install plan helper
This commit is contained in:
54
src/cli/plugin-install-plan.ts
Normal file
54
src/cli/plugin-install-plan.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import type { BundledPluginSource } from "../plugins/bundled-sources.js";
|
||||
import { PLUGIN_INSTALL_ERROR_CODE } from "../plugins/install.js";
|
||||
import { shortenHomePath } from "../utils.js";
|
||||
|
||||
type BundledLookup = (params: {
|
||||
kind: "pluginId" | "npmSpec";
|
||||
value: string;
|
||||
}) => BundledPluginSource | undefined;
|
||||
|
||||
function isBareNpmPackageName(spec: string): boolean {
|
||||
const trimmed = spec.trim();
|
||||
return /^[a-z0-9][a-z0-9-._~]*$/.test(trimmed);
|
||||
}
|
||||
|
||||
export function resolveBundledInstallPlanBeforeNpm(params: {
|
||||
rawSpec: string;
|
||||
findBundledSource: BundledLookup;
|
||||
}): { bundledSource: BundledPluginSource; warning: string } | null {
|
||||
if (!isBareNpmPackageName(params.rawSpec)) {
|
||||
return null;
|
||||
}
|
||||
const bundledSource = params.findBundledSource({
|
||||
kind: "pluginId",
|
||||
value: params.rawSpec,
|
||||
});
|
||||
if (!bundledSource) {
|
||||
return null;
|
||||
}
|
||||
return {
|
||||
bundledSource,
|
||||
warning: `Using bundled plugin "${bundledSource.pluginId}" from ${shortenHomePath(bundledSource.localPath)} for bare install spec "${params.rawSpec}". To install an npm package with the same name, use a scoped package name (for example @scope/${params.rawSpec}).`,
|
||||
};
|
||||
}
|
||||
|
||||
export function resolveBundledInstallPlanForNpmFailure(params: {
|
||||
rawSpec: string;
|
||||
code?: string;
|
||||
findBundledSource: BundledLookup;
|
||||
}): { bundledSource: BundledPluginSource; warning: string } | null {
|
||||
if (params.code !== PLUGIN_INSTALL_ERROR_CODE.NPM_PACKAGE_NOT_FOUND) {
|
||||
return null;
|
||||
}
|
||||
const bundledSource = params.findBundledSource({
|
||||
kind: "npmSpec",
|
||||
value: params.rawSpec,
|
||||
});
|
||||
if (!bundledSource) {
|
||||
return null;
|
||||
}
|
||||
return {
|
||||
bundledSource,
|
||||
warning: `npm package unavailable for ${params.rawSpec}; using bundled plugin at ${shortenHomePath(bundledSource.localPath)}.`,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user