• 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';
17import { EventEmitter } from 'events';
18
19import { initConfig } from '../common/init_config';
20import { projectConfig } from '../../../main';
21import {
22  serviceChecker,
23  watchChecker
24} from '../../ets_checker';
25import { TS_WATCH_END_MSG } from '../../pre_define';
26
27export let tsWatchEmitter: EventEmitter | undefined = undefined;
28export let tsWatchEndPromise: Promise<void>;
29
30export function etsChecker() {
31  let executedOnce: boolean = false;
32  return {
33    name: 'etsChecker',
34    buildStart() {
35      if (process.env.watchMode === 'true' && process.env.triggerTsWatch === 'true') {
36        tsWatchEmitter = new EventEmitter();
37        tsWatchEndPromise = new Promise<void>(resolve => {
38          tsWatchEmitter.on(TS_WATCH_END_MSG, () => {
39            resolve();
40          });
41        });
42      }
43      if (executedOnce) {
44        return;
45      }
46      Object.assign(projectConfig, this.share.projectConfig);
47      Object.assign(this.share.projectConfig, {
48        compileHar: projectConfig.compileHar,
49        compileShared: projectConfig.compileShared,
50        moduleRootPath: projectConfig.moduleRootPath,
51        buildPath: projectConfig.buildPath
52      });
53      const logger = this.share.getLogger('etsChecker');
54      const rootFileNames: string[] = [];
55      Object.values(projectConfig.entryObj).forEach((fileName: string) => {
56        rootFileNames.push(path.resolve(fileName));
57      });
58      if (process.env.watchMode === 'true') {
59        executedOnce = true;
60        watchChecker(rootFileNames, logger);
61      } else {
62        serviceChecker(rootFileNames, logger);
63      }
64    }
65  };
66}
67
68