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.isUsingNpm = exports.isUsingYarn = exports.resolvePackageManager = exports.findWorkspaceRoot = exports.findYarnOrNpmWorkspaceRootSafe = exports.managerResolutionOrder = 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 = require("fs");
10const path_1 = __importDefault(require("path"));
11exports.NPM_LOCK_FILE = 'package-lock.json';
12exports.YARN_LOCK_FILE = 'yarn.lock';
13exports.PNPM_LOCK_FILE = 'pnpm-lock.yaml';
14exports.PNPM_WORKSPACE_FILE = 'pnpm-workspace.yaml';
15exports.managerResolutionOrder = ['yarn', 'npm', 'pnpm'];
16/**
17 * Find the `pnpm-workspace.yaml` file that represents the root of the monorepo.
18 * This is a synchronous function based on the original async library.
19 * @see https://github.com/pnpm/pnpm/blob/main/packages/find-workspace-dir/src/index.ts
20 */
21function findPnpmWorkspaceRoot(projectRoot) {
22    const workspaceEnvName = 'NPM_CONFIG_WORKSPACE_DIR';
23    const workspaceEnvValue = process.env[workspaceEnvName] ?? process.env[workspaceEnvName.toLowerCase()];
24    const manifestLocation = workspaceEnvValue
25        ? path_1.default.join(workspaceEnvValue, exports.PNPM_WORKSPACE_FILE)
26        : (0, find_up_1.sync)(exports.PNPM_WORKSPACE_FILE, { cwd: projectRoot });
27    return manifestLocation ? path_1.default.dirname(manifestLocation) : null;
28}
29/** Wraps `findYarnOrNpmWorkspaceRoot` and guards against having an empty `package.json` file in an upper directory. */
30function findYarnOrNpmWorkspaceRootSafe(projectRoot) {
31    try {
32        return (0, find_yarn_workspace_root_1.default)(projectRoot);
33    }
34    catch (error) {
35        if (error.message.includes('Unexpected end of JSON input')) {
36            return null;
37        }
38        throw error;
39    }
40}
41exports.findYarnOrNpmWorkspaceRootSafe = findYarnOrNpmWorkspaceRootSafe;
42/**
43 * Resolve the workspace root for a project, if its part of a monorepo.
44 * Optionally, provide a specific packager to only resolve that one specifically.
45 *
46 * By default, this tries to resolve the workspaces in order of:
47 *  - npm
48 *  - yarn
49 *  - pnpm
50 */
51function findWorkspaceRoot(projectRoot, packageManager) {
52    const strategies = {
53        npm: findYarnOrNpmWorkspaceRootSafe,
54        yarn: findYarnOrNpmWorkspaceRootSafe,
55        pnpm: findPnpmWorkspaceRoot,
56    };
57    if (packageManager) {
58        return strategies[packageManager](projectRoot);
59    }
60    for (const strategy of exports.managerResolutionOrder.map((name) => strategies[name])) {
61        const root = strategy(projectRoot);
62        if (root) {
63            return root;
64        }
65    }
66    return null;
67}
68exports.findWorkspaceRoot = findWorkspaceRoot;
69/**
70 * Resolve the used node package manager for a project by checking the lockfile.
71 * This also tries to resolve the workspace root, if its part of a monorepo.
72 * Optionally, provide a specific packager to only resolve that one specifically.
73 *
74 * By default, this tries to resolve the workspaces in order of:
75 *  - npm
76 *  - yarn
77 *  - pnpm
78 */
79function resolvePackageManager(projectRoot, packageManager) {
80    const workspaceRoot = findWorkspaceRoot(projectRoot, packageManager) || projectRoot;
81    const lockfileNames = {
82        npm: exports.NPM_LOCK_FILE,
83        yarn: exports.YARN_LOCK_FILE,
84        pnpm: exports.PNPM_LOCK_FILE,
85    };
86    if (packageManager) {
87        const lockfilePath = path_1.default.join(workspaceRoot, lockfileNames[packageManager]);
88        if ((0, fs_1.existsSync)(lockfilePath)) {
89            return packageManager;
90        }
91        return null;
92    }
93    for (const manager of exports.managerResolutionOrder) {
94        const lockfilePath = path_1.default.join(workspaceRoot, lockfileNames[manager]);
95        if ((0, fs_1.existsSync)(lockfilePath)) {
96            return manager;
97        }
98    }
99    return null;
100}
101exports.resolvePackageManager = resolvePackageManager;
102/**
103 * Returns true if the project is using yarn, false if the project is using another package manager.
104 */
105function isUsingYarn(projectRoot) {
106    return !!resolvePackageManager(projectRoot, 'yarn');
107}
108exports.isUsingYarn = isUsingYarn;
109/**
110 * Returns true if the project is using npm, false if the project is using another package manager.
111 */
112function isUsingNpm(projectRoot) {
113    return !!resolvePackageManager(projectRoot, 'npm');
114}
115exports.isUsingNpm = isUsingNpm;
116//# sourceMappingURL=nodeWorkspaces.js.map