-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
tsc.impl.ts
108 lines (95 loc) · 2.97 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
import { ExecutorContext } from '@nrwl/devkit';
import {
assetGlobsToFiles,
FileInputOutput,
} from '@nrwl/workspace/src/utilities/assets';
import { join, resolve } from 'path';
import { checkDependencies } from '../../utils/check-dependencies';
import {
getHelperDependency,
HelperDependency,
} from '../../utils/compiler-helper-dependency';
import { CopyAssetsHandler } from '../../utils/copy-assets-handler';
import { ExecutorOptions, NormalizedExecutorOptions } from '../../utils/schema';
import { compileTypeScriptFiles } from '../../utils/typescript/compile-typescript-files';
import { updatePackageJson } from '../../utils/update-package-json';
import { watchForSingleFileChanges } from '../../utils/watch-for-single-file-changes';
export function normalizeOptions(
options: ExecutorOptions,
contextRoot: string,
sourceRoot?: string,
projectRoot?: string
): NormalizedExecutorOptions {
const outputPath = join(contextRoot, options.outputPath);
if (options.watch == null) {
options.watch = false;
}
const files: FileInputOutput[] = assetGlobsToFiles(
options.assets,
contextRoot,
outputPath
);
return {
...options,
root: contextRoot,
sourceRoot,
projectRoot,
files,
outputPath,
tsConfig: join(contextRoot, options.tsConfig),
mainOutputPath: resolve(
outputPath,
options.main.replace(`${projectRoot}/`, '').replace('.ts', '.js')
),
};
}
export async function* tscExecutor(
_options: ExecutorOptions,
context: ExecutorContext
) {
const { sourceRoot, root } = context.workspace.projects[context.projectName];
const options = normalizeOptions(_options, context.root, sourceRoot, root);
const { projectRoot, tmpTsConfig, target, dependencies } = checkDependencies(
context,
_options.tsConfig
);
if (tmpTsConfig) {
options.tsConfig = tmpTsConfig;
}
const tsLibDependency = getHelperDependency(
HelperDependency.tsc,
options.tsConfig,
dependencies
);
if (tsLibDependency) {
dependencies.push(tsLibDependency);
}
const assetHandler = new CopyAssetsHandler({
projectDir: projectRoot,
rootDir: context.root,
outputDir: _options.outputPath,
assets: _options.assets,
});
if (options.watch) {
const disposeWatchAssetChanges =
await assetHandler.watchAndProcessOnAssetChange();
const disposePackageJsonChanged = await watchForSingleFileChanges(
join(context.root, projectRoot),
'package.json',
() => updatePackageJson(options, context, target, dependencies)
);
process.on('exit', async () => {
await disposeWatchAssetChanges();
await disposePackageJsonChanged();
});
process.on('SIGTERM', async () => {
await disposeWatchAssetChanges();
await disposePackageJsonChanged();
});
}
return yield* compileTypeScriptFiles(options, context, async () => {
await assetHandler.processAllAssetsOnce();
updatePackageJson(options, context, target, dependencies);
});
}
export default tscExecutor;