-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-demo-snippets.js
156 lines (140 loc) · 4.89 KB
/
generate-demo-snippets.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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
const path = require('path');
const fs = require('fs');
// 指定组件名称
const COMPONENT_NAME = process.argv[process.argv.indexOf('--COMPONENT_NAME') + 1];
const filterFile = ['skyline']; // 需要过滤的示例文件夹内
const saveFileType = ['js', 'json', 'wxml', 'wxss']; // 需要保存的文件类型
const specialFileContent = [
{
file: 'app.json',
oldContent: 'pages/button/button',
newContent: 'pages/' + COMPONENT_NAME + '/' + COMPONENT_NAME,
},
{
file: 'project.private.config.json',
oldContent: 'tdesign-button-demo',
newContent: 'tdesign-' + COMPONENT_NAME + '-demo',
},
{
file: 'package.json',
oldContent: 'tdesign-button-demo',
newContent: 'tdesign-' + COMPONENT_NAME + '-demo',
},
{
file: 'package-lock.json',
oldContent: 'tdesign-button-demo',
newContent: 'tdesign-' + COMPONENT_NAME + '-demo',
},
];
const deleteKeyContent = [
{
file: `pages/${COMPONENT_NAME}/${COMPONENT_NAME}.json`,
keys: ['navigationBarBackgroundColor'],
},
];
const deleteNavbarContent = [{ file: `pages/${COMPONENT_NAME}/${COMPONENT_NAME}.wxml` }];
const copyFiles = (fromPath, toPath) => {
// fs.cp() 异步拷贝
fs.cp(fromPath, toPath, { recursive: true }, (err) => {
if (err) {
console.log(err);
}
});
};
const removeFile = (targetPath, filterFile, saveFileType) => {
fs.readdir(targetPath, (err, files) => {
if (err) {
console.error('Error reading source directory:', err);
return;
}
files.forEach((file) => {
if (fs.lstatSync(path.join(targetPath, file)).isDirectory()) {
// 处理目录
if (filterFile.includes(file)) {
// 如果是过滤的目录,则直接删除
fs.rmSync(path.join(targetPath, file), { recursive: true });
}
} else {
// 处理文件
if (!saveFileType.includes(file.slice(file.indexOf('.') + 1))) {
// 如果是过滤的文件,则直接删除
fs.unlink(path.join(targetPath, file), (err) => {
if (err) {
console.error('删除文件时出错:', err);
return;
}
console.log('文件已成功删除', path.join(targetPath, file));
});
}
}
});
});
};
const replaceContent = (targetPath) => {
specialFileContent.forEach((item) => {
fs.readFile(path.join(targetPath, item.file), 'utf8', (err, data) => {
if (err) {
console.error('Error reading source directory:', err);
return;
}
const result = data.replaceAll(item.oldContent, item.newContent);
fs.writeFile(path.join(targetPath, item.file), result, 'utf8', (err) => {
if (err) {
console.error('Error writing file:', err);
return;
}
console.log('文件已成功保存');
});
});
});
};
const deleteContentByKey = (targetPath) => {
deleteKeyContent.forEach((item) => {
const jsonData = fs.readFileSync(path.join(targetPath, item.file), 'utf8');
const data = JSON.parse(jsonData);
item.keys.forEach((key) => {
delete data[key];
});
const newData = JSON.stringify(data, null, 2);
fs.writeFileSync(path.join(targetPath, item.file), newData, 'utf8');
});
};
const deleteContentNavbar = (targetPath) => {
deleteNavbarContent.forEach((item) => {
const fileContent = fs.readFileSync(path.join(targetPath, item.file), 'utf8');
// 使用正则表达式匹配以 <t-navbar 开头并以 /> 结尾的内容
const regex = /<t-navbar[^>]*?\/>/g;
// 使用 replace() 方法替换匹配到的内容
const updatedContent = fileContent.replace(regex, '');
fs.writeFileSync(path.join(targetPath, item.file), updatedContent, 'utf8');
});
};
const generateDemoSnippets = async (componentName) => {
const baseDemoPath = path.resolve(__dirname, './_base_sinppets');
const _snippetsTargetPath = path.resolve(__dirname, './_snippets/');
const _snippetsOriginPath = path.join(
path.resolve(__dirname, './tdesign-miniprogram/_example/pages'),
componentName
);
if (!fs.existsSync(_snippetsOriginPath)) {
console.log('baseDemoPath is not exist');
return;
}
const targetPath = path.join(_snippetsTargetPath, 'tdesign-' + componentName + '-demo');
// 1. 复制基础片段到指定文件
await copyFiles(baseDemoPath, targetPath);
// 2. 复制 _snippetsOriginPath 下的示例代码到 targetPath
const cpDemoCodePath = path.join(targetPath, 'pages/' + componentName);
await copyFiles(_snippetsOriginPath, cpDemoCodePath);
setTimeout(async () => {
// 3. 检查片段内容
await removeFile(cpDemoCodePath, filterFile, saveFileType);
await replaceContent(targetPath, componentName);
await deleteContentByKey(targetPath);
await deleteContentNavbar(targetPath);
}, 1000);
};
/**
* @description 命令行执行 node generate-demo-snippets.js -- --COMPONENT_NAME action-sheet
*/
generateDemoSnippets(COMPONENT_NAME);