add:增加导出组件和为组件全局增加属性功能
This commit is contained in:
@ -5,6 +5,8 @@ import { IComponent } from '../type/IComponent';
|
||||
const componentsDir = path.join(__dirname, '../schemes/components');
|
||||
const outputJsonPath = path.join(__dirname, '../schemes/scheme.json'); // 输出到当前目录
|
||||
const outputTsPath = path.join(__dirname, '../schemes/scheme.ts'); // 输出到当前目录
|
||||
const exportModifiedComponentsDir = path.join(__dirname, '../schemes/exported-components'); // 导出修改后的组件文件夹
|
||||
const exportModifiedComponents = true; // 控制是否导出修改后的组件文件
|
||||
|
||||
async function readComponents(): Promise<IComponent[]> {
|
||||
const files = await fs.readdir(componentsDir);
|
||||
@ -26,11 +28,44 @@ async function readComponents(): Promise<IComponent[]> {
|
||||
|
||||
function mergeComponents(components: IComponent[]): Record<string, IComponent> {
|
||||
return components.reduce((acc, component) => {
|
||||
acc[component.type] = component;
|
||||
// 设置默认属性
|
||||
const updatedComponent = {
|
||||
...component,
|
||||
visible: true, // 统一设置 visible 为 true
|
||||
disable: false // 统一设置 disable 为 false
|
||||
};
|
||||
|
||||
acc[updatedComponent.type] = updatedComponent;
|
||||
return acc;
|
||||
}, {} as Record<string, IComponent>);
|
||||
}
|
||||
|
||||
async function writeModifiedComponents(components: IComponent[]): Promise<void> {
|
||||
if (!exportModifiedComponents) return;
|
||||
|
||||
try {
|
||||
await fs.mkdir(exportModifiedComponentsDir, { recursive: true });
|
||||
|
||||
for (const component of components) {
|
||||
const updatedComponent = {
|
||||
...component,
|
||||
visible: true, // 统一设置 visible 为 true
|
||||
disable: false // 统一设置 disable 为 false
|
||||
};
|
||||
|
||||
const fileName = `${component.type}.json`;
|
||||
const filePath = path.join(exportModifiedComponentsDir, fileName);
|
||||
const content = JSON.stringify(updatedComponent, null, 2);
|
||||
|
||||
await fs.writeFile(filePath, content);
|
||||
console.log(`Modified component ${fileName} has been written to ${filePath}`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`Error writing modified components:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async function writeMergedScheme(mergedScheme: Record<string, IComponent>): Promise<void> {
|
||||
try {
|
||||
const jsonContent = JSON.stringify(mergedScheme, null, 2);
|
||||
@ -50,6 +85,9 @@ async function writeMergedScheme(mergedScheme: Record<string, IComponent>): Prom
|
||||
async function main() {
|
||||
try {
|
||||
const components = await readComponents();
|
||||
if (exportModifiedComponents) {
|
||||
await writeModifiedComponents(components);
|
||||
}
|
||||
const mergedScheme = mergeComponents(components);
|
||||
await writeMergedScheme(mergedScheme);
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user