-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-themes.ts
63 lines (50 loc) · 2.12 KB
/
generate-themes.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
import fs from "fs";
import path from "path";
import {
toKebabCase,
makeJsonTransformations,
generateCSSTheme,
appendThemeToMainCss,
} from "./src/utils";
const TOKENS_FOLDER_PATH = "./tokens";
fs.writeFileSync("./themes/main.css", "");
if (fs.existsSync(TOKENS_FOLDER_PATH)) {
const colorFolders = fs.readdirSync(TOKENS_FOLDER_PATH);
colorFolders.forEach((colorFolder) => {
const colorFolderPath = path.join(TOKENS_FOLDER_PATH, colorFolder);
if (fs.statSync(colorFolderPath).isDirectory()) {
const files = fs.readdirSync(colorFolderPath);
const jsonFiles = files.filter((file) => path.extname(file) === ".json");
jsonFiles.forEach((jsonFile) => {
const jsonFilePath = path.join(colorFolderPath, jsonFile);
const jsonStr = fs.readFileSync(jsonFilePath, "utf-8");
const json = JSON.parse(jsonStr);
// FILE NAMES
const colorFolderName = toKebabCase(colorFolder);
const jsonFileName = toKebabCase(jsonFile);
const cssFileName = toKebabCase(jsonFile).replace(".json", ".css");
// JSON TRANSFORMATIONS
const transformedJson = makeJsonTransformations(json);
const transformedJsonStr = JSON.stringify(transformedJson);
// CSS THEME GENERATION
const cssThemeClassName = `${colorFolderName}-${jsonFileName.replace(
".json",
""
)}`;
const cssThemeVars = generateCSSTheme(transformedJson, "", cssThemeClassName);
// PATHS
const colorDirPath = `themes/${colorFolderName}`;
const colorThemeJsonFilePath = `${colorDirPath}/${jsonFileName}`;
const colorThemeCssFilePath = `${colorDirPath}/${cssFileName}`;
// CREATE DIRS AND FILES
const isColorDirExists = fs.existsSync(colorDirPath);
if (!isColorDirExists) fs.mkdirSync(colorDirPath, { recursive: true });
fs.writeFileSync(colorThemeJsonFilePath, transformedJsonStr);
fs.writeFileSync(colorThemeCssFilePath, cssThemeVars);
appendThemeToMainCss(colorFolderName, cssFileName);
});
}
});
} else {
console.error(`Directory ${TOKENS_FOLDER_PATH} does not exist.`);
}