@uiw-admin/plugins
Version:
159 lines • 4.95 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
/*
* @Description: 简单版本的 自动加载 model
*/
import fs from 'fs';
import path from 'path';
import { RematchFiles, getFilenameInfo } from './../utils';
import createRematchTemps, { createModelsTempStr } from './temp';
import chokidar from 'chokidar';
import { getModelInfo } from './utils';
class RematchWebpackPlugin {
constructor(props) {
this.oldModel = [];
this.deleteModel = [];
this.field = '';
this.src = '';
this.uiw = '';
this.lazyLoad = false;
this.newPath = '';
this.isTS = true;
// 递归文件
this.getPathDeep = filePath => {
var rematchFiles = new RematchFiles(filePath);
this.oldModel = rematchFiles.modelList;
};
// 重新生成
this.restCreate = () => {
var modelStr = createModelsTempStr(this.oldModel, this.lazyLoad, this.isTS);
if (!fs.existsSync(this.uiw)) {
fs.mkdirSync(this.uiw);
}
// path.resolve(process.cwd(), this.isTS ? 'src/.uiw/routes.tsx' : 'src/.uiw/routes.js'),
// 在项目的 src/.uiw/ 创建 rematch.ts 用于models加载
fs.writeFileSync(path.resolve(process.cwd(), this.isTS ? 'src/.uiw/rematch.ts' : 'src/.uiw/rematch.js'), createRematchTemps(modelStr, this.isTS), {
flag: 'w+',
encoding: 'utf-8'
});
if (this.lazyLoad) {
fs.writeFileSync(path.resolve(process.cwd(), 'src/.uiw/modelsMap.json'), JSON.stringify(this.oldModel, (_, value) => value, 2), {
flag: 'w+',
encoding: 'utf-8'
});
}
this.field = '';
this.deleteModel = [];
};
// 删除文件的时候
this.deleteField = newPath => {
var newModel = [];
this.oldModel.forEach(item => {
var {
path
} = item;
var rgx = new RegExp("^" + newPath);
if (rgx.test(path) || newPath === path) {
this.deleteModel.push(path);
} else {
newModel.push(item);
}
});
// 如果是空的不用管了
// 如果存在则直接重新生成
if (this.deleteModel.length !== 0) {
this.oldModel = newModel;
this.restCreate();
}
};
// 文件变更时
this.existenceField = newPath => {
var stats = fs.statSync(newPath);
if (!stats) {
return;
}
// 文件夹不用动
if (stats.isDirectory()) {
return;
}
// 获取文件信息
var {
isMode,
modelName,
isCreateModel
} = getModelInfo(newPath);
var newFile = this.oldModel.find(item => item.path === newPath);
if (newFile) {
// 进行判断是否还是 model
if (!isMode) {
this.deleteModel.push(newPath);
// 过滤出不是 一个 model 文件的项
this.oldModel = this.oldModel.filter(item => item.path !== newPath);
} else {
// 更新内容 重新生成
this.oldModel = this.oldModel.map(item => {
if (item.path === newPath) {
return _extends({}, item, {
name: modelName || item.name,
modelName
});
}
return _extends({}, item);
});
}
} else if (isMode) {
// 判断是不是 model 是则更新
var {
srcPath,
location,
pathUrls,
fileName
} = getFilenameInfo(newPath, this.src);
this.oldModel.push({
path: pathUrls,
filename: fileName,
modelName,
isCreateModel,
location,
name: modelName || fileName,
srcPath
});
}
if (isMode || newFile) {
this.restCreate();
}
};
// 校验文件
this.checkField = newPath => {
// 校验文件是否存在
var iss = fs.existsSync(newPath);
if (iss) {
//存在
this.existenceField(newPath);
} else {
// 不存在
this.deleteField(newPath);
}
};
this.src = path.resolve(process.cwd(), 'src');
this.uiw = path.resolve(process.cwd(), 'src/.uiw');
this.isTS = fs.existsSync(path.join(process.cwd(), 'tsconfig.json'));
this.lazyLoad = !!(props != null && props.lazyLoad);
this.getPathDeep(this.src);
this.restCreate();
}
apply(compiler) {
var _this = this;
compiler.hooks.afterPlugins.tap('RematchWebpackPlugin', function () {
if (process.env.NODE_ENV === 'development') {
chokidar.watch(path.resolve(process.cwd(), 'src'), {
cwd: path.resolve(process.cwd(), 'src')
}).on('all', (event, pathName) => {
if (['change', 'add', 'unlink'].includes(event) && /(models\/|models\.(ts|js))/.test(pathName)) {
_this.checkField(path.resolve(process.cwd(), 'src', pathName));
}
});
}
});
}
}
export default RematchWebpackPlugin;