Skip to content

Commit 73e6b57

Browse files
committed
split paths package files
1 parent 5f51227 commit 73e6b57

File tree

5 files changed

+61
-48
lines changed

5 files changed

+61
-48
lines changed

packages/paths/package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,19 @@
22
"name": "@ws-tools/paths",
33
"version": "0.1.0",
44
"license": "MIT",
5+
"description": "Helper methods related to paths in a repo or monorepo",
56
"repository": {
67
"type": "git",
78
"url": "https://github.com/microsoft/workspace-tools"
89
},
910
"main": "lib/index.js",
1011
"types": "lib/index.d.ts",
12+
"exports": {
13+
".": {
14+
"require": "./lib/index.js",
15+
"types": "./lib/index.d.ts"
16+
}
17+
},
1118
"files": [
1219
"lib/!(__*)",
1320
"lib/!(__*)/**"

packages/paths/src/findGitRoot.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import path from "path";
2+
import { spawnSync } from "child_process";
3+
4+
/**
5+
* Starting from `cwd`, uses `git rev-parse --show-toplevel` to find the root of the git repo.
6+
* Throws if `cwd` is not in a Git repository.
7+
*/
8+
export function findGitRoot(cwd: string) {
9+
const result = spawnSync("git", ["rev-parse", "--show-toplevel"], { cwd });
10+
11+
if (result.status !== 0) {
12+
throw new Error(`Directory "${cwd}" is not in a git repository`);
13+
}
14+
15+
return path.normalize(result.stdout.toString().trim());
16+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import path from "path";
2+
import { searchUp } from "./searchUp";
3+
4+
/**
5+
* Starting from `cwd`, searches up the directory hierarchy for `package.json`.
6+
*/
7+
export function findPackageRoot(cwd: string) {
8+
const jsonPath = searchUp("package.json", cwd);
9+
return jsonPath && path.dirname(jsonPath);
10+
}

packages/paths/src/index.ts

Lines changed: 3 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,3 @@
1-
import path from "path";
2-
import fs from "fs";
3-
import { spawnSync } from "child_process";
4-
5-
/**
6-
* Starting from `cwd`, searches up the directory hierarchy for `filePath`.
7-
* If multiple strings are given, searches each directory level for any of them.
8-
* @returns Full path to the item found, or undefined if not found.
9-
*/
10-
export function searchUp(filePath: string | string[], cwd: string) {
11-
const paths = typeof filePath === "string" ? [filePath] : filePath;
12-
const root = path.parse(cwd).root;
13-
14-
let foundPath: string | undefined;
15-
16-
while (!foundPath && cwd !== root) {
17-
foundPath = paths.find((p) => fs.existsSync(path.join(cwd, p)));
18-
if (foundPath) {
19-
break;
20-
}
21-
22-
cwd = path.dirname(cwd);
23-
}
24-
25-
return foundPath ? path.join(cwd, foundPath) : undefined;
26-
}
27-
28-
/**
29-
* Starting from `cwd`, uses `git rev-parse --show-toplevel` to find the root of the git repo.
30-
* Throws if `cwd` is not in a Git repository.
31-
*/
32-
export function findGitRoot(cwd: string) {
33-
const result = spawnSync("git", ["rev-parse", "--show-toplevel"], { cwd });
34-
35-
if (result.status !== 0) {
36-
throw new Error(`Directory "${cwd}" is not in a git repository`);
37-
}
38-
39-
return path.normalize(result.stdout.toString().trim());
40-
}
41-
42-
/**
43-
* Starting from `cwd`, searches up the directory hierarchy for `package.json`.
44-
*/
45-
export function findPackageRoot(cwd: string) {
46-
const jsonPath = searchUp("package.json", cwd);
47-
return jsonPath && path.dirname(jsonPath);
48-
}
1+
export { findGitRoot } from "./findGitRoot";
2+
export { findPackageRoot } from "./findPackageRoot";
3+
export { searchUp } from "./searchUp";

packages/paths/src/searchUp.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import path from "path";
2+
import fs from "fs";
3+
4+
/**
5+
* Starting from `cwd`, searches up the directory hierarchy for `filePath`.
6+
* If multiple strings are given, searches each directory level for any of them.
7+
* @returns Full path to the item found, or undefined if not found.
8+
*/
9+
export function searchUp(filePath: string | string[], cwd: string) {
10+
const paths = typeof filePath === "string" ? [filePath] : filePath;
11+
const root = path.parse(cwd).root;
12+
13+
let foundPath: string | undefined;
14+
15+
while (!foundPath && cwd !== root) {
16+
foundPath = paths.find((p) => fs.existsSync(path.join(cwd, p)));
17+
if (foundPath) {
18+
break;
19+
}
20+
21+
cwd = path.dirname(cwd);
22+
}
23+
24+
return foundPath ? path.join(cwd, foundPath) : undefined;
25+
}

0 commit comments

Comments
 (0)