forked from Tencent/tdesign-miniprogram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-css-vars.js
68 lines (60 loc) · 2.45 KB
/
generate-css-vars.js
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
const fs = require('fs');
const path = require('path');
const combine = {
avatar: ['avatar-group', 'avatar'],
cell: ['cell-group', 'cell'],
collapse: ['collapse', 'collapse-panel'],
'dropdown-menu': ['dropdown-menu', 'dropdown-item'],
tag: ['tag', 'check-tag'],
checkbox: ['checkbox-group', 'checkbox'],
indexes: ['indexes', 'indexes-anchor'],
picker: ['picker', 'picker-item'],
radio: ['radio-group', 'radio'],
'side-bar': ['side-bar', 'side-bar-item'],
steps: ['steps', 'step-item'],
swiper: ['swiper', 'swiper-nav'],
tabs: ['tabs', 'tab-panel'],
'tab-bar': ['tab-bar', 'tab-bar-item'],
grid: ['grid', 'grid-item'],
};
function resolveCwd(...args) {
args.unshift(process.cwd());
return path.join(...args);
}
const COMPONENT_NAME = process.argv[process.argv.indexOf('--NAME') + 1]; // 在 --NAME 后面
const matchReg = /(?<=var).*?(?=;)/g;
// 使用 v2 文件夹下 _var.less 文件
const lessPath = [];
if (combine[COMPONENT_NAME]) {
combine[COMPONENT_NAME].forEach((item) => {
lessPath.push(resolveCwd(`src/${item}/${item}.less`));
});
} else {
lessPath.push(resolveCwd(`src/${COMPONENT_NAME}/${COMPONENT_NAME}.less`));
}
// 追加到文件
const cssVariableHeadContent = `\n\n### CSS Variables\n\n组件提供了下列 CSS 变量,可用于自定义样式。\n名称 | 默认值 | 描述 \n-- | -- | --\n`;
const cssVariableHeadContentEn = `\n\n### CSS Variables\n\nThe component provides the following CSS variables, which can be used to customize styles.\nName | Default Value | Description \n-- | -- | --\n`;
fs.appendFileSync(resolveCwd(`src/${COMPONENT_NAME}/README.md`), cssVariableHeadContent);
fs.appendFileSync(resolveCwd(`src/${COMPONENT_NAME}/README.en-US.md`), cssVariableHeadContentEn);
// 读取 less 文件内容
lessPath.forEach((item) => {
if (fs.existsSync(item)) {
fs.readFile(item, 'utf8', (err, file) => {
if (err) {
console.log('please execute npm run update:css first!', err);
return;
}
const list = file.match(matchReg)?.sort();
let cssVariableBodyContent = '';
list?.forEach((item) => {
cssVariableBodyContent += `${item.slice(1, item.indexOf(','))} | ${item.slice(
item.indexOf(',') + 2,
item.length - 1,
)} | - \n`;
});
fs.appendFileSync(resolveCwd(`src/${COMPONENT_NAME}/README.md`), cssVariableBodyContent);
fs.appendFileSync(resolveCwd(`src/${COMPONENT_NAME}/README.en-US.md`), cssVariableBodyContent);
});
}
});