Skip to content

Commit ffd148b

Browse files
committed
Refactor build script
1 parent f78048c commit ffd148b

File tree

4 files changed

+47
-49
lines changed

4 files changed

+47
-49
lines changed

src/bin/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Here are the scripts exposed as utility to the user of `react-dsfr`

src/scripts/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
In this directory are the scripts that are run at build time.
2+
We run them directly using `ts-node`.

src/scripts/build.ts

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ import { assert } from "tsafe/assert";
2929

3030
const nodeModuleDirPath = pathJoin(projectRootDirPath, "node_modules");
3131

32-
fs.cpSync(pathJoin(nodeModuleDirPath, "@gouvfr", "dsfr", "dist"), dsfrDirPath, {
32+
const gouvFrDsfrDistDirPath = pathJoin(nodeModuleDirPath, "@gouvfr", "dsfr", "dist");
33+
34+
fs.cpSync(gouvFrDsfrDistDirPath, dsfrDirPath, {
3335
"recursive": true
3436
});
3537

@@ -43,21 +45,16 @@ import { assert } from "tsafe/assert";
4345
)
4446
);
4547

48+
const icons = await collectIcons({
49+
"remixiconDirPath": pathJoin(nodeModuleDirPath, "remixicon"),
50+
"iconsCssRawCode": fs
51+
.readFileSync(pathJoin(dsfrDirPath, "utility", "icons", "icons.css"))
52+
.toString("utf8")
53+
});
54+
4655
fs.writeFileSync(
4756
pathJoin(dsfrDirPath, pathOfIconsJson),
48-
Buffer.from(
49-
JSON.stringify(
50-
await collectIcons({
51-
"remixiconDirPath": pathJoin(nodeModuleDirPath, "remixicon"),
52-
"iconsCssRawCode": fs
53-
.readFileSync(pathJoin(dsfrDirPath, "utility", "icons", "icons.css"))
54-
.toString("utf8")
55-
}),
56-
null,
57-
2
58-
),
59-
"utf8"
60-
)
57+
Buffer.from(JSON.stringify(icons, null, 2), "utf8")
6158
);
6259

6360
const distDirPath = pathJoin(projectRootDirPath, "dist");
@@ -66,7 +63,13 @@ import { assert } from "tsafe/assert";
6663
fs.rmSync(distDirPath, { "recursive": true, "force": true });
6764
}
6865

69-
cssToTs();
66+
cssToTs({
67+
icons,
68+
"generatedDirPath": pathJoin(projectRootDirPath, "src", "lib", "generatedFromCss"),
69+
"rawDsfrCssCode": fs
70+
.readFileSync(pathJoin(gouvFrDsfrDistDirPath, "dsfr.css"))
71+
.toString("utf8")
72+
});
7073

7174
await tsc({
7275
"tsconfigDirPath": pathJoin(projectRootDirPath, "src", "bin"),
@@ -88,8 +91,7 @@ import { assert } from "tsafe/assert";
8891
"doWatch": false
8992
});
9093

91-
//NOTE: From here it's only for local linking, required for storybook and
92-
// running integration apps.
94+
//NOTE: From here it's only for local linking, required for storybook and running integration apps.
9395
if (!args.npm) {
9496
fs.writeFileSync(
9597
pathJoin(distDirPath, "package.json"),

src/scripts/cssToTs/cssToTs.ts

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,19 @@ import { generateClassNamesTsCode } from "./classNames";
88
import * as fs from "fs";
99
import { join as pathJoin, basename as pathBasename, relative as pathRelative } from "path";
1010
import type { Icon } from "../../bin/only-include-used-icons";
11-
import { pathOfIconsJson } from "../../bin/only-include-used-icons";
1211

13-
export function cssToTs() {
14-
const projectRoot = getProjectRoot();
15-
16-
const dsfrDistDirPath = pathJoin(projectRoot, "dsfr");
17-
18-
const rawCssCode = fs.readFileSync(pathJoin(dsfrDistDirPath, "dsfr.css")).toString("utf8");
19-
20-
const generatedDirPath = pathJoin(projectRoot, "src", "lib", "generatedFromCss");
12+
export function cssToTs(params: {
13+
rawDsfrCssCode: string;
14+
generatedDirPath: string;
15+
icons: Icon[];
16+
}) {
17+
const { rawDsfrCssCode, generatedDirPath, icons } = params;
2118

2219
fs.mkdirSync(generatedDirPath, { "recursive": true });
2320

2421
const warningMessage = [
2522
`// This file is generated automatically by ${pathRelative(
26-
projectRoot,
23+
getProjectRoot(),
2724
__filename
2825
)}, please don't edit.`
2926
].join("\n");
@@ -36,7 +33,7 @@ export function cssToTs() {
3633
[
3734
warningMessage,
3835
``,
39-
generateGetColorOptionsTsCode(rawCssCode),
36+
generateGetColorOptionsTsCode(rawDsfrCssCode),
4037
``,
4138
`export type ColorOptions = ReturnType<typeof getColorOptions>;`,
4239
``
@@ -55,7 +52,7 @@ export function cssToTs() {
5552
""
5653
)}";`,
5754
``,
58-
generateGetColorDecisionsTsCode(rawCssCode),
55+
generateGetColorDecisionsTsCode(rawDsfrCssCode),
5956
``,
6057
`export type ColorDecisions = ReturnType<typeof getColorDecisions>;`,
6158
``
@@ -66,7 +63,10 @@ export function cssToTs() {
6663

6764
fs.writeFileSync(
6865
pathJoin(generatedDirPath, "breakpoints.ts"),
69-
Buffer.from([warningMessage, ``, generateBreakpointsTsCode(rawCssCode)].join("\n"), "utf8")
66+
Buffer.from(
67+
[warningMessage, ``, generateBreakpointsTsCode(rawDsfrCssCode)].join("\n"),
68+
"utf8"
69+
)
7070
);
7171

7272
fs.writeFileSync(
@@ -76,7 +76,7 @@ export function cssToTs() {
7676
warningMessage,
7777
`import { breakpoints } from "../breakpoints";`,
7878
``,
79-
generateTypographyTsCode(rawCssCode),
79+
generateTypographyTsCode(rawDsfrCssCode),
8080
``
8181
].join("\n"),
8282
"utf8"
@@ -85,7 +85,10 @@ export function cssToTs() {
8585

8686
fs.writeFileSync(
8787
pathJoin(generatedDirPath, "spacing.ts"),
88-
Buffer.from([warningMessage, ``, generateSpacingTsCode(rawCssCode), ``].join("\n"), "utf8")
88+
Buffer.from(
89+
[warningMessage, ``, generateSpacingTsCode(rawDsfrCssCode), ``].join("\n"),
90+
"utf8"
91+
)
8992
);
9093

9194
fs.writeFileSync(
@@ -95,23 +98,13 @@ export function cssToTs() {
9598
warningMessage,
9699
``,
97100
generateClassNamesTsCode({
98-
rawCssCode,
99-
...(() => {
100-
const icons: Icon[] = JSON.parse(
101-
fs
102-
.readFileSync(pathJoin(dsfrDistDirPath, pathOfIconsJson))
103-
.toString("utf8")
104-
);
105-
106-
return {
107-
"dsfrIconClassNames": icons
108-
.filter(({ prefix }) => prefix === "fr-icon-")
109-
.map(({ iconId, prefix }) => `${prefix}${iconId}`),
110-
"remixiconClassNames": icons
111-
.filter(({ prefix }) => prefix === "ri-")
112-
.map(({ iconId, prefix }) => `${prefix}${iconId}`)
113-
};
114-
})()
101+
"rawCssCode": rawDsfrCssCode,
102+
"dsfrIconClassNames": icons
103+
.filter(({ prefix }) => prefix === "fr-icon-")
104+
.map(({ iconId, prefix }) => `${prefix}${iconId}`),
105+
"remixiconClassNames": icons
106+
.filter(({ prefix }) => prefix === "ri-")
107+
.map(({ iconId, prefix }) => `${prefix}${iconId}`)
115108
}),
116109
``
117110
].join("\n"),

0 commit comments

Comments
 (0)