You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

97 lines
3.3 KiB

const fs = require('fs');
const Json5 =require('json5');
const packageJsonObject =JSON.parse(fs.readFileSync('./package.json').toString());
/**
* 特殊处理, 用于解决 @maxgraph/core 不能正常使用的问题, 会出现以下错误:
* ERROR in ./node_modules/.pnpm/@maxgraph+core@0.12.0/node_modules/@maxgraph/core/dist/index.js 174:0-24
* Module not found: Error: Can't resolve './types' in 'io.sc.platform.core.frontend/node_modules/.pnpm/@maxgraph+core@0.12.0/node_modules/@maxgraph/core/dist'
* Did you mean 'types.js'?
* BREAKING CHANGE: The request './types' failed to resolve only because it was resolved as fully specified
* (probably because the origin is strict EcmaScript Module, e. g. a module with javascript mimetype, a '*.mjs' file, or a '*.js' file where the package.json contains '"type": "module"').
* The extension in the request is mandatory for it to be fully specified.
* Add the extension to the request.
*
* 解决方案:
* 1. 删除 node_modules/@maxgraph/core/package.json 文件中 [ "type" : "module" ] 属性
* 2. 删除 node_modules/.pnpm/@maxgraph+core@xxx/package.json 文件中 [ "type" : "module" ] 属性
*/
let path ='./node_modules/@maxgraph/core/package.json';
if(fs.existsSync(path)){
const packageJson =JSON.parse(fs.readFileSync(path).toString());
delete packageJson['type'];
fs.writeFileSync(path, JSON.stringify(packageJson, null, ' '));
}
path ='./node_modules/.pnpm/@maxgraph+core@' + packageJsonObject['dependencies']['@maxgraph/core'] + '/node_modules/@maxgraph/core/package.json';
if(fs.existsSync(path)){
const packageJson =JSON.parse(fs.readFileSync(path).toString());
delete packageJson['type'];
fs.writeFileSync(path, JSON.stringify(packageJson, null, ' '));
}
/**
* 用于自动生成前端组件
* 通过 src/routes/routes.json 文件构建 src/components/index.ts 文件
*/
// 解析前端路由配置文件
const routesJson = Json5.parse(fs.readFileSync('./src/routes/routes.json', 'utf8'));
let content ='';
content +='/**\n';
content +=' * 此文件为自动生成文件,请勿修改\n';
content +=' */\n\n';
for(const route of routesJson){
generateImportComonents(route);
}
content +='\n';
content +='const localComponents = { \n';
for(const route of routesJson){
generateComonents(route);
}
content +='}\n\n';
content +='export default localComponents;\n';
fs.writeFileSync('./src/components/index.ts', content);
console.info('components generated!');
function generateImportComonents(route){
const componentName =route.component.replaceAll('.','_');
const componentPath =route.componentPath;
content +=`import ${componentName} from '${componentPath}';\n`;
if(route.children && route.children.length){
for(const child of route.children){
generateImportComonents(child);
}
}
}
function generateComonents(route){
const componentName =route.component.replaceAll('.','_');
content +=`'${route.component}': ${componentName},\n`;
if(route.children && route.children.length){
for(const child of route.children){
generateComonents(child);
}
}
}
/**
* 判断一个文件是否存在
* @param {*} file 文件路径
* @returns 文件是否存在
*/
function isFileExisted(file) {
return new Promise((resolve, reject) => {
fs.access(file, (err) => {
if (err) {
reject(false);
} else {
resolve(true);
}
});
})
}