Skip to content

Commit 94c7cd9

Browse files
committed
Look for node module in upper dir
1 parent 9bb3f8a commit 94c7cd9

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

src/bin/only-include-used-icons.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as fs from "fs";
55
import { join as pathJoin } from "path";
66
import { assert } from "tsafe/assert";
77
import { exclude } from "tsafe/exclude";
8-
import { writeFile, readFile, rm } from "fs/promises";
8+
import { writeFile, readFile, rm, access } from "fs/promises";
99
import { crawl } from "./tools/crawl";
1010
import { basename as pathBasename, sep as pathSep, dirname as pathDirname } from "path";
1111
import type { Equals } from "tsafe";
@@ -100,7 +100,29 @@ async function main() {
100100
const dsfrDistDirPath =
101101
getProjectRoot() === cwd
102102
? pathJoin(cwd, "dist", "dsfr")
103-
: pathJoin(...[cwd, "node_modules", ...packageName.split("/"), "dsfr"]);
103+
: await (async function callee(n: number): Promise<string> {
104+
if (n >= cwd.split(pathSep).length) {
105+
throw new Error("Need to install node modules?");
106+
}
107+
108+
const dirPath = pathJoin(
109+
...[
110+
cwd,
111+
...new Array(n).fill(".."),
112+
"node_modules",
113+
...packageName.split("/"),
114+
"dsfr"
115+
]
116+
);
117+
118+
try {
119+
await access(dirPath);
120+
} catch {
121+
return callee(n + 1);
122+
}
123+
124+
return dirPath;
125+
})(0);
104126

105127
const icons: Icon[] = JSON.parse(
106128
(await readFile(pathJoin(dsfrDistDirPath, pathOfIconsJson))).toString("utf8")

0 commit comments

Comments
 (0)