diff --git a/packages/node/src/executors/package/package.impl.ts b/packages/node/src/executors/package/package.impl.ts index 0391503fb58d9d..ee6963c2762c8f 100644 --- a/packages/node/src/executors/package/package.impl.ts +++ b/packages/node/src/executors/package/package.impl.ts @@ -6,6 +6,7 @@ import { checkDependentProjectsHaveBeenBuilt, updateBuildableProjectPackageJsonDependencies, } from '@nrwl/workspace/src/utilities/buildable-libs-utils'; +import { debounceTime } from 'rxjs/operators'; import { NodePackageBuilderOptions } from './utils/models'; import compileTypeScriptFiles from './utils/compile-typescript-files'; import updatePackageJson from './utils/update-package-json'; @@ -36,16 +37,50 @@ export async function packageExecutor( throw new Error(); } - const result = await compileTypeScriptFiles( + const result = compileTypeScriptFiles( normalizedOptions, context, libRoot, dependencies ); - await copyAssetFiles(normalizedOptions.files); + if (options.watch && 'subscribe' in result) { + result.pipe(debounceTime(150)).subscribe(() => { + updatePackageAndCopyAssets( + normalizedOptions, + context, + target, + dependencies + ); + }); + } else { + updatePackageAndCopyAssets( + normalizedOptions, + context, + target, + dependencies + ); + } + + if (options.cli) { + addCliWrapper(normalizedOptions, context); + } + + return { + ...result, + outputPath: normalizedOptions.outputPath, + }; +} + +async function updatePackageAndCopyAssets( + options, + context, + target, + dependencies +) { + await copyAssetFiles(options.files); - updatePackageJson(normalizedOptions, context); + updatePackageJson(options, context); if ( dependencies.length > 0 && options.updateBuildableProjectDepsInPackageJson @@ -57,18 +92,9 @@ export async function packageExecutor( context.configurationName, target, dependencies, - normalizedOptions.buildableProjectDepsInPackageJsonType + options.buildableProjectDepsInPackageJsonType ); } - - if (options.cli) { - addCliWrapper(normalizedOptions, context); - } - - return { - ...result, - outputPath: normalizedOptions.outputPath, - }; } export default packageExecutor; diff --git a/packages/workspace/src/utilities/typescript/compilation.ts b/packages/workspace/src/utilities/typescript/compilation.ts index 71603d0df63f9a..0dd3777f8ea306 100644 --- a/packages/workspace/src/utilities/typescript/compilation.ts +++ b/packages/workspace/src/utilities/typescript/compilation.ts @@ -1,5 +1,6 @@ import { logger } from '@nrwl/devkit'; import { removeSync } from 'fs-extra'; +import { Observable, Subject } from 'rxjs'; import * as ts from 'typescript'; import { readTsConfig } from '../typescript'; @@ -13,9 +14,16 @@ export interface TypeScriptCompilationOptions { watch?: boolean; } +export interface TypescriptWatchChangeEvent { + diagnostic: ts.Diagnostic; + newLine: string; + options: ts.CompilerOptions; + errorCount: number; +} + export function compileTypeScript( options: TypeScriptCompilationOptions -): { success: boolean } | Promise { +): { success: boolean } | Observable { const normalizedOptions = normalizeOptions(options); if (normalizedOptions.deleteOutputPath) { @@ -64,14 +72,20 @@ function createProgram( } } -function createWatchProgram(tsconfig: ts.ParsedCommandLine): Promise { +function createWatchProgram( + tsconfig: ts.ParsedCommandLine +): Observable { + const changeSubject = new Subject(); const host = ts.createWatchCompilerHost( tsconfig.fileNames, tsconfig.options, ts.sys ); + host.onWatchStatusChange = (diagnostic, newLine, options, errorCount) => { + changeSubject.next({ diagnostic, newLine, options, errorCount }); + }; ts.createWatchProgram(host); - return new Promise(() => {}); + return changeSubject.asObservable(); } function normalizeOptions(