Skip to content

Commit 60a5a55

Browse files
committed
Search for icons in libraries using the DSFR
1 parent 5e22f22 commit 60a5a55

File tree

5 files changed

+61
-11
lines changed

5 files changed

+61
-11
lines changed

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

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { assert } from "tsafe/assert";
77
import { exclude } from "tsafe/exclude";
88
import { writeFile, readFile, rm } from "fs/promises";
99
import { crawl } from "./tools/crawl";
10-
import { basename as pathBasename } from "path";
10+
import { basename as pathBasename, sep as pathSep } from "path";
1111
import type { Equals } from "tsafe";
1212

1313
export const pathOfIconsJson = pathJoin("utility", "icons", "icons.json");
@@ -109,14 +109,60 @@ async function main() {
109109
const candidateFilePaths = (
110110
await crawl({
111111
"dirPath": cwd,
112-
"getDoCrawlInDir": ({ relativeDirPath }) => {
113-
const dirBasename = pathBasename(relativeDirPath);
112+
"getDoCrawlInDir": async ({ relativeDirPath }) => {
113+
if (relativeDirPath === "node_modules") {
114+
return true;
115+
}
116+
117+
if (
118+
relativeDirPath.startsWith(`node_modules${pathSep}@`) &&
119+
relativeDirPath.split(pathSep).length === 2
120+
) {
121+
return true;
122+
}
123+
124+
if (
125+
relativeDirPath.startsWith("node_modules") &&
126+
(relativeDirPath.split(pathSep).length === 2 ||
127+
(relativeDirPath.startsWith(`node_modules${pathSep}@`) &&
128+
relativeDirPath.split(pathSep).length === 3))
129+
) {
130+
const parsedPackageJson = await readFile(
131+
pathJoin(relativeDirPath, "package.json")
132+
).then(
133+
buff => JSON.parse(buff.toString("utf8")),
134+
() => undefined
135+
);
136+
137+
if (parsedPackageJson === undefined) {
138+
return false;
139+
}
140+
141+
if (
142+
Object.keys({
143+
...parsedPackageJson["dependencies"],
144+
...parsedPackageJson["devDependencies"]
145+
}).includes("@gouvfr/dsfr")
146+
) {
147+
return true;
148+
}
149+
150+
return false;
151+
}
152+
153+
if (relativeDirPath === `public${pathSep}dsfr`) {
154+
return false;
155+
}
156+
157+
if (pathBasename(relativeDirPath) === "generatedFromCss") {
158+
return false;
159+
}
114160

115-
if (dirBasename === "node_modules") {
161+
if (pathBasename(relativeDirPath) === "node_modules") {
116162
return false;
117163
}
118164

119-
if (dirBasename.startsWith(".")) {
165+
if (pathBasename(relativeDirPath).startsWith(".")) {
120166
return false;
121167
}
122168

src/bin/tools/crawl.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { readdir, lstat } from "fs/promises";
22
import { join as pathJoin, relative as pathRelative } from "path";
3+
import { realpath as fsRealpath } from "fs/promises";
34

45
async function crawlRec(params: {
56
dirPath: string;
@@ -13,11 +14,11 @@ async function crawlRec(params: {
1314

1415
await Promise.all(
1516
(
16-
await readdir(dirPath)
17+
await readdir(await fsRealpath(dirPath))
1718
).map(async fileOrDirectoryBasename => {
1819
const fileOrDirectoryPath = pathJoin(dirPath, fileOrDirectoryBasename);
1920

20-
if ((await lstat(fileOrDirectoryPath)).isDirectory()) {
21+
if ((await lstat(await fsRealpath(fileOrDirectoryPath))).isDirectory()) {
2122
const dirPath = fileOrDirectoryPath;
2223

2324
if (!(await getDoCrawlInDir({ dirPath }))) {

src/global.d.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
declare module "@gouvfr/dsfr/dist/dsfr.module" {}
2-
31
declare module "*.svg" {
42
const _default: string | { src: string };
53
export default _default;

src/lib/start.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export async function startDsfrReact(params: Params) {
7171

7272
(window as any).dsfr = { verbose, "mode": "manual" };
7373

74-
await import("../dsfr/dsfr.module" as any as "@gouvfr/dsfr/dist/dsfr.module");
74+
await import("../dsfr/dsfr.module" as any);
7575

7676
if (isNextJsDevEnvironnement) {
7777
// NOTE: @gouvfr/dsfr/dist/dsfr.module.js is not isomorphic, it can't run on the Server.",

test/integration/vite/vite.config.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,10 @@ import react from "@vitejs/plugin-react";
33

44
// https://vitejs.dev/config/
55
export default defineConfig({
6-
plugins: [react()]
6+
plugins: [react()],
7+
server: {
8+
fs: {
9+
allow: ['../../..']
10+
}
11+
}
712
});

0 commit comments

Comments
 (0)