Skip to content

Commit

Permalink
fix(node): package execution doesn't exit on watch mode
Browse files Browse the repository at this point in the history
Closes #5208
  • Loading branch information
bekos authored and vsavkin committed May 18, 2021
1 parent 6a4bb34 commit 4103c60
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 48 deletions.
42 changes: 11 additions & 31 deletions packages/workspace/src/executors/tsc/tsc.impl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,38 +67,18 @@ describe('executor: tsc', () => {
expect(result).toBe(expectedResult);
});

describe('copy assets', () => {
it('should not copy assets when typescript compilation is not successful', async () => {
compileTypeScriptMock.mockReturnValue({ success: false });
it('should copy assets before typescript compilation', async () => {
await tscExecutor(options, context);

await tscExecutor(options, context);

expect(copyAssets).not.toHaveBeenCalled();
});

it('should copy assets when typescript compilation is successful', async () => {
compileTypeScriptMock.mockReturnValue({ success: true });

await tscExecutor(options, context);

expect(copyAssets).toHaveBeenCalledWith(
assets,
context.root,
normalizedOptions.outputPath
);
});
expect(copyAssets).toHaveBeenCalledWith(
assets,
context.root,
normalizedOptions.outputPath
);
});

describe('update package.json', () => {
it('should not update package.json when typescript compilation is not successful', async () => {
compileTypeScriptMock.mockReturnValue({ success: false });

await tscExecutor(options, context);

expect(writeJsonFile).not.toHaveBeenCalled();
});

it('should update the package.json when typescript compilation is successful and both main and typings are missing', async () => {
it('should update the package.json when both main and typings are missing', async () => {
compileTypeScriptMock.mockReturnValue({ success: true });

await tscExecutor(options, context);
Expand All @@ -113,7 +93,7 @@ describe('executor: tsc', () => {
);
});

it('should update the package.json when typescript compilation is successful and only main is missing', async () => {
it('should update the package.json when only main is missing', async () => {
compileTypeScriptMock.mockReturnValue({ success: true });
const packageJson = {
...defaultPackageJson,
Expand All @@ -132,7 +112,7 @@ describe('executor: tsc', () => {
);
});

it('should update the package.json when typescript compilation is successful and only typings is missing', async () => {
it('should update the package.json when only typings is missing', async () => {
compileTypeScriptMock.mockReturnValue({ success: true });
const packageJson = {
...defaultPackageJson,
Expand All @@ -151,7 +131,7 @@ describe('executor: tsc', () => {
);
});

it('should not update the package.json when typescript compilation is successful and both main and typings are specified', async () => {
it('should not update the package.json when both main and typings are specified', async () => {
compileTypeScriptMock.mockReturnValue({ success: true });
readJsonFileMock.mockReturnValue({
...defaultPackageJson,
Expand Down
20 changes: 8 additions & 12 deletions packages/workspace/src/executors/tsc/tsc.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,19 @@ export async function tscExecutor(
const normalizedOptions = normalizeOptions(options, context);
const projectRoot = context.workspace.projects[context.projectName].root;

const result = compileTypeScript({
await copyAssets(
normalizedOptions.assets,
context.root,
normalizedOptions.outputPath
);
updatePackageJson(normalizedOptions, projectRoot);

return compileTypeScript({
outputPath: normalizedOptions.outputPath,
projectName: context.projectName,
projectRoot,
tsConfig: normalizedOptions.tsConfig,
});

if (result.success) {
await copyAssets(
normalizedOptions.assets,
context.root,
normalizedOptions.outputPath
);
updatePackageJson(normalizedOptions, projectRoot);
}

return result;
}

function getMainFileDirRelativeToProjectRoot(
Expand Down
8 changes: 3 additions & 5 deletions packages/workspace/src/utilities/typescript/compilation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface TypeScriptCompilationOptions {

export function compileTypeScript(
options: TypeScriptCompilationOptions
): { success: boolean } {
): { success: boolean } | Promise<any> {
const normalizedOptions = normalizeOptions(options);

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

function createWatchProgram(
tsconfig: ts.ParsedCommandLine
): { success: boolean } {
function createWatchProgram(tsconfig: ts.ParsedCommandLine): Promise<any> {
const host = ts.createWatchCompilerHost(
tsconfig.fileNames,
tsconfig.options,
ts.sys
);
ts.createWatchProgram(host);
return { success: true };
return new Promise(() => {});
}

function normalizeOptions(
Expand Down

0 comments on commit 4103c60

Please sign in to comment.