1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3    return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6exports.findPnpmWorkspaceRoot = exports.findYarnOrNpmWorkspaceRoot = exports.BUN_LOCK_FILE = exports.PNPM_WORKSPACE_FILE = exports.PNPM_LOCK_FILE = exports.YARN_LOCK_FILE = exports.NPM_LOCK_FILE = void 0;
7const find_up_1 = require("find-up");
8const find_yarn_workspace_root_1 = __importDefault(require("find-yarn-workspace-root"));
9const fs_1 = __importDefault(require("fs"));
10const js_yaml_1 = __importDefault(require("js-yaml"));
11const micromatch_1 = __importDefault(require("micromatch"));
12const path_1 = __importDefault(require("path"));
13exports.NPM_LOCK_FILE = 'package-lock.json';
14exports.YARN_LOCK_FILE = 'yarn.lock';
15exports.PNPM_LOCK_FILE = 'pnpm-lock.yaml';
16exports.PNPM_WORKSPACE_FILE = 'pnpm-workspace.yaml';
17exports.BUN_LOCK_FILE = 'bun.lockb';
18/** Wraps `find-yarn-workspace-root` and guards against having an empty `package.json` file in an upper directory. */
19function findYarnOrNpmWorkspaceRoot(projectRoot) {
20    try {
21        return (0, find_yarn_workspace_root_1.default)(projectRoot);
22    }
23    catch (error) {
24        if (error.message.includes('Unexpected end of JSON input')) {
25            return null;
26        }
27        throw error;
28    }
29}
30exports.findYarnOrNpmWorkspaceRoot = findYarnOrNpmWorkspaceRoot;
31/**
32 * Find the `pnpm-workspace.yaml` file that represents the root of the monorepo.
33 * This is a synchronous function based on the original async library.
34 * @see https://github.com/pnpm/pnpm/blob/main/packages/find-workspace-dir/src/index.ts
35 */
36function findPnpmWorkspaceRoot(projectRoot) {
37    const workspaceEnvName = 'NPM_CONFIG_WORKSPACE_DIR';
38    const workspaceEnvValue = process.env[workspaceEnvName] ?? process.env[workspaceEnvName.toLowerCase()];
39    const workspaceFile = workspaceEnvValue
40        ? path_1.default.join(workspaceEnvValue, exports.PNPM_WORKSPACE_FILE)
41        : (0, find_up_1.sync)(exports.PNPM_WORKSPACE_FILE, { cwd: projectRoot });
42    if (!workspaceFile || !fs_1.default.existsSync(workspaceFile)) {
43        return null;
44    }
45    try {
46        // See: https://pnpm.io/pnpm-workspace_yaml
47        const { packages: workspaces } = js_yaml_1.default.load(fs_1.default.readFileSync(workspaceFile, 'utf8'));
48        // See: https://github.com/square/find-yarn-workspace-root/blob/11f6e31d3fa15a5bb7b7419f0091390e4c16204c/index.js#L26-L33
49        const workspaceRoot = path_1.default.dirname(workspaceFile);
50        const relativePath = path_1.default.relative(workspaceRoot, projectRoot);
51        if (relativePath === '' || (0, micromatch_1.default)([relativePath], workspaces).length > 0) {
52            return workspaceRoot;
53        }
54    }
55    catch {
56        // TODO: implement debug logger?
57        return null;
58    }
59    return null;
60}
61exports.findPnpmWorkspaceRoot = findPnpmWorkspaceRoot;
62//# sourceMappingURL=nodeWorkspaces.js.map