107ac75b1Sopenharmony_ci/*
207ac75b1Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
307ac75b1Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
407ac75b1Sopenharmony_ci * you may not use this file except in compliance with the License.
507ac75b1Sopenharmony_ci * You may obtain a copy of the License at
607ac75b1Sopenharmony_ci *
707ac75b1Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
807ac75b1Sopenharmony_ci *
907ac75b1Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1007ac75b1Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1107ac75b1Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1207ac75b1Sopenharmony_ci * See the License for the specific language governing permissions and
1307ac75b1Sopenharmony_ci * limitations under the License.
1407ac75b1Sopenharmony_ci */
1507ac75b1Sopenharmony_ci
1607ac75b1Sopenharmony_ciimport path from 'path';
1707ac75b1Sopenharmony_ci
1807ac75b1Sopenharmony_ciimport {
1907ac75b1Sopenharmony_ci  shouldWriteChangedList,
2007ac75b1Sopenharmony_ci  writeFileSync,
2107ac75b1Sopenharmony_ci  getHotReloadFiles,
2207ac75b1Sopenharmony_ci  storedFileInfo,
2307ac75b1Sopenharmony_ci  resourcesRawfile,
2407ac75b1Sopenharmony_ci  differenceResourcesRawfile
2507ac75b1Sopenharmony_ci} from '../../utils';
2607ac75b1Sopenharmony_ciimport {
2707ac75b1Sopenharmony_ci  projectConfig,
2807ac75b1Sopenharmony_ci  readAppResource,
2907ac75b1Sopenharmony_ci  resources
3007ac75b1Sopenharmony_ci} from '../../../main';
3107ac75b1Sopenharmony_ciimport {
3207ac75b1Sopenharmony_ci  incrementWatchFile,
3307ac75b1Sopenharmony_ci  hotReloadSupportFiles
3407ac75b1Sopenharmony_ci} from '../../ets_checker';
3507ac75b1Sopenharmony_ciimport { ShouldEnableDebugLine } from '../ets_ui/rollup-plugin-ets-typescript';
3607ac75b1Sopenharmony_ci
3707ac75b1Sopenharmony_ciexport function watchChangeFiles() {
3807ac75b1Sopenharmony_ci  function addFileToCache(this: any, key: string, id: string) {
3907ac75b1Sopenharmony_ci    let modifiedFiles: string[] = [];
4007ac75b1Sopenharmony_ci    if (!projectConfig.hotReload && this.cache && this.cache.has(key)) {
4107ac75b1Sopenharmony_ci      modifiedFiles = this.cache.get(key);
4207ac75b1Sopenharmony_ci      modifiedFiles.push(id);
4307ac75b1Sopenharmony_ci    } else {
4407ac75b1Sopenharmony_ci      modifiedFiles.push(id);
4507ac75b1Sopenharmony_ci    }
4607ac75b1Sopenharmony_ci    this.cache.set(key, modifiedFiles);
4707ac75b1Sopenharmony_ci  }
4807ac75b1Sopenharmony_ci  return {
4907ac75b1Sopenharmony_ci    name: 'watchChangedFiles',
5007ac75b1Sopenharmony_ci    watchChange(id: string, change: {event: 'create' | 'update' | 'delete'}): void {
5107ac75b1Sopenharmony_ci      if (change.event === 'update') {
5207ac75b1Sopenharmony_ci        addFileToCache.call(this, 'watchModifiedFiles', id);
5307ac75b1Sopenharmony_ci      }
5407ac75b1Sopenharmony_ci      if (['create', 'delete'].includes(change.event)) {
5507ac75b1Sopenharmony_ci        addFileToCache.call(this, 'watchRemovedFiles', id);
5607ac75b1Sopenharmony_ci      }
5707ac75b1Sopenharmony_ci      if (path.resolve(id) === path.resolve(process.env.appResource) && process.env.compileMode === 'moduleJson') {
5807ac75b1Sopenharmony_ci        storedFileInfo.resourceTableChanged = true;
5907ac75b1Sopenharmony_ci        storedFileInfo.resourceList.clear();
6007ac75b1Sopenharmony_ci        resources.app = {};
6107ac75b1Sopenharmony_ci        readAppResource(process.env.appResource);
6207ac75b1Sopenharmony_ci        if (process.env.rawFileResource) {
6307ac75b1Sopenharmony_ci          resourcesRawfile(process.env.rawFileResource, storedFileInfo.resourcesArr);
6407ac75b1Sopenharmony_ci          this.share.rawfilechanged = differenceResourcesRawfile(storedFileInfo.lastResourcesSet, storedFileInfo.resourcesArr);
6507ac75b1Sopenharmony_ci        }
6607ac75b1Sopenharmony_ci      }
6707ac75b1Sopenharmony_ci      ShouldEnableDebugLine.enableDebugLine = false;
6807ac75b1Sopenharmony_ci    },
6907ac75b1Sopenharmony_ci    beforeBuild() {
7007ac75b1Sopenharmony_ci      this.cache.set('watchChangedFilesCache', 'watchChangedFiles');
7107ac75b1Sopenharmony_ci      const watchModifiedFiles: string[] = this.cache.get('watchModifiedFiles') || [];
7207ac75b1Sopenharmony_ci      const watchRemovedFiles: string[] = this.cache.get('watchRemovedFiles') || [];
7307ac75b1Sopenharmony_ci      if (!projectConfig.removeChangedFileListInSdk && shouldWriteChangedList(watchModifiedFiles, watchRemovedFiles)) {
7407ac75b1Sopenharmony_ci        writeFileSync(projectConfig.changedFileList, JSON.stringify(
7507ac75b1Sopenharmony_ci          getHotReloadFiles(watchModifiedFiles, watchRemovedFiles, hotReloadSupportFiles)));
7607ac75b1Sopenharmony_ci      }
7707ac75b1Sopenharmony_ci      incrementWatchFile(watchModifiedFiles, watchRemovedFiles);
7807ac75b1Sopenharmony_ci      if (this.cache.get('lastWatchResourcesArr')) {
7907ac75b1Sopenharmony_ci        storedFileInfo.lastResourcesSet = new Set([...this.cache.get('lastWatchResourcesArr')]);
8007ac75b1Sopenharmony_ci      }
8107ac75b1Sopenharmony_ci    }
8207ac75b1Sopenharmony_ci  };
8307ac75b1Sopenharmony_ci}
84