• 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 {
17  shouldWriteChangedList,
18  writeFileSync,
19  getHotReloadFiles
20} from '../../utils';
21import { projectConfig } from '../../../main';
22import {
23  incrementWatchFile,
24  hotReloadSupportFiles
25} from '../../ets_checker';
26
27export function watchChangeFiles() {
28  function addFileToCache(this: any, key: string, id: string) {
29    let modifiedFiles: string[] = [];
30    if (this.cache && this.cache.has(key)) {
31      modifiedFiles = this.cache.get(key);
32      modifiedFiles.push(id);
33    } else {
34      modifiedFiles.push(id);
35    }
36    this.cache.set(key, modifiedFiles);
37  }
38  return {
39    name: 'watchChangedFiles',
40    watchChange(id: string, change: {event: 'create' | 'update' | 'delete'}): void {
41      if (change.event === 'update') {
42        addFileToCache.call(this, 'watchModifiedFiles', id);
43      }
44      if (['create', 'delete'].includes(change.event)) {
45        addFileToCache.call(this, 'watchRemovedFiles', id);
46      }
47    },
48    beforeBuild() {
49      this.cache.set('watchChangedFilesCache', 'watchChangedFiles');
50      const watchModifiedFiles: string[] = this.cache.get('watchModifiedFiles') || [];
51      const watchRemovedFiles: string[] = this.cache.get('watchRemovedFiles') || [];
52      if (shouldWriteChangedList(watchModifiedFiles, watchRemovedFiles)) {
53        writeFileSync(projectConfig.changedFileList, JSON.stringify(
54          getHotReloadFiles(watchModifiedFiles, watchRemovedFiles, hotReloadSupportFiles)));
55      }
56      incrementWatchFile(watchModifiedFiles, watchRemovedFiles);
57    }
58  };
59}
60