fix wecom channel (#530)
This commit is contained in:
committed by
GitHub
Unverified
parent
7e54aad9e6
commit
f1e2e9fa01
@@ -1,6 +1,6 @@
|
||||
import { app } from 'electron';
|
||||
import path from 'path';
|
||||
import { existsSync, readFileSync, cpSync, mkdirSync, rmSync, readdirSync, realpathSync } from 'fs';
|
||||
import { existsSync, readFileSync, cpSync, mkdirSync, rmSync } from 'fs';
|
||||
import { homedir } from 'os';
|
||||
import { join } from 'path';
|
||||
import { getAllSettings } from '../utils/store';
|
||||
@@ -14,6 +14,7 @@ import { buildProxyEnv, resolveProxySettings } from '../utils/proxy';
|
||||
import { syncProxyConfigToOpenClaw } from '../utils/openclaw-proxy';
|
||||
import { logger } from '../utils/logger';
|
||||
import { prependPathEntry } from '../utils/env-path';
|
||||
import { copyPluginFromNodeModules, fixupPluginManifest } from '../utils/plugin-install';
|
||||
|
||||
export interface GatewayLaunchContext {
|
||||
appSettings: Awaited<ReturnType<typeof getAllSettings>>;
|
||||
@@ -47,112 +48,6 @@ function readPluginVersion(pkgJsonPath: string): string | null {
|
||||
}
|
||||
}
|
||||
|
||||
/** Walk up from a path until we find a parent named node_modules. */
|
||||
function findParentNodeModules(startPath: string): string | null {
|
||||
let dir = startPath;
|
||||
while (dir !== path.dirname(dir)) {
|
||||
if (path.basename(dir) === 'node_modules') return dir;
|
||||
dir = path.dirname(dir);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** List packages inside a node_modules dir (handles @scoped packages). */
|
||||
function listPackagesInDir(nodeModulesDir: string): Array<{ name: string; fullPath: string }> {
|
||||
const result: Array<{ name: string; fullPath: string }> = [];
|
||||
if (!existsSync(nodeModulesDir)) return result;
|
||||
const SKIP = new Set(['.bin', '.package-lock.json', '.modules.yaml', '.pnpm']);
|
||||
for (const entry of readdirSync(nodeModulesDir, { withFileTypes: true })) {
|
||||
if (!entry.isDirectory() && !entry.isSymbolicLink()) continue;
|
||||
if (SKIP.has(entry.name)) continue;
|
||||
const entryPath = join(nodeModulesDir, entry.name);
|
||||
if (entry.name.startsWith('@')) {
|
||||
try {
|
||||
for (const sub of readdirSync(entryPath)) {
|
||||
result.push({ name: `${entry.name}/${sub}`, fullPath: join(entryPath, sub) });
|
||||
}
|
||||
} catch { /* ignore */ }
|
||||
} else {
|
||||
result.push({ name: entry.name, fullPath: entryPath });
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy a plugin from a pnpm node_modules location, including its
|
||||
* transitive runtime dependencies (replicates bundle-openclaw-plugins.mjs
|
||||
* logic).
|
||||
*/
|
||||
function copyPluginFromNodeModules(npmPkgPath: string, targetDir: string, npmName: string): void {
|
||||
let realPath: string;
|
||||
try {
|
||||
realPath = realpathSync(npmPkgPath);
|
||||
} catch {
|
||||
throw new Error(`Cannot resolve real path for ${npmPkgPath}`);
|
||||
}
|
||||
|
||||
// 1. Copy plugin package itself
|
||||
rmSync(targetDir, { recursive: true, force: true });
|
||||
mkdirSync(targetDir, { recursive: true });
|
||||
cpSync(realPath, targetDir, { recursive: true, dereference: true });
|
||||
|
||||
// 2. Collect transitive deps from pnpm virtual store
|
||||
const rootVirtualNM = findParentNodeModules(realPath);
|
||||
if (!rootVirtualNM) {
|
||||
logger.warn(`[plugin] Cannot find virtual store node_modules for ${npmName}, plugin may lack deps`);
|
||||
return;
|
||||
}
|
||||
|
||||
// Read peer deps to skip (they're provided by the host gateway)
|
||||
const SKIP_PACKAGES = new Set(['typescript', '@playwright/test']);
|
||||
try {
|
||||
const pluginPkg = JSON.parse(readFileSync(join(targetDir, 'package.json'), 'utf-8'));
|
||||
for (const peer of Object.keys(pluginPkg.peerDependencies || {})) {
|
||||
SKIP_PACKAGES.add(peer);
|
||||
}
|
||||
} catch { /* ignore */ }
|
||||
|
||||
const collected = new Map<string, string>(); // realPath → packageName
|
||||
const queue: Array<{ nodeModulesDir: string; skipPkg: string }> = [
|
||||
{ nodeModulesDir: rootVirtualNM, skipPkg: npmName },
|
||||
];
|
||||
|
||||
while (queue.length > 0) {
|
||||
const { nodeModulesDir, skipPkg } = queue.shift()!;
|
||||
for (const { name, fullPath } of listPackagesInDir(nodeModulesDir)) {
|
||||
if (name === skipPkg) continue;
|
||||
if (SKIP_PACKAGES.has(name) || name.startsWith('@types/')) continue;
|
||||
let depRealPath: string;
|
||||
try {
|
||||
depRealPath = realpathSync(fullPath);
|
||||
} catch { continue; }
|
||||
if (collected.has(depRealPath)) continue;
|
||||
collected.set(depRealPath, name);
|
||||
const depVirtualNM = findParentNodeModules(depRealPath);
|
||||
if (depVirtualNM && depVirtualNM !== nodeModulesDir) {
|
||||
queue.push({ nodeModulesDir: depVirtualNM, skipPkg: name });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Copy flattened deps into targetDir/node_modules/
|
||||
const outputNM = join(targetDir, 'node_modules');
|
||||
mkdirSync(outputNM, { recursive: true });
|
||||
const copiedNames = new Set<string>();
|
||||
for (const [depRealPath, pkgName] of collected) {
|
||||
if (copiedNames.has(pkgName)) continue;
|
||||
copiedNames.add(pkgName);
|
||||
const dest = join(outputNM, pkgName);
|
||||
try {
|
||||
mkdirSync(path.dirname(dest), { recursive: true });
|
||||
cpSync(depRealPath, dest, { recursive: true, dereference: true });
|
||||
} catch { /* skip individual dep failures */ }
|
||||
}
|
||||
|
||||
logger.info(`[plugin] Copied ${copiedNames.size} deps for ${npmName}`);
|
||||
}
|
||||
|
||||
function buildBundledPluginSources(pluginDirName: string): string[] {
|
||||
return app.isPackaged
|
||||
? [
|
||||
@@ -179,9 +74,8 @@ function ensureConfiguredPluginsUpgraded(configuredChannels: string[]): void {
|
||||
|
||||
const targetDir = join(homedir(), '.openclaw', 'extensions', dirName);
|
||||
const targetManifest = join(targetDir, 'openclaw.plugin.json');
|
||||
if (!existsSync(targetManifest)) continue; // not installed, nothing to upgrade
|
||||
|
||||
const installedVersion = readPluginVersion(join(targetDir, 'package.json'));
|
||||
const isInstalled = existsSync(targetManifest);
|
||||
const installedVersion = isInstalled ? readPluginVersion(join(targetDir, 'package.json')) : null;
|
||||
|
||||
// Try bundled sources first (packaged mode or if bundle-plugins was run)
|
||||
const bundledSources = buildBundledPluginSources(dirName);
|
||||
@@ -189,14 +83,16 @@ function ensureConfiguredPluginsUpgraded(configuredChannels: string[]): void {
|
||||
|
||||
if (bundledDir) {
|
||||
const sourceVersion = readPluginVersion(join(bundledDir, 'package.json'));
|
||||
if (sourceVersion && installedVersion && sourceVersion !== installedVersion) {
|
||||
logger.info(`[plugin] Auto-upgrading ${channelType} plugin: ${installedVersion} → ${sourceVersion} (bundled)`);
|
||||
// Install or upgrade if version differs or plugin not installed
|
||||
if (!isInstalled || (sourceVersion && installedVersion && sourceVersion !== installedVersion)) {
|
||||
logger.info(`[plugin] ${isInstalled ? 'Auto-upgrading' : 'Installing'} ${channelType} plugin${isInstalled ? `: ${installedVersion} → ${sourceVersion}` : `: ${sourceVersion}`} (bundled)`);
|
||||
try {
|
||||
mkdirSync(join(homedir(), '.openclaw', 'extensions'), { recursive: true });
|
||||
rmSync(targetDir, { recursive: true, force: true });
|
||||
cpSync(bundledDir, targetDir, { recursive: true, dereference: true });
|
||||
fixupPluginManifest(targetDir);
|
||||
} catch (err) {
|
||||
logger.warn(`[plugin] Failed to auto-upgrade ${channelType} plugin:`, err);
|
||||
logger.warn(`[plugin] Failed to ${isInstalled ? 'auto-upgrade' : 'install'} ${channelType} plugin:`, err);
|
||||
}
|
||||
}
|
||||
continue;
|
||||
@@ -207,14 +103,17 @@ function ensureConfiguredPluginsUpgraded(configuredChannels: string[]): void {
|
||||
const npmPkgPath = join(process.cwd(), 'node_modules', ...npmName.split('/'));
|
||||
if (!existsSync(join(npmPkgPath, 'openclaw.plugin.json'))) continue;
|
||||
const sourceVersion = readPluginVersion(join(npmPkgPath, 'package.json'));
|
||||
if (!sourceVersion || !installedVersion || sourceVersion === installedVersion) continue;
|
||||
if (!sourceVersion) continue;
|
||||
// Skip only if installed AND same version
|
||||
if (isInstalled && installedVersion && sourceVersion === installedVersion) continue;
|
||||
|
||||
logger.info(`[plugin] Auto-upgrading ${channelType} plugin: ${installedVersion} → ${sourceVersion} (dev/node_modules)`);
|
||||
logger.info(`[plugin] ${isInstalled ? 'Auto-upgrading' : 'Installing'} ${channelType} plugin${isInstalled ? `: ${installedVersion} → ${sourceVersion}` : `: ${sourceVersion}`} (dev/node_modules)`);
|
||||
try {
|
||||
mkdirSync(join(homedir(), '.openclaw', 'extensions'), { recursive: true });
|
||||
copyPluginFromNodeModules(npmPkgPath, targetDir, npmName);
|
||||
fixupPluginManifest(targetDir);
|
||||
} catch (err) {
|
||||
logger.warn(`[plugin] Failed to auto-upgrade ${channelType} plugin from node_modules:`, err);
|
||||
logger.warn(`[plugin] Failed to ${isInstalled ? 'auto-upgrade' : 'install'} ${channelType} plugin from node_modules:`, err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user