forked from jpmorganchase/salt-ds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathks-theme-create.ts
92 lines (87 loc) · 2.5 KB
/
ks-theme-create.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { join, relative } from "path";
import fs from "fs-extra";
import {
KnapsackAssetSetsConfig,
KnapsackAssetSetConfig,
} from "@knapsack/types";
import tokens from "./knapsack/dist/tokens/design-tokens.nested.json";
const cssTokens = fs.readFileSync(
join(__dirname, "./knapsack/dist/tokens/design-tokens.css"),
"utf8"
);
const assetSetsPath = join(
__dirname,
"./knapsack/data/knapsack.asset-sets.json"
);
const themesDir = join(__dirname, "./knapsack/dist/themes");
fs.ensureDirSync(themesDir);
console.log("Creating theme...");
// @ts-ignore
const themeNames = Object.keys(tokens?.theme || {});
const themes = themeNames.map((theme) => {
console.log(`Making theme ${theme}`);
const themeTokenNames = cssTokens.split("\n").flatMap((line) => {
if (!line.trim().startsWith(`--theme-${theme}`)) return [];
const tokenName = line.trim().split(":")[0];
return [
`${tokenName.replace(`theme-${theme}`, `salt`)}: var(${tokenName});`,
];
});
const contents = [".salt-theme.salt-theme {", ...themeTokenNames, "}"].join(
"\n"
);
const cssPath = join(themesDir, `ks-theme-${theme}.css`);
fs.writeFileSync(cssPath, contents, {
encoding: "utf8",
});
return {
cssPath,
themeName: theme,
};
});
function capitalizeFirstLetter(string: string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
const assetSets: KnapsackAssetSetsConfig = {
globalAssetSetIds: ["salt-light", "salt-dark", ...themeNames],
allAssetSets: Object.fromEntries([
[
"salt-light",
((): KnapsackAssetSetConfig => ({
id: "salt-light",
title: "Salt (light)",
inlineJs: "document.body.setAttribute('data-mode', 'light');",
assets: [],
}))(),
],
[
"salt-dark",
((): KnapsackAssetSetConfig => ({
id: "salt-dark",
title: "Salt (dark)",
inlineJs: "document.body.setAttribute('data-mode', 'dark');",
assets: [],
}))(),
],
...themes.map(({ cssPath, themeName }) => {
const assetSet: KnapsackAssetSetConfig = {
id: themeName,
title: capitalizeFirstLetter(themeName),
assets: [
{
src: "../dist/tokens/design-tokens.css",
},
{
src: relative(join(__dirname, "./knapsack/data"), cssPath),
},
],
};
return [themeName, assetSet];
}),
]),
};
fs.writeFileSync(
join(__dirname, "./knapsack/data/knapsack.asset-sets.json"),
JSON.stringify(assetSets, null, 2)
);
console.log("Done");