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.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';
17/** Wraps `find-yarn-workspace-root` and guards against having an empty `package.json` file in an upper directory. */
18function findYarnOrNpmWorkspaceRoot(projectRoot) {
19    try {
20        return (0, find_yarn_workspace_root_1.default)(projectRoot);
21    }
22    catch (error) {
23        if (error.message.includes('Unexpected end of JSON input')) {
24            return null;
25        }
26        throw error;
27    }
28}
29exports.findYarnOrNpmWorkspaceRoot = findYarnOrNpmWorkspaceRoot;
30/**
31 * Find the `pnpm-workspace.yaml` file that represents the root of the monorepo.
32 * This is a synchronous function based on the original async library.
33 * @see https://github.com/pnpm/pnpm/blob/main/packages/find-workspace-dir/src/index.ts
34 */
35function findPnpmWorkspaceRoot(projectRoot) {
36    const workspaceEnvName = 'NPM_CONFIG_WORKSPACE_DIR';
37    const workspaceEnvValue = process.env[workspaceEnvName] ?? process.env[workspaceEnvName.toLowerCase()];
38    const workspaceFile = workspaceEnvValue
39        ? path_1.default.join(workspaceEnvValue, exports.PNPM_WORKSPACE_FILE)
40        : (0, find_up_1.sync)(exports.PNPM_WORKSPACE_FILE, { cwd: projectRoot });
41    if (!workspaceFile || !fs_1.default.existsSync(workspaceFile)) {
42        return null;
43    }
44    try {
45        // See: https://pnpm.io/pnpm-workspace_yaml
46        const { packages: workspaces } = js_yaml_1.default.load(fs_1.default.readFileSync(workspaceFile, 'utf8'));
47        // See: https://github.com/square/find-yarn-workspace-root/blob/11f6e31d3fa15a5bb7b7419f0091390e4c16204c/index.js#L26-L33
48        const workspaceRoot = path_1.default.dirname(workspaceFile);
49        const relativePath = path_1.default.relative(workspaceRoot, projectRoot);
50        if (relativePath === '' || (0, micromatch_1.default)([relativePath], workspaces).length > 0) {
51            return workspaceRoot;
52        }
53    }
54    catch {
55        // TODO: implement debug logger?
56        return null;
57    }
58    return null;
59}
60exports.findPnpmWorkspaceRoot = findPnpmWorkspaceRoot;
61//# sourceMappingURL=nodeWorkspaces.js.map