Skip to content

Commit

Permalink
fix(node): assets should be copied when building with --watch
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder committed Jun 1, 2021
1 parent d01bac7 commit c95f246
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 16 deletions.
52 changes: 39 additions & 13 deletions packages/node/src/executors/package/package.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
Expand All @@ -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;
20 changes: 17 additions & 3 deletions packages/workspace/src/utilities/typescript/compilation.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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<any> {
): { success: boolean } | Observable<TypescriptWatchChangeEvent> {
const normalizedOptions = normalizeOptions(options);

if (normalizedOptions.deleteOutputPath) {
Expand Down Expand Up @@ -64,14 +72,20 @@ function createProgram(
}
}

function createWatchProgram(tsconfig: ts.ParsedCommandLine): Promise<any> {
function createWatchProgram(
tsconfig: ts.ParsedCommandLine
): Observable<TypescriptWatchChangeEvent> {
const changeSubject = new Subject<TypescriptWatchChangeEvent>();
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(
Expand Down

0 comments on commit c95f246

Please sign in to comment.