public async watch()

in src/compiler.ts [126:171]


  public async watch(opts?: NonBlockingWatchOptions): Promise<ts.Watch<ts.BuilderProgram> | never> {
    this.prepareForBuild();

    const host = ts.createWatchCompilerHost(
      this.configPath,
      {
        ...this.tsconfig.compilerOptions,
        noEmitOnError: false,
      },
      this.system,
      ts.createEmitAndSemanticDiagnosticsBuilderProgram,
      opts?.reportDiagnostics,
      opts?.reportWatchStatus,
      this.tsconfig.watchOptions,
    );
    if (!host.getDefaultLibLocation) {
      throw new Error('No default library location was found on the TypeScript compiler host!');
    }
    const orig = host.afterProgramCreate;
    // This is a callback cascade, so it's "okay" to return an unhandled promise there. This may
    // cause an unhandled promise rejection warning, but that's not a big deal.
    //
    // eslint-disable-next-line @typescript-eslint/no-misused-promises
    host.afterProgramCreate = (builderProgram) => {
      const emitResult = this.consumeProgram(builderProgram.getProgram(), host.getDefaultLibLocation!());

      for (const diag of emitResult.diagnostics.filter((d) => d.code === JSII_DIAGNOSTICS_CODE)) {
        utils.logDiagnostic(diag, this.projectRoot);
      }

      if (orig) {
        orig.call(host, builderProgram);
      }
      if (opts?.compilationComplete) {
        opts.compilationComplete(emitResult);
      }
    };
    const watch = ts.createWatchProgram(host);

    if (opts?.nonBlocking) {
      // In non-blocking mode, returns the handle to the TypeScript watch interface.
      return watch;
    }
    // In blocking mode, returns a never-resolving promise.
    return new Promise<never>(() => null);
  }