mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 18:10:45 +00:00
129 lines
3.8 KiB
JavaScript
129 lines
3.8 KiB
JavaScript
import { spawnSync } from "node:child_process";
|
|
import fs from "node:fs";
|
|
import path from "node:path";
|
|
import { readFlagValue } from "./arg-utils.mjs";
|
|
|
|
const CORE_TEST_CONFIGS = new Set([
|
|
"tsconfig.core.test.json",
|
|
"tsconfig.core.test.agents.json",
|
|
"tsconfig.core.test.non-agents.json",
|
|
]);
|
|
|
|
const CORE_PROD_CONFIGS = new Set(["tsconfig.core.json"]);
|
|
const TSGO_SPARSE_SKIP_ENV_KEY = "OPENCLAW_TSGO_SPARSE_SKIP";
|
|
|
|
const CORE_PROD_REQUIRED_PATHS = [
|
|
{
|
|
path: "apps/shared/OpenClawKit/Sources/OpenClawKit/Resources/tool-display.json",
|
|
whenPresent: "ui/src/ui/tool-display.ts",
|
|
},
|
|
{
|
|
path: "scripts/lib/bundled-runtime-sidecar-paths.json",
|
|
whenPresent: "src/plugins/runtime-sidecar-paths.ts",
|
|
},
|
|
{
|
|
path: "scripts/lib/official-external-channel-catalog.json",
|
|
whenPresent: "src/channels/plugins/catalog.ts",
|
|
},
|
|
{
|
|
path: "scripts/lib/plugin-sdk-entrypoints.json",
|
|
whenPresent: "src/plugin-sdk/entrypoints.ts",
|
|
},
|
|
];
|
|
|
|
const CORE_TEST_REQUIRED_PATHS = [
|
|
"packages/plugin-package-contract/src/index.ts",
|
|
"ui/src/i18n/lib/registry.ts",
|
|
"ui/src/i18n/lib/types.ts",
|
|
"ui/src/ui/app-settings.ts",
|
|
"ui/src/ui/gateway.ts",
|
|
];
|
|
|
|
export function shouldSkipSparseTsgoGuardError(env = process.env) {
|
|
const value = env[TSGO_SPARSE_SKIP_ENV_KEY]?.trim().toLowerCase();
|
|
return value === "1" || value === "true";
|
|
}
|
|
|
|
export function createSparseTsgoSkipEnv(baseEnv = process.env) {
|
|
return {
|
|
...baseEnv,
|
|
[TSGO_SPARSE_SKIP_ENV_KEY]: baseEnv[TSGO_SPARSE_SKIP_ENV_KEY]?.trim() || "1",
|
|
};
|
|
}
|
|
|
|
export function getSparseTsgoGuardError(
|
|
args,
|
|
{ cwd = process.cwd(), fileExists = fs.existsSync, isSparseCheckoutEnabled } = {},
|
|
) {
|
|
const projectPath = readProjectFlag(args);
|
|
const projectName = projectPath ? path.basename(projectPath) : null;
|
|
if (
|
|
!projectName ||
|
|
(!CORE_PROD_CONFIGS.has(projectName) && !CORE_TEST_CONFIGS.has(projectName)) ||
|
|
isMetadataOnlyCommand(args)
|
|
) {
|
|
return null;
|
|
}
|
|
|
|
const sparseEnabled =
|
|
isSparseCheckoutEnabled?.({ cwd }) ?? getGitBooleanConfig("core.sparseCheckout", { cwd });
|
|
if (!sparseEnabled) {
|
|
return null;
|
|
}
|
|
|
|
const missingPaths = getRequiredPathsForProject(projectName, cwd, fileExists).filter(
|
|
(relativePath) => !fileExists(path.join(cwd, relativePath)),
|
|
);
|
|
if (missingPaths.length === 0) {
|
|
return null;
|
|
}
|
|
|
|
return [
|
|
`${projectName} cannot be typechecked from this sparse checkout because tracked project inputs are missing:`,
|
|
...missingPaths.map((relativePath) => `- ${relativePath}`),
|
|
"Expand this worktree's sparse checkout to include those paths, or rerun in a full worktree.",
|
|
].join("\n");
|
|
}
|
|
|
|
function getRequiredPathsForProject(projectName, cwd, fileExists) {
|
|
const requiredPaths = [];
|
|
if (CORE_PROD_CONFIGS.has(projectName)) {
|
|
requiredPaths.push(...conditionalRequiredPaths(CORE_PROD_REQUIRED_PATHS, cwd, fileExists));
|
|
}
|
|
if (CORE_TEST_CONFIGS.has(projectName)) {
|
|
requiredPaths.push(...CORE_TEST_REQUIRED_PATHS);
|
|
}
|
|
return [...new Set(requiredPaths)].toSorted((left, right) => left.localeCompare(right));
|
|
}
|
|
|
|
function conditionalRequiredPaths(entries, cwd, fileExists) {
|
|
return entries
|
|
.filter((entry) => fileExists(path.join(cwd, entry.whenPresent)))
|
|
.map((entry) => entry.path);
|
|
}
|
|
|
|
function getGitBooleanConfig(name, { cwd }) {
|
|
const result = spawnSync("git", ["config", "--get", "--bool", name], {
|
|
cwd,
|
|
encoding: "utf8",
|
|
stdio: ["ignore", "pipe", "pipe"],
|
|
shell: process.platform === "win32",
|
|
});
|
|
|
|
if (result.error || (result.status ?? 1) !== 0) {
|
|
return false;
|
|
}
|
|
|
|
return (result.stdout ?? "").trim() === "true";
|
|
}
|
|
|
|
function readProjectFlag(args) {
|
|
return readFlagValue(args, "-p") ?? readFlagValue(args, "--project");
|
|
}
|
|
|
|
function isMetadataOnlyCommand(args) {
|
|
return args.some((arg) =>
|
|
["--help", "-h", "--version", "-v", "--init", "--showConfig"].includes(arg),
|
|
);
|
|
}
|