zephyr-agent
Version:
Zephyr plugin agent
101 lines (100 loc) • 3.37 kB
JavaScript
import "node:module";
import { accessSync, constants } from "node:fs";
import { readFile } from "node:fs/promises";
import { join, resolve } from "node:path";
import { ze_log } from "../logging/debug.mjs";
const MONOREPO_CONFIG_FILES = [
{
file: 'pnpm-workspace.yaml',
type: 'pnpm'
},
{
file: 'pnpm-workspace.yml',
type: 'pnpm'
},
{
file: 'lerna.json',
type: 'lerna'
},
{
file: 'nx.json',
type: 'nx'
},
{
file: 'rush.json',
type: 'rush'
}
];
const MAX_TRAVERSAL_DEPTH = 10;
async function detectMonorepo(startPath) {
let currentDir = resolve(startPath);
let depth = 0;
while(depth < MAX_TRAVERSAL_DEPTH){
for (const config of MONOREPO_CONFIG_FILES){
const configPath = join(currentDir, config.file);
if (fileExists(configPath)) {
ze_log.misc(`Found ${config.type} monorepo config at ${configPath}`);
return {
type: config.type,
root: currentDir,
configFile: configPath
};
}
}
const packageJsonPath = join(currentDir, 'package.json');
if (fileExists(packageJsonPath)) try {
const content = await readFile(packageJsonPath, 'utf8');
const packageJson = JSON.parse(content);
if (packageJson.workspaces) {
ze_log.misc(`Found ${packageJson.packageManager?.startsWith('yarn') ? 'yarn' : 'npm'} workspaces at ${packageJsonPath}`);
return {
type: packageJson.packageManager?.startsWith('yarn') ? 'yarn' : 'npm',
root: currentDir,
configFile: packageJsonPath,
workspaces: Array.isArray(packageJson.workspaces) ? packageJson.workspaces : packageJson.workspaces.packages || []
};
}
const yarnrcPath = join(currentDir, '.yarnrc.yml');
if (fileExists(yarnrcPath)) {
ze_log.misc(`Found Yarn Berry workspace at ${currentDir}`);
return {
type: 'yarn',
root: currentDir,
configFile: yarnrcPath
};
}
} catch (error) {
ze_log.misc(`Error reading package.json at ${packageJsonPath}:`, error);
}
const parentDir = resolve(currentDir, '..');
if (parentDir === currentDir) break;
currentDir = parentDir;
depth++;
}
ze_log.misc('No monorepo configuration found');
return {
type: 'none',
root: startPath
};
}
async function getMonorepoRootPackageJson(monorepoRoot) {
const packageJsonPath = join(monorepoRoot, 'package.json');
if (!fileExists(packageJsonPath)) return null;
try {
const content = await readFile(packageJsonPath, 'utf8');
return JSON.parse(content);
} catch (error) {
ze_log.misc(`Error reading root package.json at ${packageJsonPath}:`, error);
return null;
}
}
function fileExists(path) {
try {
accessSync(path, constants.F_OK);
return true;
} catch {
return false;
}
}
export { detectMonorepo, getMonorepoRootPackageJson };
//# sourceMappingURL=detect-monorepo.mjs.map