@ms-cloudpack/path-utilities 2.1.2 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/findPackageRoot.d.ts
CHANGED
|
@@ -2,5 +2,7 @@
|
|
|
2
2
|
* Given an absolute folder path into a package, traverse up the paths
|
|
3
3
|
* until the root has been found (containing the package.json file).
|
|
4
4
|
* Returns undefined if no package root is found after traversing to the filesystem root.
|
|
5
|
+
* @param startFolder - Absolute path to the folder to start searching from.
|
|
6
|
+
* @param excludeNodeModules - If true, don't return a path that contains a `node_modules` segment.
|
|
5
7
|
*/
|
|
6
|
-
export declare function findPackageRoot(
|
|
8
|
+
export declare function findPackageRoot(startFolder: string, excludeNodeModules?: boolean): string | undefined;
|
package/lib/findPackageRoot.js
CHANGED
|
@@ -1,18 +1,25 @@
|
|
|
1
1
|
import fs from 'fs';
|
|
2
2
|
import path from 'path';
|
|
3
|
+
const nodeModulesRegex = /[\\/]node_modules($|[\\/])/;
|
|
3
4
|
/**
|
|
4
5
|
* Given an absolute folder path into a package, traverse up the paths
|
|
5
6
|
* until the root has been found (containing the package.json file).
|
|
6
7
|
* Returns undefined if no package root is found after traversing to the filesystem root.
|
|
8
|
+
* @param startFolder - Absolute path to the folder to start searching from.
|
|
9
|
+
* @param excludeNodeModules - If true, don't return a path that contains a `node_modules` segment.
|
|
7
10
|
*/
|
|
8
|
-
export function findPackageRoot(
|
|
9
|
-
const root = path.parse(
|
|
10
|
-
|
|
11
|
-
|
|
11
|
+
export function findPackageRoot(startFolder, excludeNodeModules) {
|
|
12
|
+
const root = path.parse(startFolder).root;
|
|
13
|
+
let currentFolder = startFolder;
|
|
14
|
+
if (excludeNodeModules && nodeModulesRegex.test(startFolder)) {
|
|
15
|
+
currentFolder = startFolder.split(nodeModulesRegex)[0];
|
|
16
|
+
}
|
|
17
|
+
while (!fs.existsSync(path.join(currentFolder, 'package.json'))) {
|
|
18
|
+
if (currentFolder === root) {
|
|
12
19
|
return undefined;
|
|
13
20
|
}
|
|
14
|
-
|
|
21
|
+
currentFolder = path.dirname(currentFolder);
|
|
15
22
|
}
|
|
16
|
-
return
|
|
23
|
+
return currentFolder;
|
|
17
24
|
}
|
|
18
25
|
//# sourceMappingURL=findPackageRoot.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"findPackageRoot.js","sourceRoot":"","sources":["../src/findPackageRoot.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB
|
|
1
|
+
{"version":3,"file":"findPackageRoot.js","sourceRoot":"","sources":["../src/findPackageRoot.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB,MAAM,gBAAgB,GAAG,4BAA4B,CAAC;AAEtD;;;;;;GAMG;AACH,MAAM,UAAU,eAAe,CAAC,WAAmB,EAAE,kBAA4B;IAC/E,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC,IAAI,CAAC;IAC1C,IAAI,aAAa,GAAG,WAAW,CAAC;IAChC,IAAI,kBAAkB,IAAI,gBAAgB,CAAC,IAAI,CAAC,WAAW,CAAC,EAAE;QAC5D,aAAa,GAAG,WAAW,CAAC,KAAK,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAC,CAAC;KACxD;IAED,OAAO,CAAC,EAAE,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,aAAa,EAAE,cAAc,CAAC,CAAC,EAAE;QAC/D,IAAI,aAAa,KAAK,IAAI,EAAE;YAC1B,OAAO,SAAS,CAAC;SAClB;QACD,aAAa,GAAG,IAAI,CAAC,OAAO,CAAC,aAAa,CAAC,CAAC;KAC7C;IAED,OAAO,aAAa,CAAC;AACvB,CAAC","sourcesContent":["import fs from 'fs';\nimport path from 'path';\n\nconst nodeModulesRegex = /[\\\\/]node_modules($|[\\\\/])/;\n\n/**\n * Given an absolute folder path into a package, traverse up the paths\n * until the root has been found (containing the package.json file).\n * Returns undefined if no package root is found after traversing to the filesystem root.\n * @param startFolder - Absolute path to the folder to start searching from.\n * @param excludeNodeModules - If true, don't return a path that contains a `node_modules` segment.\n */\nexport function findPackageRoot(startFolder: string, excludeNodeModules?: boolean): string | undefined {\n const root = path.parse(startFolder).root;\n let currentFolder = startFolder;\n if (excludeNodeModules && nodeModulesRegex.test(startFolder)) {\n currentFolder = startFolder.split(nodeModulesRegex)[0];\n }\n\n while (!fs.existsSync(path.join(currentFolder, 'package.json'))) {\n if (currentFolder === root) {\n return undefined;\n }\n currentFolder = path.dirname(currentFolder);\n }\n\n return currentFolder;\n}\n"]}
|