-
-
Notifications
You must be signed in to change notification settings - Fork 622
/
Copy pathpath-utils.ts
30 lines (26 loc) · 1005 Bytes
/
path-utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import * as findup from "findup-sync";
import * as fs from "fs";
import * as path from "path";
/**
* Attempts to detect whether the string is a local path regardless of its
* existence by checking its format. The point is to distinguish between
* paths and modules on the npm registry. This will fail for non-existent
* local Windows paths that begin with a drive letter, e.g. C:..\generator.js,
* but will succeed for any existing files and any absolute paths.
*
* @param {String} str - string to check
* @returns {Boolean} whether the string could be a path to a local file or directory
*/
export function isLocalPath(str: string): boolean {
return path.isAbsolute(str) || /^\./.test(str) || fs.existsSync(str);
}
/**
* Find the root directory path of a project.
*
* @returns {String} Absolute path of the project root.
*/
export function findProjectRoot(): string {
const rootFilePath = findup("package.json");
const projectRoot = path.dirname(rootFilePath);
return projectRoot;
}