• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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} from '../../../main';
30import {
31  incrementWatchFile,
32  hotReloadSupportFiles,
33  files
34} from '../../ets_checker';
35
36export function watchChangeFiles() {
37  function addFileToCache(this: any, key: string, id: string) {
38    let modifiedFiles: string[] = [];
39    if (this.cache && this.cache.has(key)) {
40      modifiedFiles = this.cache.get(key);
41      modifiedFiles.push(id);
42    } else {
43      modifiedFiles.push(id);
44    }
45    this.cache.set(key, modifiedFiles);
46  }
47  return {
48    name: 'watchChangedFiles',
49    watchChange(id: string, change: {event: 'create' | 'update' | 'delete'}): void {
50      if (change.event === 'update') {
51        addFileToCache.call(this, 'watchModifiedFiles', id);
52      }
53      if (['create', 'delete'].includes(change.event)) {
54        addFileToCache.call(this, 'watchRemovedFiles', id);
55      }
56      files[path.resolve(id)] ? files[path.resolve(id)].version++ : files[path.resolve(id)] = {version: 1};
57      if (path.resolve(id) === path.resolve(process.env.appResource) && process.env.compileMode === 'moduleJson') {
58        storedFileInfo.resourceTableChanged = true;
59        storedFileInfo.resourceList.clear();
60        readAppResource(process.env.appResource);
61        if (process.env.rawFileResource) {
62          resourcesRawfile(process.env.rawFileResource, storedFileInfo.resourcesArr);
63          this.share.rawfilechanged = differenceResourcesRawfile(storedFileInfo.lastResourcesSet, storedFileInfo.resourcesArr);
64        }
65      }
66    },
67    beforeBuild() {
68      this.cache.set('watchChangedFilesCache', 'watchChangedFiles');
69      const watchModifiedFiles: string[] = this.cache.get('watchModifiedFiles') || [];
70      const watchRemovedFiles: string[] = this.cache.get('watchRemovedFiles') || [];
71      if (shouldWriteChangedList(watchModifiedFiles, watchRemovedFiles)) {
72        writeFileSync(projectConfig.changedFileList, JSON.stringify(
73          getHotReloadFiles(watchModifiedFiles, watchRemovedFiles, hotReloadSupportFiles)));
74      }
75      incrementWatchFile(watchModifiedFiles, watchRemovedFiles);
76      if (this.cache.get('lastWatchResourcesArr')) {
77        storedFileInfo.lastResourcesSet = new Set([...this.cache.get('lastWatchResourcesArr')]);
78      }
79    }
80  };
81}
82