forked from mui/material-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuildColorTypes.js
More file actions
80 lines (66 loc) · 2.22 KB
/
buildColorTypes.js
File metadata and controls
80 lines (66 loc) · 2.22 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
import * as path from 'path';
import * as fse from 'fs-extra';
import * as colors from '@mui/material/colors';
// use netlify deploy preview if you want to test changes
const HOST = 'https://mui.com/';
function getColorHref(name, variant) {
return `static/colors-preview/${name}-${variant}-24x24.svg`;
}
function buildColorType(name, variants) {
const typesFilename = path.resolve(__dirname, `../packages/mui-material/src/colors/${name}.d.ts`);
const typescript = `/* tslint:disable max-line-length */
/**
* ${Object.entries(variants)
.map((entry) => {
const [variant] = entry;
return `})`;
})
.join(' ')}
*/
declare const ${name}: {
${Object.entries(variants)
.map((entry) => {
const [variant, color] = entry;
return ` /**
* Preview: })
*/
${variant}: '${color}';`;
})
.join('\n')}
};
export default ${name};
`;
return fse.writeFile(typesFilename, typescript, { encoding: 'utf8' });
}
function buildColorPreviews(name, variants) {
const nextPublicPath = path.resolve(__dirname, '../docs/public/');
return Promise.all(
Object.entries(variants).map(async (variantEntry) => {
const [variant, color] = variantEntry;
const svg = `<svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 0 24 24" width="24">
<rect width="100%" height="100%" fill="${color}"/>
</svg>`;
const filename = path.resolve(nextPublicPath, getColorHref(name, variant));
await fse.writeFile(filename, svg, { encoding: 'utf8' });
}),
);
}
/**
* The goal is to have a preview of the actual color and the color string in IntelliSense
* We create for each color an svg that is filled with that color and reference
* that svg in the corresponding JSDoc.
* Since we use https://mui.com as a reference changes are only visible
* after release
*/
async function main() {
await Promise.all(
Object.entries(colors).map(async (entry) => {
const [name, variants] = entry;
await Promise.all([buildColorPreviews(name, variants), buildColorType(name, variants)]);
}),
);
}
main().catch((error) => {
console.error(error);
process.exit(error.code || 1);
});