Skip to content

Commit 17e9901

Browse files
authored
Merge pull request #196 from typed-ember/simplify-watcher
Simplify watcher
2 parents 59cf3a8 + 3d5e68f commit 17e9901

File tree

1 file changed

+14
-34
lines changed

1 file changed

+14
-34
lines changed

lib/utilities/compile.js

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
/* eslint-env node */
22
'use strict';
33

4-
const ts = require('typescript');
54
const chokidar = require('chokidar');
65
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');
97

108
module.exports = function compile(project, tsOptions, callbacks) {
119
// Ensure the output directory is created even if no files are generated
@@ -14,9 +12,12 @@ module.exports = function compile(project, tsOptions, callbacks) {
1412
let fullOptions = Object.assign({
1513
rootDir: project.root,
1614
allowJs: false,
17-
noEmit: false
15+
noEmit: false,
16+
diagnostics: debug.enabled,
17+
extendedDiagnostics: debug.enabled
1818
}, tsOptions);
1919

20+
let ts = project.require('typescript');
2021
let configPath = ts.findConfigFile('./', ts.sys.fileExists, 'tsconfig.json');
2122
let createProgram = ts.createEmitAndSemanticDiagnosticsBuilderProgram;
2223
let host = ts.createWatchCompilerHost(
@@ -28,6 +29,10 @@ module.exports = function compile(project, tsOptions, callbacks) {
2829
diagnosticCallback(callbacks.reportWatchStatus)
2930
);
3031

32+
if (debug.enabled) {
33+
host.trace = str => debug(str.trim());
34+
}
35+
3136
return ts.createWatchProgram(host);
3237
};
3338

@@ -43,44 +48,19 @@ function diagnosticCallback(callback) {
4348
}
4449

4550
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-
5451
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,
6953
watchDirectory(dir, callback) {
7054
if (!fs.existsSync(dir)) return;
7155

7256
let ignored = /\/(\..*?|dist|node_modules|tmp)\//;
7357
let watcher = chokidar.watch(dir, { ignored, ignoreInitial: true });
7458

7559
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();
8464
}
8565
});
8666

0 commit comments

Comments
 (0)