1/* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import path from 'path'; 17 18import { 19 shouldWriteChangedList, 20 writeFileSync, 21 getHotReloadFiles, 22 storedFileInfo, 23 resourcesRawfile, 24 differenceResourcesRawfile 25} from '../../utils'; 26import { 27 projectConfig, 28 readAppResource, 29 resources 30} from '../../../main'; 31import { 32 incrementWatchFile, 33 hotReloadSupportFiles 34} from '../../ets_checker'; 35import { ShouldEnableDebugLine } from '../ets_ui/rollup-plugin-ets-typescript'; 36 37export function watchChangeFiles() { 38 function addFileToCache(this: any, key: string, id: string) { 39 let modifiedFiles: string[] = []; 40 if (!projectConfig.hotReload && this.cache && this.cache.has(key)) { 41 modifiedFiles = this.cache.get(key); 42 modifiedFiles.push(id); 43 } else { 44 modifiedFiles.push(id); 45 } 46 this.cache.set(key, modifiedFiles); 47 } 48 return { 49 name: 'watchChangedFiles', 50 watchChange(id: string, change: {event: 'create' | 'update' | 'delete'}): void { 51 if (change.event === 'update') { 52 addFileToCache.call(this, 'watchModifiedFiles', id); 53 } 54 if (['create', 'delete'].includes(change.event)) { 55 addFileToCache.call(this, 'watchRemovedFiles', id); 56 } 57 if (path.resolve(id) === path.resolve(process.env.appResource) && process.env.compileMode === 'moduleJson') { 58 storedFileInfo.resourceTableChanged = true; 59 storedFileInfo.resourceList.clear(); 60 resources.app = {}; 61 readAppResource(process.env.appResource); 62 if (process.env.rawFileResource) { 63 resourcesRawfile(process.env.rawFileResource, storedFileInfo.resourcesArr); 64 this.share.rawfilechanged = differenceResourcesRawfile(storedFileInfo.lastResourcesSet, storedFileInfo.resourcesArr); 65 } 66 } 67 ShouldEnableDebugLine.enableDebugLine = false; 68 }, 69 beforeBuild() { 70 this.cache.set('watchChangedFilesCache', 'watchChangedFiles'); 71 const watchModifiedFiles: string[] = this.cache.get('watchModifiedFiles') || []; 72 const watchRemovedFiles: string[] = this.cache.get('watchRemovedFiles') || []; 73 if (!projectConfig.removeChangedFileListInSdk && shouldWriteChangedList(watchModifiedFiles, watchRemovedFiles)) { 74 writeFileSync(projectConfig.changedFileList, JSON.stringify( 75 getHotReloadFiles(watchModifiedFiles, watchRemovedFiles, hotReloadSupportFiles))); 76 } 77 incrementWatchFile(watchModifiedFiles, watchRemovedFiles); 78 if (this.cache.get('lastWatchResourcesArr')) { 79 storedFileInfo.lastResourcesSet = new Set([...this.cache.get('lastWatchResourcesArr')]); 80 } 81 } 82 }; 83} 84