-
Notifications
You must be signed in to change notification settings - Fork 381
Expand file tree
/
Copy pathwriteTokens.mjs
More file actions
106 lines (92 loc) · 3.38 KB
/
writeTokens.mjs
File metadata and controls
106 lines (92 loc) · 3.38 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
105
106
import { outputFileSync } from 'fs-extra/esm';
import { resolve, join } from 'node:path';
import { generateTokens } from './generateTokens.mjs';
const outDir = resolve(import.meta.dirname, '../dist');
const writeESMExport = (tokenName, tokenString) =>
outputFileSync(
join(outDir, 'esm/', `${tokenName}.js`),
`
export const ${tokenName} = ${tokenString};
export default ${tokenName};
`.trim()
);
const writeCJSExport = (tokenName, tokenString) =>
outputFileSync(
join(outDir, 'js', `${tokenName}.js`),
`
"use strict";
exports.__esModule = true;
exports.${tokenName} = ${tokenString};
exports["default"] = exports.${tokenName};
`.trim()
);
const writeDTSExport = (tokenName, tokenString) => {
const text = `
export const ${tokenName}: ${tokenString};
export default ${tokenName};
`.trim();
const filename = `${tokenName}.d.ts`;
outputFileSync(join(outDir, 'esm', filename), text);
outputFileSync(join(outDir, 'js', filename), text);
};
const allIndex = {};
const componentIndex = [];
const outputIndex = (index, indexFile) => {
const esmIndexString = index.map((file) => `export { ${file} } from './${file}';`).join('\n');
outputFileSync(join(outDir, 'esm', indexFile), esmIndexString);
outputFileSync(
join(outDir, 'js', indexFile),
`
"use strict";
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
exports.__esModule = true;
${index.map((file) => `__export(require('./${file}'));`).join('\n')}
`.trim()
);
outputFileSync(join(outDir, 'esm', indexFile.replace('.js', '.d.ts')), esmIndexString);
outputFileSync(join(outDir, 'js', indexFile.replace('.js', '.d.ts')), esmIndexString);
};
/**
* Writes CJS and ESM tokens to `dist` directory
*
* @param {any} tokens tokens from generateTokens
*/
function writeTokens(tokens) {
Object.entries(tokens).forEach(([tokenName, tokenValue]) => {
const tokenString = JSON.stringify(tokenValue, null, 2);
writeESMExport(tokenName, tokenString);
writeCJSExport(tokenName, tokenString);
writeDTSExport(tokenName, tokenString);
allIndex[tokenName] = true;
componentIndex.push(tokenName);
// Legacy token support -- values may be incorrect.
Object.values(tokenValue)
.map((values) => Object.entries(values))
.reduce((acc, val) => acc.concat(val), []) // flatten
.forEach(([oldTokenName, { name, value, darkValue }]) => {
const isChart = oldTokenName.includes('chart');
const oldToken = {
name,
value: isChart && !isNaN(+value) ? +value : value,
var: isChart ? `var(${name}, ${value})` : `var(${name})` // Include fallback value for chart vars
};
// Add dark theme values if they exist
if (darkValue !== undefined) {
oldToken.darkValue = isChart && !isNaN(+darkValue) ? +darkValue : darkValue;
}
const oldTokenString = JSON.stringify(oldToken, null, 2);
writeESMExport(oldTokenName, oldTokenString);
writeCJSExport(oldTokenName, oldTokenString);
writeDTSExport(oldTokenName, oldTokenString);
allIndex[oldTokenName] = true;
});
});
// Index files including legacy tokens
outputIndex(Object.keys(allIndex), 'index.js');
outputIndex(componentIndex, 'componentIndex.js');
// eslint-disable-next-line no-console
console.log('Wrote', Object.keys(allIndex).length * 4 + 4, 'token files');
}
writeTokens(generateTokens());