gxd-vue-library
Version:
依赖与element Ui插件库,聚福宝福利PC端插件库
80 lines (68 loc) • 2.06 kB
JavaScript
;
const path = require('path');
const basePath = require('./path');
const fileHelper = require('./fileHepler');
const clog = require('./clog');
const lib = require('./lib/utils');
class AutoLoad {
constructor() {
this.init();
}
init(){
let files = fileHelper.getDirFiles(
basePath.buildComponentsDirectory,
['vue'],
['XdMpHtml/node/node.vue'],
false
);
//拼接字符串
let importStr = '',
exportStr = '',
count = 0;
Object.keys(files).map(key=>{
let file = files[key];
let moduleName = this.handleFile(file.path).join('');
let modulePath = file.path.replace(/\\/g,'/');
let str = '';
if (count === 0) {
str = `import ${moduleName} from './${modulePath}';`;
exportStr += `${moduleName}`;
} else {
str = `
import ${moduleName} from './${modulePath}';`;
exportStr += `,
${moduleName}`;
}
importStr += str;
count++;
});
/**读取文件,并替换文件内容**/
let content = fileHelper.readFileSync(path.join(basePath.buildTemplateDirectory, 'autoload.txt'));
exportStr = `{
${exportStr}
}`;
content = content.replace(/@import_modules@/g, importStr);
content = content.replace(/@modules_name@/g, exportStr);
fileHelper.writeFileSync(path.join(basePath.buildComponentsDirectory, 'autoload.js'), content);
clog('生成Components自动加载文件成功', 'green');
clog(`文件路径:${path.join(basePath.buildComponentsDirectory, 'autoload.js')}`, 'green');
lib.logsLine(clog);
}
handleFile(filePath){
if (filePath.indexOf(path.sep) !== -1) {
let temp = [];
let arr = filePath.split(path.sep);
let len = arr.length;
for (let i = 0; i < len; i++) {
if (i === 0) temp.push(arr[i]);
else temp.push(arr[i].replace(arr[i][0], arr[i][0].toLocaleUpperCase()));
}
return temp;
} else {
return [filePath];
}
}
}
module.exports = function(){
return new AutoLoad();
};