/
tslib-dependency.ts
47 lines (39 loc) · 1.42 KB
/
tslib-dependency.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { ExecutorContext, getPackageManagerCommand } from '@nrwl/devkit';
import { readCachedProjectGraph } from '@nrwl/workspace/src/core/project-graph';
import { DependentBuildableProjectNode } from '@nrwl/workspace/src/utilities/buildable-libs-utils';
import { readTsConfig } from '@nrwl/workspace/src/utilities/typescript';
import { NormalizedExecutorOptions } from './schema';
const tslibNodeName = 'npm:tslib';
function shouldAddTslibDependency(
tsConfig: string,
dependencies: DependentBuildableProjectNode[]
): boolean {
if (dependencies.some((dep) => dep.name === tslibNodeName)) {
return false;
}
const config = readTsConfig(tsConfig);
return !!config.options.importHelpers;
}
export function addTslibDependencyIfNeeded(
options: NormalizedExecutorOptions,
context: ExecutorContext,
dependencies: DependentBuildableProjectNode[]
): void {
if (!shouldAddTslibDependency(options.tsConfig, dependencies)) {
return;
}
const depGraph = readCachedProjectGraph();
const tslibNode = depGraph.externalNodes[tslibNodeName];
if (!tslibNode) {
const pmc = getPackageManagerCommand();
throw new Error(
`"importHelpers" is enabled for ${context.targetName} but tslib is not installed. Use "${pmc.add} tslib" to install it.`
);
}
const tslibDependency: DependentBuildableProjectNode = {
name: tslibNodeName,
outputs: [],
node: tslibNode,
};
dependencies.push(tslibDependency);
}