-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate.js
More file actions
104 lines (80 loc) · 3.46 KB
/
generate.js
File metadata and controls
104 lines (80 loc) · 3.46 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
/* eslint-disable no-console */
/* eslint-disable @typescript-eslint/no-var-requires */
/* eslint-disable global-require */
/* eslint-disable guard-for-in */
const { paramCase, camelCase } = require('change-case');
const { join, dirname } = require('path');
const { writeFile, mkdir } = require('fs');
const { promisify } = require('util');
const writeFileAsync = promisify(writeFile);
const mkdirpAsync = promisify(mkdir);
const writeToShadowDir = (file, content) =>
mkdirpAsync(dirname(file), { recursive: true }).then(() => writeFileAsync(file, content, 'utf-8'));
const themesFolder = join(process.cwd(), 'generated');
const themes = require('./palette');
const jsToken = (token, value, { noVar } = {}) =>
`export const ${camelCase(token)} = '${noVar ? value : `var(--${paramCase(token)})`}';`;
const cssToken = (token, value, tabs = 8) => `${' '.repeat(tabs)}--${paramCase(token)}: ${value};\n`;
const capitalizeFirstLetter = (string) => string.charAt(0).toUpperCase() + string.slice(1);
const getThemeConstName = (theme) => `${capitalizeFirstLetter(theme)}Theme`;
const jsTokensFile = (theme, file, { noVar } = {}) => {
const fileContent = ['// AUTOGENERATED CONTENT\n'];
for (const token in theme) {
fileContent.push(jsToken(token, theme[token], { noVar }));
fileContent.push('\n');
}
return writeToShadowDir(file, fileContent.join(''));
};
const SCTokensFile = (theme, file, tabs = 4) => {
const fileContent = [
'// AUTOGENERATED CONTENT\n',
"import { createGlobalStyle } from 'styled-components';\n\n",
'export const theme = createGlobalStyle`\n',
`${' '.repeat(tabs)}:root {\n`,
];
for (const token in theme) {
if (!theme[token].value) fileContent.push(cssToken(token, theme[token]));
}
fileContent.push(`${' '.repeat(tabs)}}\n`);
fileContent.push('`;\n');
fileContent.push('export default theme;\r\n');
return writeToShadowDir(file, fileContent.join(''));
};
const CssTokensFile = (theme, file) => {
const fileContent = ['/* AUTOGENERATED CONTENT */\n', ':root {\n'];
for (const token in theme) {
if (!theme[token].value) fileContent.push(cssToken(token, theme[token], 4));
}
fileContent.push('}\r\n');
return writeToShadowDir(file, fileContent.join(''));
};
const indexFile = (themes, file) => {
const fileContent = ['// AUTOGENERATED CONTENT\n', "export * from './colors';\n"];
themes.forEach((theme) => {
fileContent.push(`export { theme as ${getThemeConstName(theme)} } from './${theme}'\n`);
});
return writeToShadowDir(file, fileContent.join(''));
};
(async (t) => {
const writers = [];
// eslint-disable-next-line no-shadow
const themes = Object.keys(t);
for (const theme in t) {
for (const token in t[theme]) {
themes.forEach((themeName) => {
if (!t[themeName][token]) {
console.log('\n');
console.warn(`😞 Token: "${token}" doesn't exist in theme "${themeName}"!`);
}
});
}
}
for (const theme in t) {
writers.push(SCTokensFile(t[theme], join(themesFolder, `${theme}.ts`)));
writers.push(CssTokensFile(t[theme], join(themesFolder, `${theme}.css`)));
}
const tokensSchema = t[themes[0]];
writers.push(jsTokensFile(tokensSchema, join(themesFolder, 'colors.ts')));
writers.push(indexFile(themes, join(themesFolder, 'index.ts')));
await Promise.all(writers);
})(themes);