1
1
/* eslint-env node */
2
2
'use strict' ;
3
3
4
- const ts = require ( 'typescript' ) ;
5
4
const chokidar = require ( 'chokidar' ) ;
6
5
const fs = require ( 'fs-extra' ) ;
7
- const path = require ( 'path' ) ;
8
- const debugWatcher = require ( 'debug' ) ( 'ember-cli-typescript:watcher' ) ;
6
+ const debug = require ( 'debug' ) ( 'ember-cli-typescript:tsc:trace' ) ;
9
7
10
8
module . exports = function compile ( project , tsOptions , callbacks ) {
11
9
// Ensure the output directory is created even if no files are generated
@@ -14,9 +12,12 @@ module.exports = function compile(project, tsOptions, callbacks) {
14
12
let fullOptions = Object . assign ( {
15
13
rootDir : project . root ,
16
14
allowJs : false ,
17
- noEmit : false
15
+ noEmit : false ,
16
+ diagnostics : debug . enabled ,
17
+ extendedDiagnostics : debug . enabled
18
18
} , tsOptions ) ;
19
19
20
+ let ts = project . require ( 'typescript' ) ;
20
21
let configPath = ts . findConfigFile ( './' , ts . sys . fileExists , 'tsconfig.json' ) ;
21
22
let createProgram = ts . createEmitAndSemanticDiagnosticsBuilderProgram ;
22
23
let host = ts . createWatchCompilerHost (
@@ -28,6 +29,10 @@ module.exports = function compile(project, tsOptions, callbacks) {
28
29
diagnosticCallback ( callbacks . reportWatchStatus )
29
30
) ;
30
31
32
+ if ( debug . enabled ) {
33
+ host . trace = str => debug ( str . trim ( ) ) ;
34
+ }
35
+
31
36
return ts . createWatchProgram ( host ) ;
32
37
} ;
33
38
@@ -43,44 +48,19 @@ function diagnosticCallback(callback) {
43
48
}
44
49
45
50
function buildWatchHooks ( sys , callbacks ) {
46
- let watchedFiles = new Map ( ) ;
47
- let fileChanged = ( type , path ) => {
48
- debugWatcher ( 'file changed (%s) %s' , type , path ) ;
49
- if ( callbacks . watchedFileChanged ) {
50
- callbacks . watchedFileChanged ( ) ;
51
- }
52
- } ;
53
-
54
51
return Object . assign ( { } , sys , {
55
- watchFile ( _file , callback ) {
56
- // tsc is inconsistent about whether it provides relative or absolute paths
57
- let file = path . resolve ( _file ) ;
58
-
59
- debugWatcher ( 'watchFile %s' , file ) ;
60
- watchedFiles . set ( file , callback ) ;
61
-
62
- return {
63
- close ( ) {
64
- watchedFiles . delete ( file ) ;
65
- }
66
- } ;
67
- } ,
68
-
52
+ watchFile : null ,
69
53
watchDirectory ( dir , callback ) {
70
54
if ( ! fs . existsSync ( dir ) ) return ;
71
55
72
56
let ignored = / \/ ( \. .* ?| d i s t | n o d e _ m o d u l e s | t m p ) \/ / ;
73
57
let watcher = chokidar . watch ( dir , { ignored, ignoreInitial : true } ) ;
74
58
75
59
watcher . on ( 'all' , ( type , path ) => {
76
- if ( type === 'add' && path . endsWith ( '.ts' ) ) {
77
- fileChanged ( type , path ) ;
78
- callback ( path ) ;
79
- } else if ( watchedFiles . has ( path ) ) {
80
- fileChanged ( type , path ) ;
81
- watchedFiles . get ( path ) ( path , type === 'change' ? 1 : 2 ) ;
82
- } else {
83
- debugWatcher ( 'unknown file event %s %s' , type , path ) ;
60
+ callback ( path ) ;
61
+
62
+ if ( path . endsWith ( '.ts' ) && callbacks . watchedFileChanged ) {
63
+ callbacks . watchedFileChanged ( ) ;
84
64
}
85
65
} ) ;
86
66
0 commit comments