Datasets:
| ; | |
| /** | |
| * config.js — Nomad scripts/ shared config resolution layer. | |
| * | |
| * Everything in scripts/ resolves paths through this file — never a literal | |
| * absolute path baked into a script (Nomad `team-skeleton.md` §5, hardcoding | |
| * points #1/#2/#7). Two things are located purely by directory *structure*, | |
| * never by a specific directory *name*, so this works regardless of whether | |
| * a deployment names its folders in English, Chinese, or anything else | |
| * (`team-skeleton.md` hardcoding point #5): | |
| * | |
| * - team root = nearest ancestor directory (starting from cwd) containing | |
| * `team-config.json`. | |
| * - agent home = nearest ancestor directory (starting from cwd) containing | |
| * `AGENTS.md`. | |
| * | |
| * This deliberately does NOT replicate the production hooks-core approach of | |
| * regex-matching a hardcoded parent-folder name (e.g. `<agent-parent-dir>\(name)`) | |
| * to infer the agent home. That approach only works because the production | |
| * deployment's folder name is a known constant; a portable template cannot | |
| * assume any particular folder name exists at all — see design note in | |
| * scripts/README.md. | |
| * | |
| * Resolution order for every tunable value: environment variable > | |
| * `team-config.json` > safe built-in fallback (§3 rule, hardcoding point #2). | |
| */ | |
| const fs = require('fs'); | |
| const path = require('path'); | |
| const CONFIG_FILENAME = 'team-config.json'; | |
| const AGENT_MARKER_FILENAME = 'AGENTS.md'; | |
| /** Strip a leading UTF-8 BOM if present (team-skeleton.md §3 rule — Windows editors routinely add one). */ | |
| function stripBOM(s) { | |
| if (typeof s === 'string' && s.charCodeAt(0) === 0xfeff) return s.slice(1); | |
| return s; | |
| } | |
| function readTextFileStripBOM(filePath) { | |
| return stripBOM(fs.readFileSync(filePath, 'utf8')); | |
| } | |
| function readJsonFile(filePath) { | |
| return JSON.parse(readTextFileStripBOM(filePath)); | |
| } | |
| /** | |
| * Walk upward from startDir (inclusive) until predicate(dir) is true. | |
| * Returns the matching absolute directory path, or null if it reaches the | |
| * filesystem root without a match. Never throws on a missing directory. | |
| */ | |
| function findUp(startDir, predicate) { | |
| let dir = path.resolve(startDir); | |
| // eslint-disable-next-line no-constant-condition | |
| while (true) { | |
| try { | |
| if (predicate(dir)) return dir; | |
| } catch (e) { | |
| /* a broken predicate should not crash resolution — treat as no-match and keep climbing */ | |
| } | |
| const parent = path.dirname(dir); | |
| if (parent === dir) return null; // reached filesystem root | |
| dir = parent; | |
| } | |
| } | |
| function hasFile(dir, fileName) { | |
| try { | |
| return fs.existsSync(path.join(dir, fileName)); | |
| } catch (e) { | |
| return false; | |
| } | |
| } | |
| /** | |
| * Team root resolution: env `NOMAD_TEAM_ROOT` (validated — must actually contain | |
| * team-config.json, otherwise ignored, not blindly trusted) > walk up from cwd | |
| * looking for team-config.json > null. | |
| */ | |
| function resolveTeamRoot(cwd) { | |
| const envRoot = process.env.NOMAD_TEAM_ROOT; | |
| if (envRoot && hasFile(envRoot, CONFIG_FILENAME)) { | |
| return path.resolve(envRoot); | |
| } | |
| return findUp(cwd || process.cwd(), (d) => hasFile(d, CONFIG_FILENAME)); | |
| } | |
| /** | |
| * Agent home resolution: nearest ancestor of cwd containing AGENTS.md. | |
| * No env override — an agent's home is a structural fact about where the | |
| * script is actually running, not a value worth overriding for a single run | |
| * (unlike team root, which selftest.js legitimately wants to redirect). | |
| */ | |
| function resolveAgentHome(cwd) { | |
| return findUp(cwd || process.cwd(), (d) => hasFile(d, AGENT_MARKER_FILENAME)); | |
| } | |
| /** Load team-config.json from a resolved team root. Returns null (not throw) on any failure. */ | |
| function loadTeamConfig(teamRoot) { | |
| if (!teamRoot) return null; | |
| const p = path.join(teamRoot, CONFIG_FILENAME); | |
| if (!fs.existsSync(p)) return null; | |
| try { | |
| return readJsonFile(p); | |
| } catch (e) { | |
| return null; | |
| } | |
| } | |
| /** | |
| * Language resolution: env `NOMAD_LANG` (must be 'en' or 'zh-CN') > | |
| * config.principal.language (any 'zh*' value normalizes to 'zh-CN', else 'en') > | |
| * 'en' fallback. English-default-with-config-override, per this batch's brief — | |
| * a portable template should not assume Chinese as the silent default. | |
| */ | |
| function resolveLanguage(config) { | |
| const envLang = process.env.NOMAD_LANG; | |
| if (envLang === 'en' || envLang === 'zh-CN') return envLang; | |
| const raw = config && config.principal && config.principal.language; | |
| if (typeof raw === 'string' && raw.trim()) { | |
| return raw.trim().toLowerCase().startsWith('zh') ? 'zh-CN' : 'en'; | |
| } | |
| return 'en'; | |
| } | |
| /** Resolve one of config.shared_paths[key] (a team-root-relative path) to an absolute path. Null if unresolvable. */ | |
| function resolveSharedPath(teamRoot, config, key) { | |
| if (!teamRoot || !config || !config.shared_paths) return null; | |
| const rel = config.shared_paths[key]; | |
| if (!rel || typeof rel !== 'string') return null; | |
| // shared_paths values are documented as forward-slash-or-native relative paths in team-config.json; | |
| // split on both separators and rejoin with path.join so this works regardless of which the installer typed. | |
| const parts = rel.split(/[\\/]+/).filter(Boolean); | |
| return path.join(teamRoot, ...parts); | |
| } | |
| /** | |
| * Return the first candidate (joined onto baseDir) that exists on disk, or null. | |
| * Used for structural-but-name-agnostic lookups this package intentionally does | |
| * NOT add new team-config.json fields for (e.g. the memory/ dir might be named | |
| * `记忆` or `memory` — see scripts/README.md "naming agnosticism" note). | |
| */ | |
| function firstExisting(baseDir, candidates) { | |
| if (!baseDir) return null; | |
| for (const c of candidates) { | |
| const p = path.join(baseDir, c); | |
| if (fs.existsSync(p)) return p; | |
| } | |
| return null; | |
| } | |
| module.exports = { | |
| CONFIG_FILENAME, | |
| AGENT_MARKER_FILENAME, | |
| stripBOM, | |
| readTextFileStripBOM, | |
| readJsonFile, | |
| findUp, | |
| resolveTeamRoot, | |
| resolveAgentHome, | |
| loadTeamConfig, | |
| resolveLanguage, | |
| resolveSharedPath, | |
| firstExisting, | |
| }; | |