Skip to content

Commit

Permalink
fix(misc): make workspace tsc executor to handle dependencies (#6475)
Browse files Browse the repository at this point in the history
  • Loading branch information
leosvelperez committed Jul 23, 2021
1 parent 1655f4b commit f087146
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 20 deletions.
52 changes: 38 additions & 14 deletions packages/workspace/src/executors/tsc/tsc.impl.spec.ts
@@ -1,31 +1,44 @@
jest.mock('../../core/project-graph');
jest.mock('../../utilities/assets');
jest.mock('../../utilities/buildable-libs-utils');
jest.mock('../../utilities/fileutils');
jest.mock('../../utilities/typescript/compilation');

import { ExecutorContext } from '@nrwl/devkit';
import { join } from 'path';
import { copyAssets } from '../../utilities/assets';
import {
calculateProjectDependencies,
checkDependentProjectsHaveBeenBuilt,
createTmpTsConfig,
} from '../../utilities/buildable-libs-utils';
import { readJsonFile, writeJsonFile } from '../../utilities/fileutils';
import { compileTypeScript } from '../../utilities/typescript/compilation';
import { TypeScriptExecutorOptions } from './schema';
import { tscExecutor } from './tsc.impl';

const defaultPackageJson = { name: 'workspacelib', version: '0.0.1' };
jest.mock('../../utilities/fileutils', () => ({
...jest.requireActual<any>('../../utilities/fileutils'),
writeJsonFile: jest.fn(),
readJsonFile: jest.fn(() => ({ ...defaultPackageJson })),
}));
const readJsonFileMock = readJsonFile as jest.Mock<any>;
jest.mock('../../utilities/typescript/compilation');
const compileTypeScriptMock = compileTypeScript as jest.Mock<{
success: boolean;
}>;
jest.mock('../../utilities/assets');

describe('executor: tsc', () => {
const assets = ['some-file.md'];
let context: ExecutorContext;
let normalizedOptions: TypeScriptExecutorOptions;
let options: TypeScriptExecutorOptions;
const defaultPackageJson = { name: 'workspacelib', version: '0.0.1' };
const compileTypeScriptMock = compileTypeScript as jest.Mock;
const readJsonFileMock = readJsonFile as jest.Mock;

beforeEach(() => {
jest.clearAllMocks();

(calculateProjectDependencies as jest.Mock).mockImplementation(() => ({
target: { data: { root: 'libs/workspacelib' } },
dependencies: [],
}));
(createTmpTsConfig as jest.Mock).mockImplementation(
() => '/my-app/tsconfig.app.generated.json'
);
(checkDependentProjectsHaveBeenBuilt as jest.Mock).mockReturnValue(true);
readJsonFileMock.mockImplementation(() => ({ ...defaultPackageJson }));

context = {
cwd: '/root',
root: '/root',
Expand Down Expand Up @@ -54,8 +67,19 @@ describe('executor: tsc', () => {
outputPath: join(context.root, options.outputPath),
tsConfig: join(context.root, options.tsConfig),
};
});

jest.clearAllMocks();
it('should return { success: false } when dependent projects have not been built', async () => {
(calculateProjectDependencies as jest.Mock).mockImplementation(() => ({
target: { data: { root: 'libs/workspacelib' } },
dependencies: [{}],
}));
(checkDependentProjectsHaveBeenBuilt as jest.Mock).mockReturnValue(false);

const result = await tscExecutor(options, context);

expect(result).toEqual({ success: false });
expect(compileTypeScriptMock).not.toHaveBeenCalled();
});

it('should return typescript compilation result', async () => {
Expand Down
44 changes: 38 additions & 6 deletions packages/workspace/src/executors/tsc/tsc.impl.ts
@@ -1,19 +1,51 @@
import { ExecutorContext, logger, normalizePath } from '@nrwl/devkit';
import { ExecutorContext, normalizePath } from '@nrwl/devkit';
import { basename, dirname, join, relative } from 'path';
import { readCachedProjectGraph } from '../../core/project-graph';
import { copyAssets } from '../../utilities/assets';
import { readJsonFile, writeJsonFile } from '../../utilities/fileutils';
import {
compileTypeScript,
TypescriptWatchChangeEvent,
} from '../../utilities/typescript/compilation';
calculateProjectDependencies,
checkDependentProjectsHaveBeenBuilt,
createTmpTsConfig,
} from '../../utilities/buildable-libs-utils';
import { readJsonFile, writeJsonFile } from '../../utilities/fileutils';
import { compileTypeScript } from '../../utilities/typescript/compilation';
import { TypeScriptExecutorOptions } from './schema';

export async function tscExecutor(
options: TypeScriptExecutorOptions,
context: ExecutorContext
) {
const normalizedOptions = normalizeOptions(options, context);
const projectRoot = context.workspace.projects[context.projectName].root;
// const projectRoot = context.workspace.projects[context.projectName].root;

const projectGraph = readCachedProjectGraph();
const { target, dependencies } = calculateProjectDependencies(
projectGraph,
context.root,
context.projectName,
context.targetName,
context.configurationName
);
const projectRoot = target.data.root;

if (dependencies.length > 0) {
const areDependentProjectsBuilt = checkDependentProjectsHaveBeenBuilt(
context.root,
context.projectName,
context.targetName,
dependencies
);
if (!areDependentProjectsBuilt) {
return { success: false };
}

normalizedOptions.tsConfig = createTmpTsConfig(
join(context.root, options.tsConfig),
context.root,
projectRoot,
dependencies
);
}

// this has to happen first so the folder is created where the assets are copied into
const result = compileTypeScript({
Expand Down

0 comments on commit f087146

Please sign in to comment.