-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathBuilder.ts
65 lines (56 loc) · 1.59 KB
/
Builder.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
/*
* @poppinss/chokidar-ts
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import Debug from 'debug'
import tsStatic from 'typescript'
import { PluginManager } from './PluginManager'
const debug = Debug('tsc:builder')
/**
* Exposes the API to build the project similar to `tsc` command.
*/
export class Builder {
public host: tsStatic.CompilerHost
public program: tsStatic.Program
public compilerOptions?: tsStatic.CompilerOptions
constructor(
private ts: typeof tsStatic,
private config: tsStatic.ParsedCommandLine,
private pluginManager: PluginManager
) {
debug('initiating builder')
}
/**
* Create typescript program
*/
public createProgram() {
const { options, fileNames } = this.config
this.host = this.ts.createCompilerHost(options)
this.program = this.ts.createProgram(fileNames, options, this.host)
this.compilerOptions = this.config.options
}
/**
* Build the project using the Typescript compiler API
*/
public build() {
this.createProgram()
debug('emitting program')
const result = this.program.emit(
undefined,
this.ts.sys.writeFile,
undefined,
undefined,
this.pluginManager.getTransformers(this.ts, this.config.options)
)
const diagnostics = this.ts.getPreEmitDiagnostics(this.program).concat(result.diagnostics)
debug('initial build has "%d" errors', diagnostics.length)
return {
skipped: result.emitSkipped,
diagnostics,
}
}
}