44import fs from 'fs' ;
55import path from 'path' ;
66import webpack from 'webpack' ;
7- import { IsModel } from './../utils' ;
7+ import { RematchFiles , getFilenameInfo } from './../utils' ;
88import createRematchTemps , { createModelsTempStr } from './temp' ;
99import { ModelType } from './../utils/interface' ;
1010import chokidar from 'chokidar' ;
11+ import { getModelInfo } from './utils' ;
1112
1213export type { ModelType } ;
1314class RematchWebpackPlugin {
@@ -31,47 +32,9 @@ class RematchWebpackPlugin {
3132 this . restCreate ( ) ;
3233 }
3334 // 递归文件
34- getPathDeep = ( filePath : string , isModel = false ) => {
35- const files = fs . readdirSync ( filePath ) ;
36- if ( files ) {
37- files . forEach ( ( filename : string ) => {
38- let mode = isModel ;
39- const filedir = path . join ( filePath , filename ) ;
40- const isNoEmty = fs . existsSync ( filedir ) ;
41- if ( ! isNoEmty ) {
42- return ;
43- }
44- const stats = fs . statSync ( filedir ) ;
45- if ( stats ) {
46- const isFile = stats . isFile ( ) ; //是文件
47- const isDir = stats . isDirectory ( ) ; //是文件夹
48- if ( isFile && isModel && / \. ( t s | | j s ) $ / . test ( filename ) ) {
49- const data = fs . readFileSync ( filedir , { encoding : 'utf-8' } ) ;
50- const { isModels, modelNames, isCreateModel } = IsModel ( data ) ;
51- const pathUrls = `${ filedir } ` . replace ( / \\ / g, '/' ) ;
52- const location = pathUrls . replace ( / \/ m o d e l s .* $ / , '' ) ;
53- const srcPath = pathUrls . replace ( new RegExp ( this . src ) , '.' ) ;
54- if ( isModels ) {
55- this . oldModel . push ( {
56- path : pathUrls ,
57- filename : filename . replace ( / \. ( t s | j s ) $ / , '' ) ,
58- modelName : modelNames ,
59- isCreateModel,
60- location,
61- name : modelNames || filename ,
62- srcPath,
63- } ) ;
64- }
65- }
66- if ( filename === 'models' ) {
67- mode = true ;
68- }
69- if ( isDir ) {
70- this . getPathDeep ( filedir , mode ) ; //递归,如果是文件夹,就继续遍历该文件夹下面的文件
71- }
72- }
73- } ) ;
74- }
35+ getPathDeep = ( filePath : string ) => {
36+ const rematchFiles = new RematchFiles ( filePath ) ;
37+ this . oldModel = rematchFiles . modelList ;
7538 } ;
7639
7740 // 重新生成
@@ -131,22 +94,9 @@ class RematchWebpackPlugin {
13194 if ( stats . isDirectory ( ) ) {
13295 return ;
13396 }
134- // 1. 判断是否已经存在
135- // 如果已经存在着直接更新
136- let isMode = false ;
137- let modelName : undefined ;
138- let isCreateModel = false ;
139- // 先判断路径是否存在models 和ts|js 结尾
140- if ( / \. ( t s | j s ) $ / . test ( newPath ) && / m o d e l s / . test ( newPath ) ) {
141- const {
142- isModels,
143- modelNames,
144- isCreateModel : isCreate ,
145- } = IsModel ( fs . readFileSync ( newPath , { encoding : 'utf-8' } ) ) ;
146- modelName = modelNames ;
147- isMode = isModels ;
148- isCreateModel = isCreate ;
149- }
97+ // 获取文件信息
98+ const { isMode, modelName, isCreateModel } = getModelInfo ( newPath ) ;
99+
150100 const newFile = this . oldModel . find ( ( item ) => item . path === newPath ) ;
151101 if ( newFile ) {
152102 // 进行判断是否还是 model
@@ -167,26 +117,24 @@ class RematchWebpackPlugin {
167117 return { ...item } ;
168118 } ) ;
169119 }
170- this . restCreate ( ) ;
171- } else {
120+ } else if ( isMode ) {
172121 // 判断是不是 model 是则更新
173- if ( isMode ) {
174- const pathUrls = `${ newPath } ` . replace ( / \\ / g, '/' ) ;
175- const arr = pathUrls . split ( / \\ | \/ / ) ;
176- let filename = arr [ arr . length - 1 ] . replace ( / \. ( t s | j s ) $ / , '' ) ;
177- const location = pathUrls . replace ( / \/ m o d e l s .* $ / , '' ) ;
178- const srcPath = pathUrls . replace ( new RegExp ( this . src ) , '.' ) ;
179- this . oldModel . push ( {
180- path : pathUrls ,
181- filename,
182- modelName,
183- isCreateModel,
184- location,
185- name : modelName || filename ,
186- srcPath,
187- } ) ;
188- this . restCreate ( ) ;
189- }
122+ const { srcPath, location, pathUrls, fileName } = getFilenameInfo (
123+ newPath ,
124+ this . src ,
125+ ) ;
126+ this . oldModel . push ( {
127+ path : pathUrls ,
128+ filename : fileName ,
129+ modelName,
130+ isCreateModel,
131+ location,
132+ name : modelName || fileName ,
133+ srcPath,
134+ } ) ;
135+ }
136+ if ( isMode || newFile ) {
137+ this . restCreate ( ) ;
190138 }
191139 } ;
192140 // 校验文件
@@ -217,16 +165,6 @@ class RematchWebpackPlugin {
217165 this . checkField ( path . resolve ( process . cwd ( ) , 'src' , pathName ) ) ;
218166 }
219167 } ) ;
220- // const watcher = fs.watch(path.resolve(process.cwd(), 'src'), {
221- // recursive: true,
222- // });
223- // watcher.on('change', (type, filename) => {
224- // if (typeof filename === 'string') {
225- // this.field = filename as string;
226- // this.newPath = path.resolve(process.cwd(), 'src', this.field);
227- // this.checkField();
228- // }
229- // });
230168 }
231169 } ) ;
232170 }
0 commit comments