1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.scanForPackage = exports.tryResolvePackage = void 0;
|
4 | const fs = require("fs");
|
5 | const path = require("path");
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | function tryResolvePackage(possiblePaths, lookupPaths) {
|
11 | for (const pkg of possiblePaths) {
|
12 | try {
|
13 |
|
14 |
|
15 | const possiblePath = require.resolve(`${pkg}/package.json`, (lookupPaths === null || lookupPaths === void 0 ? void 0 : lookupPaths.length) ? { paths: lookupPaths } : undefined);
|
16 | if (fs.existsSync(possiblePath)) {
|
17 | return path.dirname(possiblePath);
|
18 | }
|
19 | }
|
20 | catch (_a) {
|
21 |
|
22 | }
|
23 | }
|
24 | }
|
25 | exports.tryResolvePackage = tryResolvePackage;
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | function scanForPackage(possiblePaths, startDir = __dirname) {
|
31 |
|
32 | let curDir = path.join(startDir, "../node_modules");
|
33 | while (true) {
|
34 | for (const pkg of possiblePaths) {
|
35 | const possiblePath = path.join(curDir, pkg, "package.json");
|
36 | try {
|
37 |
|
38 | if (fs.existsSync(possiblePath) &&
|
39 | JSON.parse(fs.readFileSync(possiblePath, "utf8")).name ===
|
40 | pkg.toLowerCase()) {
|
41 | return path.dirname(possiblePath);
|
42 | }
|
43 | }
|
44 | catch (_a) {
|
45 |
|
46 | }
|
47 | }
|
48 |
|
49 | const parentDir = path.dirname(curDir);
|
50 | if (parentDir === curDir) {
|
51 |
|
52 | break;
|
53 | }
|
54 | curDir = parentDir;
|
55 | }
|
56 | }
|
57 | exports.scanForPackage = scanForPackage;
|