|
| 1 | +/** Selects built plugin artifacts without importing active runtime state. */ |
| 2 | +import fs from "node:fs"; |
| 3 | +import path from "node:path"; |
| 4 | +import { resolveRealpathOrAbsolute } from "../infra/boundary-path.js"; |
| 5 | +import type { OpenClawPackageManifest } from "./manifest.js"; |
| 6 | +import type { PluginOrigin } from "./plugin-origin.types.js"; |
| 7 | + |
| 8 | +function rewriteBundledRuntimeArtifactRelativePath(relativePath: string): string { |
| 9 | + return relativePath.replace(/\.[^.]+$/u, ".js"); |
| 10 | +} |
| 11 | + |
| 12 | +function listPackageLocalRuntimeArtifactOutputExtensions(sourceExt: string): string[] { |
| 13 | + switch (sourceExt) { |
| 14 | + case ".mts": |
| 15 | + case ".mjs": |
| 16 | + return [".mjs", ".js", ".cjs"]; |
| 17 | + case ".cts": |
| 18 | + case ".cjs": |
| 19 | + return [".cjs", ".js", ".mjs"]; |
| 20 | + default: |
| 21 | + return [".js", ".mjs", ".cjs"]; |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +function listPackageLocalRuntimeArtifactRelativePathBases(relativePath: string): string[] { |
| 26 | + const ext = path.extname(relativePath).toLowerCase(); |
| 27 | + const withoutExt = ext ? relativePath.slice(0, -ext.length) : relativePath; |
| 28 | + if (!withoutExt.startsWith(`src${path.sep}`) && !withoutExt.startsWith("src/")) { |
| 29 | + return [withoutExt]; |
| 30 | + } |
| 31 | + return [withoutExt.slice(4), withoutExt]; |
| 32 | +} |
| 33 | + |
| 34 | +function listPackageLocalDistRuntimeArtifactRelativePaths(relativePath: string): string[] { |
| 35 | + const ext = path.extname(relativePath).toLowerCase(); |
| 36 | + const candidates = new Set<string>(); |
| 37 | + for (const base of listPackageLocalRuntimeArtifactRelativePathBases(relativePath)) { |
| 38 | + for (const outputExt of listPackageLocalRuntimeArtifactOutputExtensions(ext)) { |
| 39 | + candidates.add(`${base}${outputExt}`); |
| 40 | + } |
| 41 | + } |
| 42 | + return [...candidates]; |
| 43 | +} |
| 44 | + |
| 45 | +function shouldPreferPackageLocalDistRuntimeArtifact(source: string): boolean { |
| 46 | + switch (path.extname(source).toLowerCase()) { |
| 47 | + case ".ts": |
| 48 | + case ".tsx": |
| 49 | + case ".mts": |
| 50 | + case ".cts": |
| 51 | + return true; |
| 52 | + default: |
| 53 | + return false; |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +function resolvePackageLocalDistRuntimeArtifact(params: { |
| 58 | + source: string; |
| 59 | + rootDir: string; |
| 60 | +}): string | null { |
| 61 | + const relativeSource = path.relative(params.rootDir, params.source); |
| 62 | + if ( |
| 63 | + !shouldPreferPackageLocalDistRuntimeArtifact(relativeSource) || |
| 64 | + relativeSource === "" || |
| 65 | + relativeSource.startsWith("..") || |
| 66 | + path.isAbsolute(relativeSource) |
| 67 | + ) { |
| 68 | + return null; |
| 69 | + } |
| 70 | + const artifactRoot = path.join(params.rootDir, "dist"); |
| 71 | + for (const artifactRelativePath of listPackageLocalDistRuntimeArtifactRelativePaths( |
| 72 | + relativeSource, |
| 73 | + )) { |
| 74 | + const artifactSource = path.join(artifactRoot, artifactRelativePath); |
| 75 | + if (fs.existsSync(artifactSource)) { |
| 76 | + return resolveRealpathOrAbsolute(artifactSource); |
| 77 | + } |
| 78 | + } |
| 79 | + return null; |
| 80 | +} |
| 81 | + |
| 82 | +function resolvePreferredBundledRootArtifactFromCanonicalPaths(params: { |
| 83 | + source: string; |
| 84 | + rootDir: string; |
| 85 | + packageManifest?: OpenClawPackageManifest; |
| 86 | +}): { source: string; rootDir: string } { |
| 87 | + const { rootDir, source } = params; |
| 88 | + const sourceExternal = params.packageManifest?.build?.bundledDist === false; |
| 89 | + const extensionsDir = path.dirname(rootDir); |
| 90 | + if (path.basename(extensionsDir) !== "extensions") { |
| 91 | + return { source, rootDir }; |
| 92 | + } |
| 93 | + const packageRoot = path.dirname(extensionsDir); |
| 94 | + if (path.basename(packageRoot) === "dist" || path.basename(packageRoot) === "dist-runtime") { |
| 95 | + return { source, rootDir }; |
| 96 | + } |
| 97 | + const relativeSource = path.relative(rootDir, source); |
| 98 | + if (relativeSource === "" || relativeSource.startsWith("..") || path.isAbsolute(relativeSource)) { |
| 99 | + return { source, rootDir }; |
| 100 | + } |
| 101 | + const artifactRelativePath = rewriteBundledRuntimeArtifactRelativePath(relativeSource); |
| 102 | + // Source-external packaging can replace the flat root build while leaving its |
| 103 | + // staging wrapper behind, so only bundled artifacts may fall back to dist-runtime. |
| 104 | + for (const artifactRootName of sourceExternal ? ["dist"] : ["dist-runtime", "dist"]) { |
| 105 | + const artifactRoot = path.join( |
| 106 | + packageRoot, |
| 107 | + artifactRootName, |
| 108 | + "extensions", |
| 109 | + path.basename(rootDir), |
| 110 | + ); |
| 111 | + const artifactSource = path.join(artifactRoot, artifactRelativePath); |
| 112 | + if (fs.existsSync(artifactSource)) { |
| 113 | + return { |
| 114 | + source: resolveRealpathOrAbsolute(artifactSource), |
| 115 | + rootDir: resolveRealpathOrAbsolute(artifactRoot), |
| 116 | + }; |
| 117 | + } |
| 118 | + } |
| 119 | + return { source, rootDir }; |
| 120 | +} |
| 121 | + |
| 122 | +/** Selects the lifecycle-owned root build for one bundled source artifact. */ |
| 123 | +export function resolvePreferredBundledRootArtifact(params: { |
| 124 | + source: string; |
| 125 | + rootDir: string; |
| 126 | + packageManifest?: OpenClawPackageManifest; |
| 127 | +}): { source: string; rootDir: string } { |
| 128 | + return resolvePreferredBundledRootArtifactFromCanonicalPaths({ |
| 129 | + source: resolveRealpathOrAbsolute(params.source), |
| 130 | + rootDir: resolveRealpathOrAbsolute(params.rootDir), |
| 131 | + packageManifest: params.packageManifest, |
| 132 | + }); |
| 133 | +} |
| 134 | + |
| 135 | +/** Applies source, package-local, and root-build preference without runtime memo state. */ |
| 136 | +export function resolvePreferredBuiltRuntimeArtifact(params: { |
| 137 | + source: string; |
| 138 | + rootDir: string; |
| 139 | + origin: PluginOrigin; |
| 140 | + preferBuiltPluginArtifacts: boolean; |
| 141 | + packageManifest?: OpenClawPackageManifest; |
| 142 | +}): { source: string; rootDir: string } { |
| 143 | + // The stateful resolver canonicalizes both paths before memo-key construction. |
| 144 | + const { rootDir, source } = params; |
| 145 | + if (!params.preferBuiltPluginArtifacts) { |
| 146 | + return { source, rootDir }; |
| 147 | + } |
| 148 | + if (params.origin !== "bundled") { |
| 149 | + const artifactSource = resolvePackageLocalDistRuntimeArtifact({ source, rootDir }); |
| 150 | + return artifactSource ? { source: artifactSource, rootDir } : { source, rootDir }; |
| 151 | + } |
| 152 | + // Source-external plugins keep source authoritative over package-local output; |
| 153 | + // only the lifecycle-owned canonical root build may replace that pair. |
| 154 | + const sourceExternal = params.packageManifest?.build?.bundledDist === false; |
| 155 | + const packageLocalArtifactSource = sourceExternal |
| 156 | + ? null |
| 157 | + : resolvePackageLocalDistRuntimeArtifact({ source, rootDir }); |
| 158 | + if (packageLocalArtifactSource) { |
| 159 | + return { source: packageLocalArtifactSource, rootDir }; |
| 160 | + } |
| 161 | + return resolvePreferredBundledRootArtifactFromCanonicalPaths({ |
| 162 | + source, |
| 163 | + rootDir, |
| 164 | + packageManifest: params.packageManifest, |
| 165 | + }); |
| 166 | +} |
0 commit comments