• 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';
18import * as ts from 'typescript';
19
20import {
21  projectConfig,
22  globalProgram
23} from '../../../main';
24import {
25  serviceChecker,
26  languageService,
27  printDiagnostic,
28  fastBuildLogger
29} from '../../ets_checker';
30import { TS_WATCH_END_MSG } from '../../pre_define';
31import { setChecker } from '../../utils';
32
33export let tsWatchEmitter: EventEmitter | undefined = undefined;
34export let tsWatchEndPromise: Promise<void>;
35
36export function etsChecker() {
37  let executedOnce: boolean = false;
38  return {
39    name: 'etsChecker',
40    buildStart() {
41      if (process.env.watchMode === 'true' && process.env.triggerTsWatch === 'true') {
42        tsWatchEmitter = new EventEmitter();
43        tsWatchEndPromise = new Promise<void>(resolve => {
44          tsWatchEmitter.on(TS_WATCH_END_MSG, () => {
45            resolve();
46          });
47        });
48      }
49      Object.assign(projectConfig, this.share.projectConfig);
50      Object.assign(this.share.projectConfig, {
51        compileHar: projectConfig.compileHar,
52        compileShared: projectConfig.compileShared,
53        moduleRootPath: projectConfig.moduleRootPath,
54        buildPath: projectConfig.buildPath,
55        isCrossplatform: projectConfig.isCrossplatform
56      });
57      const logger = this.share.getLogger('etsChecker');
58      const rootFileNames: string[] = [];
59      const resolveModulePaths: string[] = [];
60      Object.values(projectConfig.entryObj).forEach((fileName: string) => {
61        rootFileNames.push(path.resolve(fileName));
62      });
63      if (this.share && this.share.projectConfig && this.share.projectConfig.resolveModulePaths &&
64        Array.isArray(this.share.projectConfig.resolveModulePaths)) {
65        resolveModulePaths.push(...this.share.projectConfig.resolveModulePaths);
66      }
67      if (process.env.watchMode === 'true') {
68        !executedOnce && serviceChecker(rootFileNames, logger, resolveModulePaths);
69        executedOnce = true;
70        globalProgram.program = languageService.getProgram();
71        const allDiagnostics: ts.Diagnostic[] = globalProgram.program
72          .getSyntacticDiagnostics()
73          .concat(globalProgram.program.getSemanticDiagnostics())
74          .concat(globalProgram.program.getDeclarationDiagnostics());
75        allDiagnostics.forEach((diagnostic: ts.Diagnostic) => {
76          printDiagnostic(diagnostic);
77        });
78        fastBuildLogger.debug(TS_WATCH_END_MSG);
79        tsWatchEmitter.emit(TS_WATCH_END_MSG);
80      } else {
81        serviceChecker(rootFileNames, logger, resolveModulePaths);
82      }
83      setChecker();
84    }
85  };
86}
87
88