本地dev环境,运行时用node模块自动读取并整合文件

const http = require('http');
const fs = require('fs');
const path = require('path');
const glob = require('globby');

const fileList = glob.sync(['../services/*.*']);
console.info('tag', fileList);


let allData = '';
const needTag1 = 'import {service} from "../core/service";';
const needTag2 = 'import DEC from "../mock/decorator";'
const needTag3 = 'export {service}';

readFile(fileList,0)

function readFile(filesTag,index){
    if(index<filesTag.length) {
        fs.readFile(filesTag[index], 'utf-8', function (err, data) {
            if (err) {
                console.log(err);
            } else {
                index++;
                allData+=data;
                allData = allData.replace(/import(.*?)\;/ig,'');
                allData = allData.replace(/export(.*?)\}/ig,'');
                readFile(filesTag, index);
            }
        });
    }else{
        writeFileSync(needTag1+'\n'+needTag2+allData+'\n'+needTag3)
    }
}
function writeFileSync(allData){
    fs.writeFileSync('allService.js', allData, 'utf-8', (err) => {
        if (err) {
            console.log(`写入失败`)
        } else {
            console.log(`写入成功`)
        }
    });
}

 

posted @ 2019-07-23 10:48  黑夜蓝天  阅读(187)  评论(0编辑  收藏  举报