

















@@ -108,6 +108,70 @@ function assertPluginNpmRuntimeBuildExists(plan) {
108108assertPackageFilesDoNotExcludeRequiredRuntimeArtifacts(plan);
109109}
110110111+function hasPackageRuntimeDependencies(packageJson) {
112+return (
113+Object.keys(packageJson.dependencies ?? {}).length > 0 ||
114+Object.keys(packageJson.optionalDependencies ?? {}).length > 0
115+);
116+}
117+118+function shouldBundleDependencies(value) {
119+return value === true || value === "1" || value === "true";
120+}
121+122+function installPackageLocalBundledDependencies(params) {
123+const packageJson = params.packageJson;
124+if (packageJson.bundleDependencies !== true || !hasPackageRuntimeDependencies(packageJson)) {
125+return () => {};
126+}
127+128+const shrinkwrapPath = path.join(params.packageDir, "npm-shrinkwrap.json");
129+if (!fs.existsSync(shrinkwrapPath)) {
130+throw new Error(
131+`package-local bundled dependency install requires npm-shrinkwrap.json for ${params.pluginDir}`,
132+);
133+}
134+135+const nodeModulesPath = path.join(params.packageDir, "node_modules");
136+if (fs.existsSync(nodeModulesPath)) {
137+throw new Error(
138+`package-local bundled dependency install refuses to replace existing node_modules for ${params.pluginDir}`,
139+);
140+}
141+142+console.error(`[plugin-npm-publish] installing bundled dependencies for ${params.pluginDir}`);
143+const result = spawnSync(
144+"npm",
145+[
146+"install",
147+"--omit=dev",
148+"--omit=peer",
149+"--legacy-peer-deps",
150+"--ignore-scripts",
151+"--no-audit",
152+"--no-fund",
153+"--package-lock=false",
154+"--loglevel=error",
155+],
156+{
157+cwd: params.packageDir,
158+env: process.env,
159+stdio: ["ignore", "ignore", "inherit"],
160+},
161+);
162+if (result.error) {
163+throw result.error;
164+}
165+if ((result.status ?? 1) !== 0) {
166+throw new Error(
167+`package-local bundled dependency install failed for ${params.pluginDir} with exit ${result.status ?? 1}`,
168+);
169+}
170+return () => {
171+fs.rmSync(nodeModulesPath, { recursive: true, force: true });
172+};
173+}
174+111175export function resolveAugmentedPluginNpmPackageJson(params) {
112176const repoRoot = path.resolve(params.repoRoot ?? ".");
113177const packageDir = resolvePackageDir(repoRoot, params.packageDir);
@@ -147,6 +211,11 @@ export function resolveAugmentedPluginNpmPackageJson(params) {
147211 ...(plan.runtimeSetupEntry ? { runtimeSetupEntry: plan.runtimeSetupEntry } : {}),
148212},
149213};
214+if (shouldBundleDependencies(params.bundleDependencies)) {
215+packageJson.bundleDependencies = true;
216+delete packageJson.bundledDependencies;
217+delete packageJson.devDependencies;
218+}
150219const changed = JSON.stringify(packageJson) !== JSON.stringify(plan.packageJson);
151220return {
152221 packageJsonPath,
@@ -155,6 +224,7 @@ export function resolveAugmentedPluginNpmPackageJson(params) {
155224 changed,
156225 packageJson,
157226pluginDir: plan.pluginDir,
227+bundleDependencies: shouldBundleDependencies(params.bundleDependencies),
158228reason: changed ? "package-local-runtime" : "unchanged",
159229};
160230}
@@ -290,13 +360,15 @@ export function resolveAugmentedPluginNpmManifest(params) {
290360export function withAugmentedPluginNpmManifestForPackage(params, callback) {
291361const repoRoot = path.resolve(params.repoRoot ?? ".");
292362const packageDir = resolvePackageDir(repoRoot, params.packageDir);
363+const bundleDependencies = shouldBundleDependencies(params.bundleDependencies);
293364const resolvedManifest = resolveAugmentedPluginNpmManifest({
294365 repoRoot,
295366 packageDir,
296367});
297368const resolvedPackageJson = resolveAugmentedPluginNpmPackageJson({
298369 repoRoot,
299370 packageDir,
371+ bundleDependencies,
300372});
301373302374if (
@@ -332,7 +404,15 @@ export function withAugmentedPluginNpmManifestForPackage(params, callback) {
332404);
333405writeJsonFile(resolvedPackageJson.packageJsonPath, resolvedPackageJson.packageJson);
334406}
407+let cleanupBundledDependencies = () => {};
335408try {
409+if (bundleDependencies && resolvedPackageJson.packageJson) {
410+cleanupBundledDependencies = installPackageLocalBundledDependencies({
411+ packageDir,
412+packageJson: resolvedPackageJson.packageJson,
413+pluginDir: resolvedPackageJson.pluginDir ?? path.basename(packageDir),
414+});
415+}
336416return callback({
337417 ...resolvedManifest,
338418 packageDir,
@@ -341,6 +421,7 @@ export function withAugmentedPluginNpmManifestForPackage(params, callback) {
341421packageJsonApplied: resolvedPackageJson.changed && Boolean(resolvedPackageJson.packageJson),
342422});
343423} finally {
424+cleanupBundledDependencies();
344425if (originalManifest !== undefined) {
345426fs.writeFileSync(resolvedManifest.manifestPath, originalManifest, "utf8");
346427}
@@ -372,17 +453,23 @@ function parseRunArgs(argv) {
372453373454function main(argv = process.argv.slice(2)) {
374455const { packageDir, command, args } = parseRunArgs(argv);
375-return withAugmentedPluginNpmManifestForPackage({ packageDir }, ({ packageDir: cwd }) => {
376-const result = spawnSync(command, args, {
377- cwd,
378-env: process.env,
379-stdio: "inherit",
380-});
381-if (result.error) {
382-throw result.error;
383-}
384-return result.status ?? 1;
385-});
456+return withAugmentedPluginNpmManifestForPackage(
457+{
458+ packageDir,
459+bundleDependencies: process.env.OPENCLAW_PLUGIN_NPM_BUNDLE_DEPENDENCIES,
460+},
461+({ packageDir: cwd }) => {
462+const result = spawnSync(command, args, {
463+ cwd,
464+env: process.env,
465+stdio: "inherit",
466+});
467+if (result.error) {
468+throw result.error;
469+}
470+return result.status ?? 1;
471+},
472+);
386473}
387474388475if (import.meta.url === pathToFileURL(process.argv[1] ?? "").href) {
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。