mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-08 18:33:39 +02:00
refactor: dedupe script and matrix send helpers
This commit is contained in:
@@ -39,11 +39,7 @@ export async function withResolvedMatrixSendClient<T>(
|
||||
},
|
||||
run: (client: MatrixClient) => Promise<T>,
|
||||
): Promise<T> {
|
||||
if (opts.client) {
|
||||
return await run(opts.client);
|
||||
}
|
||||
const { withResolvedRuntimeMatrixClient } = await loadMatrixSendClientRuntime();
|
||||
return await withResolvedRuntimeMatrixClient(
|
||||
return await withResolvedMatrixClient(
|
||||
{
|
||||
...opts,
|
||||
// One-off outbound sends still need a started client so room encryption
|
||||
@@ -66,11 +62,7 @@ export async function withResolvedMatrixControlClient<T>(
|
||||
},
|
||||
run: (client: MatrixClient) => Promise<T>,
|
||||
): Promise<T> {
|
||||
if (opts.client) {
|
||||
return await run(opts.client);
|
||||
}
|
||||
const { withResolvedRuntimeMatrixClient } = await loadMatrixSendClientRuntime();
|
||||
return await withResolvedRuntimeMatrixClient(
|
||||
return await withResolvedMatrixClient(
|
||||
{
|
||||
...opts,
|
||||
readiness: "none",
|
||||
@@ -78,3 +70,21 @@ export async function withResolvedMatrixControlClient<T>(
|
||||
run,
|
||||
);
|
||||
}
|
||||
|
||||
async function withResolvedMatrixClient<T>(
|
||||
opts: {
|
||||
client?: MatrixClient;
|
||||
cfg?: CoreConfig;
|
||||
timeoutMs?: number;
|
||||
accountId?: string | null;
|
||||
readiness: "started" | "none";
|
||||
},
|
||||
run: (client: MatrixClient) => Promise<T>,
|
||||
shutdownBehavior?: "persist",
|
||||
): Promise<T> {
|
||||
if (opts.client) {
|
||||
return await run(opts.client);
|
||||
}
|
||||
const { withResolvedRuntimeMatrixClient } = await loadMatrixSendClientRuntime();
|
||||
return await withResolvedRuntimeMatrixClient(opts, run, shutdownBehavior);
|
||||
}
|
||||
|
||||
37
scripts/lib/vitest-batch-runner.mjs
Normal file
37
scripts/lib/vitest-batch-runner.mjs
Normal file
@@ -0,0 +1,37 @@
|
||||
import { spawn } from "node:child_process";
|
||||
import path from "node:path";
|
||||
import { fileURLToPath, pathToFileURL } from "node:url";
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
const repoRoot = path.resolve(__dirname, "../..");
|
||||
const pnpm = "pnpm";
|
||||
|
||||
export async function runVitestBatch(params) {
|
||||
return await new Promise((resolve, reject) => {
|
||||
const child = spawn(
|
||||
pnpm,
|
||||
["exec", "vitest", "run", "--config", params.config, ...params.targets, ...params.args],
|
||||
{
|
||||
cwd: repoRoot,
|
||||
stdio: "inherit",
|
||||
shell: process.platform === "win32",
|
||||
env: params.env,
|
||||
},
|
||||
);
|
||||
|
||||
child.on("error", reject);
|
||||
child.on("exit", (code, signal) => {
|
||||
if (signal) {
|
||||
process.kill(process.pid, signal);
|
||||
return;
|
||||
}
|
||||
resolve(code ?? 1);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export function isDirectScriptRun(metaUrl) {
|
||||
const entryHref = process.argv[1] ? pathToFileURL(path.resolve(process.argv[1])).href : "";
|
||||
return metaUrl === entryHref;
|
||||
}
|
||||
@@ -1,38 +1,7 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import { spawn } from "node:child_process";
|
||||
import path from "node:path";
|
||||
import { fileURLToPath, pathToFileURL } from "node:url";
|
||||
import { resolveExtensionBatchPlan } from "./lib/extension-test-plan.mjs";
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
const repoRoot = path.resolve(__dirname, "..");
|
||||
const pnpm = "pnpm";
|
||||
|
||||
async function runVitestBatch(params) {
|
||||
return await new Promise((resolve, reject) => {
|
||||
const child = spawn(
|
||||
pnpm,
|
||||
["exec", "vitest", "run", "--config", params.config, ...params.targets, ...params.args],
|
||||
{
|
||||
cwd: repoRoot,
|
||||
stdio: "inherit",
|
||||
shell: process.platform === "win32",
|
||||
env: params.env,
|
||||
},
|
||||
);
|
||||
|
||||
child.on("error", reject);
|
||||
child.on("exit", (code, signal) => {
|
||||
if (signal) {
|
||||
process.kill(process.pid, signal);
|
||||
return;
|
||||
}
|
||||
resolve(code ?? 1);
|
||||
});
|
||||
});
|
||||
}
|
||||
import { isDirectScriptRun, runVitestBatch } from "./lib/vitest-batch-runner.mjs";
|
||||
|
||||
function printUsage() {
|
||||
console.error("Usage: pnpm test:extensions:batch <extension[,extension...]> [vitest args...]");
|
||||
@@ -98,8 +67,6 @@ async function run() {
|
||||
}
|
||||
}
|
||||
|
||||
const entryHref = process.argv[1] ? pathToFileURL(path.resolve(process.argv[1])).href : "";
|
||||
|
||||
if (import.meta.url === entryHref) {
|
||||
if (isDirectScriptRun(import.meta.url)) {
|
||||
await run();
|
||||
}
|
||||
|
||||
@@ -1,38 +1,7 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import { spawn } from "node:child_process";
|
||||
import path from "node:path";
|
||||
import { fileURLToPath, pathToFileURL } from "node:url";
|
||||
import { resolveExtensionTestPlan } from "./lib/extension-test-plan.mjs";
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
const repoRoot = path.resolve(__dirname, "..");
|
||||
const pnpm = "pnpm";
|
||||
|
||||
async function runVitestBatch(params) {
|
||||
return await new Promise((resolve, reject) => {
|
||||
const child = spawn(
|
||||
pnpm,
|
||||
["exec", "vitest", "run", "--config", params.config, ...params.targets, ...params.args],
|
||||
{
|
||||
cwd: repoRoot,
|
||||
stdio: "inherit",
|
||||
shell: process.platform === "win32",
|
||||
env: params.env,
|
||||
},
|
||||
);
|
||||
|
||||
child.on("error", reject);
|
||||
child.on("exit", (code, signal) => {
|
||||
if (signal) {
|
||||
process.kill(process.pid, signal);
|
||||
return;
|
||||
}
|
||||
resolve(code ?? 1);
|
||||
});
|
||||
});
|
||||
}
|
||||
import { isDirectScriptRun, runVitestBatch } from "./lib/vitest-batch-runner.mjs";
|
||||
|
||||
function printUsage() {
|
||||
console.error("Usage: pnpm test:extension <extension-name|path> [vitest args...]");
|
||||
@@ -81,8 +50,6 @@ async function run() {
|
||||
process.exit(exitCode);
|
||||
}
|
||||
|
||||
const entryHref = process.argv[1] ? pathToFileURL(path.resolve(process.argv[1])).href : "";
|
||||
|
||||
if (import.meta.url === entryHref) {
|
||||
if (isDirectScriptRun(import.meta.url)) {
|
||||
await run();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user