Skip to content

Commit d5e14e2

Browse files
committed
Generate getColorOptions.ts
1 parent a579e4b commit d5e14e2

File tree

4 files changed

+46
-3
lines changed

4 files changed

+46
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,5 @@ jspm_packages
4444
/.yarn_home
4545
/dsfr
4646
/dist_test
47+
/src/lib/useTheme/generated
4748

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"main": "dist/lib/index.js",
1010
"types": "dist/lib/index.d.ts",
1111
"scripts": {
12-
"build": "rimraf dist/ && tsc -p src/bin && tsc -p src && yarn grant_exec_perms && yarn copy_dsfr_dist_to_root",
12+
"build": "rimraf dist/ && tsc -p src/bin && yarn copy_dsfr_dist_to_root && yarn css_to_ts && tsc -p src && yarn grant_exec_perms",
1313
"build:test": "rimraf dist_test/ && tsc -p src/test",
1414
"pretest": "yarn build:test",
1515
"test": "node dist_test/test/bin/main.js",
@@ -22,8 +22,9 @@
2222
"format": "yarn _format --write",
2323
"format:check": "yarn _format --list-different",
2424
"yarn_link": "ts-node src/bin/yarn_link.ts",
25-
"grant_exec_perms": "node dist/bin/tools/grant_exec_perms.js",
26-
"copy_dsfr_dist_to_root": "node dist/bin/copy_dsfr_dist_to_root.js"
25+
"grant_exec_perms": "node dist/bin/tools/grant_exec_perms",
26+
"copy_dsfr_dist_to_root": "node dist/bin/copy_dsfr_dist_to_root",
27+
"css_to_ts": "node dist/bin/css_to_ts"
2728
},
2829
"bin": {
2930
"copy_dsfr_dist_to_public": "dist/bin/copy_dsfr_dist_to_public.js"

src/bin/css_to_ts/css_to_ts.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { generateGetColorOptionsTsCode, parseColorOptions } from "./colorOptions";
2+
import { getProjectRoot } from "../tools/getProjectRoot";
3+
import * as fs from "fs";
4+
import { join as pathJoin, dirname as pathDirname, relative as pathRelative } from "path";
5+
6+
const projectRoot = getProjectRoot();
7+
8+
const rawCssCode = fs.readFileSync(pathJoin(projectRoot, "dsfr", "dsfr.css")).toString("utf8");
9+
10+
const tsCode = generateGetColorOptionsTsCode(parseColorOptions(rawCssCode));
11+
12+
const targetFilePath = pathJoin(
13+
projectRoot,
14+
"src",
15+
"lib",
16+
"useTheme",
17+
"generated",
18+
"getColorOptions.ts"
19+
);
20+
21+
fs.mkdirSync(pathDirname(targetFilePath), { "recursive": true });
22+
23+
fs.writeFileSync(
24+
targetFilePath,
25+
Buffer.from(
26+
[
27+
`// This file is generated automatically by ${pathRelative(
28+
projectRoot,
29+
__filename
30+
)}, please don't edit.`,
31+
`import type { ColorScheme } from "../../useColorScheme";`,
32+
``,
33+
tsCode,
34+
``,
35+
`export type ColorOptions = ReturnType<typeof getColorOptions>;`,
36+
``
37+
].join("\n"),
38+
"utf8"
39+
)
40+
);

src/bin/css_to_ts/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import "./css_to_ts";

0 commit comments

Comments
 (0)