-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
tsc.impl.ts
112 lines (99 loc) · 3.32 KB
/
tsc.impl.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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 {
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 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({
outputPath: normalizedOptions.outputPath,
projectName: context.projectName,
projectRoot,
tsConfig: normalizedOptions.tsConfig,
});
await copyAssets(
normalizedOptions.assets,
context.root,
normalizedOptions.outputPath
);
updatePackageJson(normalizedOptions, projectRoot);
return result;
}
function getMainFileDirRelativeToProjectRoot(
main: string,
projectRoot: string
): string {
const mainFileDir = dirname(main);
const relativeDir = normalizePath(relative(projectRoot, mainFileDir));
const relativeMainFile = relativeDir === '' ? `./` : `./${relativeDir}/`;
return relativeMainFile;
}
function normalizeOptions(
options: TypeScriptExecutorOptions,
context: ExecutorContext
): TypeScriptExecutorOptions {
return {
...options,
outputPath: join(context.root, options.outputPath),
tsConfig: join(context.root, options.tsConfig),
};
}
function updatePackageJson(
options: TypeScriptExecutorOptions,
projectRoot: string
): void {
const packageJson = readJsonFile(join(projectRoot, 'package.json'));
if (packageJson.main && packageJson.typings) {
return;
}
const mainFile = basename(options.main).replace(/\.[tj]s$/, '');
const relativeMainFileDir = getMainFileDirRelativeToProjectRoot(
options.main,
projectRoot
);
const mainJsFile = `${relativeMainFileDir}${mainFile}.js`;
const typingsFile = `${relativeMainFileDir}${mainFile}.d.ts`;
packageJson.main = packageJson.main ?? mainJsFile;
packageJson.typings = packageJson.typings ?? typingsFile;
const outputPackageJson = join(options.outputPath, 'package.json');
writeJsonFile(outputPackageJson, packageJson);
}
export default tscExecutor;