Skip to content

Commit 9f959fa

Browse files
committed
fixes #142
1 parent e8b9f55 commit 9f959fa

File tree

4 files changed

+208
-133
lines changed

4 files changed

+208
-133
lines changed

scripts/build/icons.ts

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -113,23 +113,22 @@ export async function collectIcons(params: {
113113
})
114114
.filter(exclude(undefined));
115115

116-
const remixiconIcons = await (async () => {
117-
const iconDirPath = pathJoin(remixiconDirPath, "icons");
118-
119-
return Promise.all(
120-
(await crawl({ "dirPath": iconDirPath }))
121-
.filter(filePath => filePath.endsWith(".svg"))
122-
.map(async svgFilePath =>
123-
id<Icon.Remixicon>({
124-
"prefix": "ri-",
125-
"iconId": pathBasename(svgFilePath).replace(/\.svg$/, ""),
126-
"rawSvgCode": (await readFile(pathJoin(iconDirPath, svgFilePath))).toString(
127-
"utf8"
128-
)
129-
})
130-
)
131-
);
132-
})();
116+
const remixiconIcons = await Promise.all(
117+
(
118+
await crawl({
119+
"dirPath": pathJoin(remixiconDirPath, "icons"),
120+
"returnedPathsType": "absolute"
121+
})
122+
)
123+
.filter(filePath => filePath.endsWith(".svg"))
124+
.map(async svgFilePath =>
125+
id<Icon.Remixicon>({
126+
"prefix": "ri-",
127+
"iconId": pathBasename(svgFilePath).replace(/\.svg$/, ""),
128+
"rawSvgCode": (await readFile(svgFilePath)).toString("utf8")
129+
})
130+
)
131+
);
133132

134133
return [...dsfrIcons, ...remixiconIcons];
135134
}

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

Lines changed: 183 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -91,130 +91,201 @@ export function generateIconsRawCssCode(params: {
9191
}
9292

9393
async function main() {
94-
const packageName = JSON.parse(
94+
const codegouvfrReactDsfr: string = JSON.parse(
9595
fs.readFileSync(pathJoin(getProjectRoot(), "package.json")).toString("utf8")
9696
)["name"];
9797

9898
const cwd = process.cwd();
9999

100-
const dsfrDistDirPath =
101-
getProjectRoot() === cwd
102-
? pathJoin(cwd, "dist", "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);
100+
const isCwdReactDsfr = pathJoin(getProjectRoot(), "..") === cwd;
101+
102+
const dsfrDistDirPath = isCwdReactDsfr
103+
? pathJoin(cwd, "dist", "dsfr")
104+
: await (async function callee(n: number): Promise<string> {
105+
if (n >= cwd.split(pathSep).length) {
106+
throw new Error("Need to install node modules?");
107+
}
108+
109+
const dirPath = pathJoin(
110+
...[
111+
cwd,
112+
...new Array(n).fill(".."),
113+
"node_modules",
114+
...codegouvfrReactDsfr.split("/"),
115+
"dsfr"
116+
]
117+
);
118+
119+
try {
120+
await access(dirPath);
121+
} catch {
122+
return callee(n + 1);
123+
}
124+
125+
return dirPath;
126+
})(0);
126127

127128
const icons: Icon[] = JSON.parse(
128129
(await readFile(pathJoin(dsfrDistDirPath, pathOfIconsJson))).toString("utf8")
129130
);
130131

131132
const { usedIconClassNames } = await (async function getUsedIconClassNames() {
132133
const candidateFilePaths = (
133-
await crawl({
134-
"dirPath": cwd,
135-
"getDoCrawlInDir": async ({ relativeDirPath }) => {
136-
if (relativeDirPath === "node_modules") {
137-
return true;
138-
}
139-
140-
if (
141-
relativeDirPath.startsWith(`node_modules${pathSep}@`) &&
142-
relativeDirPath.split(pathSep).length === 2
143-
) {
144-
return true;
145-
}
146-
147-
if (
148-
relativeDirPath.startsWith("node_modules") &&
149-
(relativeDirPath.split(pathSep).length === 2 ||
150-
(relativeDirPath.startsWith(`node_modules${pathSep}@`) &&
151-
relativeDirPath.split(pathSep).length === 3))
152-
) {
153-
const parsedPackageJson = await readFile(
154-
pathJoin(relativeDirPath, "package.json")
155-
).then(
156-
buff => JSON.parse(buff.toString("utf8")),
157-
() => undefined
158-
);
159-
160-
if (parsedPackageJson === undefined) {
161-
return false;
162-
}
163-
164-
if (parsedPackageJson["name"] === "tss-react") {
165-
return false;
166-
}
167-
168-
for (const packageName of [
169-
"@gouvfr/dsfr",
170-
"@codegouvfr/react-dsfr",
171-
"@dataesr/react-dsfr"
172-
]) {
173-
if (
174-
Object.keys({
175-
...parsedPackageJson["dependencies"],
176-
...parsedPackageJson["devDependencies"],
177-
...parsedPackageJson["peerDependencies"]
178-
}).includes(packageName)
179-
) {
180-
return true;
181-
}
182-
}
183-
184-
return false;
185-
}
186-
187-
if (relativeDirPath === `public${pathSep}dsfr`) {
188-
return false;
189-
}
190-
191-
if (pathBasename(relativeDirPath) === "generatedFromCss") {
192-
return false;
193-
}
194-
195-
if (
196-
pathDirname(relativeDirPath).endsWith(pathJoin(...packageName.split("/")))
197-
) {
198-
return pathBasename(relativeDirPath) === "src";
199-
}
200-
201-
if (pathBasename(relativeDirPath) === "node_modules") {
202-
return false;
203-
}
204-
205-
if (pathBasename(relativeDirPath).startsWith(".")) {
206-
return false;
207-
}
208-
209-
return true;
210-
}
211-
})
212-
).filter(
213-
filePath =>
214-
["tsx", "jsx", "js", "ts", "html", "htm"].find(ext =>
215-
filePath.endsWith(`.${ext}`)
216-
) !== undefined
217-
);
134+
await Promise.all(
135+
isCwdReactDsfr
136+
? [
137+
crawl({
138+
"dirPath": pathJoin(cwd, "stories"),
139+
"returnedPathsType": "absolute"
140+
}),
141+
crawl({
142+
"dirPath": pathJoin(cwd, "src"),
143+
"returnedPathsType": "absolute",
144+
"getDoCrawlInDir": async ({ relativeDirPath }) => {
145+
if (pathBasename(relativeDirPath) === "generatedFromCss") {
146+
return false;
147+
}
148+
149+
return true;
150+
}
151+
})
152+
]
153+
: [
154+
crawl({
155+
"dirPath": cwd,
156+
"returnedPathsType": "absolute",
157+
"getDoCrawlInDir": async ({ relativeDirPath }) => {
158+
if (pathBasename(relativeDirPath) === "node_modules") {
159+
return false;
160+
}
161+
162+
if (relativeDirPath === `public${pathSep}dsfr`) {
163+
return false;
164+
}
165+
166+
if (pathBasename(relativeDirPath).startsWith(".")) {
167+
return false;
168+
}
169+
170+
return true;
171+
}
172+
}),
173+
(async () => {
174+
const nodeModuleDirPath = await (async function callee(
175+
n: number
176+
): Promise<string> {
177+
if (n >= cwd.split(pathSep).length) {
178+
throw new Error("Need to install node modules?");
179+
}
180+
181+
const nodeModuleDirPath = pathJoin(
182+
...[cwd, ...new Array(n).fill(".."), "node_modules"]
183+
);
184+
185+
try {
186+
await access(
187+
pathJoin(
188+
...[
189+
nodeModuleDirPath,
190+
...codegouvfrReactDsfr.split("/")
191+
]
192+
)
193+
);
194+
} catch {
195+
return callee(n + 1);
196+
}
197+
198+
return nodeModuleDirPath;
199+
})(0);
200+
201+
return await crawl({
202+
"dirPath": nodeModuleDirPath,
203+
"returnedPathsType": "absolute",
204+
"getDoCrawlInDir": async ({ relativeDirPath }) => {
205+
if (
206+
relativeDirPath.startsWith("@") &&
207+
relativeDirPath.split(pathSep).length === 1
208+
) {
209+
return true;
210+
}
211+
212+
if (
213+
relativeDirPath.split(pathSep).length === 1 ||
214+
(relativeDirPath.startsWith("@") &&
215+
relativeDirPath.split(pathSep).length === 2)
216+
) {
217+
const parsedPackageJson = await readFile(
218+
pathJoin(
219+
nodeModuleDirPath,
220+
relativeDirPath,
221+
"package.json"
222+
)
223+
).then(
224+
buff => JSON.parse(buff.toString("utf8")),
225+
() => undefined
226+
);
227+
228+
if (parsedPackageJson === undefined) {
229+
return false;
230+
}
231+
232+
if (parsedPackageJson["name"] === "tss-react") {
233+
return false;
234+
}
235+
236+
for (const packageName of [
237+
codegouvfrReactDsfr,
238+
"@gouvfr/dsfr",
239+
"@dataesr/react-dsfr"
240+
]) {
241+
if (
242+
Object.keys({
243+
...parsedPackageJson["dependencies"],
244+
...parsedPackageJson["devDependencies"],
245+
...parsedPackageJson["peerDependencies"]
246+
}).includes(packageName)
247+
) {
248+
return true;
249+
}
250+
}
251+
252+
return false;
253+
}
254+
255+
if (
256+
pathDirname(relativeDirPath).endsWith(
257+
pathJoin(...codegouvfrReactDsfr.split("/"))
258+
)
259+
) {
260+
return pathBasename(relativeDirPath) === "src";
261+
}
262+
263+
if (pathBasename(relativeDirPath) === "generatedFromCss") {
264+
return false;
265+
}
266+
267+
if (pathBasename(relativeDirPath) === "node_modules") {
268+
return false;
269+
}
270+
271+
if (pathBasename(relativeDirPath).startsWith(".")) {
272+
return false;
273+
}
274+
275+
return true;
276+
}
277+
});
278+
})()
279+
]
280+
)
281+
)
282+
.flat()
283+
.filter(
284+
filePath =>
285+
["tsx", "jsx", "js", "ts", "html", "htm"].find(ext =>
286+
filePath.endsWith(`.${ext}`)
287+
) !== undefined
288+
);
218289

219290
const prefixes = { "prefixDsfr": "fr-icon-", "prefixRemixIcon": "ri-" } as const;
220291

src/bin/tools/crawl.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,23 @@ async function crawlRec(params: {
4444
return [filePaths, ...recursiveCallResults].flat();
4545
}
4646

47-
/** List all files in a given directory return paths relative to the dirPath */
4847
export async function crawl(params: {
4948
dirPath: string;
5049
getDoCrawlInDir?: (prams: { relativeDirPath: string }) => boolean | Promise<boolean>;
50+
returnedPathsType: "absolute" | "relative to dirPath";
5151
}) {
52-
const { dirPath: rootDirPath, getDoCrawlInDir = () => true } = params;
52+
const { dirPath: rootDirPath, getDoCrawlInDir = () => true, returnedPathsType } = params;
5353

5454
const filePaths = await crawlRec({
5555
"dirPath": rootDirPath,
5656
"getDoCrawlInDir": ({ dirPath }) =>
5757
getDoCrawlInDir({ "relativeDirPath": pathRelative(rootDirPath, dirPath) })
5858
});
5959

60-
return filePaths.map(filePath => pathRelative(rootDirPath, filePath));
60+
switch (returnedPathsType) {
61+
case "absolute":
62+
return filePaths;
63+
case "relative to dirPath":
64+
return filePaths.map(filePath => pathRelative(rootDirPath, filePath));
65+
}
6166
}

src/bin/tools/getProjectRoot.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as fs from "fs";
22
import * as path from "path";
33

44
function getProjectRootRec(dirPath: string): string {
5-
if (fs.existsSync(path.join(dirPath, "LICENSE"))) {
5+
if (fs.existsSync(path.join(dirPath, "package.json"))) {
66
return dirPath;
77
}
88
return getProjectRootRec(path.join(dirPath, ".."));

0 commit comments

Comments
 (0)