Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 14 additions & 34 deletions lib/utilities/compile.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
/* eslint-env node */
'use strict';

const ts = require('typescript');
const chokidar = require('chokidar');
const fs = require('fs-extra');
const path = require('path');
const debugWatcher = require('debug')('ember-cli-typescript:watcher');
const debug = require('debug')('ember-cli-typescript:tsc:trace');

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

let ts = project.require('typescript');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

let configPath = ts.findConfigFile('./', ts.sys.fileExists, 'tsconfig.json');
let createProgram = ts.createEmitAndSemanticDiagnosticsBuilderProgram;
let host = ts.createWatchCompilerHost(
Expand All @@ -28,6 +29,10 @@ module.exports = function compile(project, tsOptions, callbacks) {
diagnosticCallback(callbacks.reportWatchStatus)
);

if (debug.enabled) {
host.trace = str => debug(str.trim());
}

return ts.createWatchProgram(host);
};

Expand All @@ -43,44 +48,19 @@ function diagnosticCallback(callback) {
}

function buildWatchHooks(sys, callbacks) {
let watchedFiles = new Map();
let fileChanged = (type, path) => {
debugWatcher('file changed (%s) %s', type, path);
if (callbacks.watchedFileChanged) {
callbacks.watchedFileChanged();
}
};

return Object.assign({}, sys, {
watchFile(_file, callback) {
// tsc is inconsistent about whether it provides relative or absolute paths
let file = path.resolve(_file);

debugWatcher('watchFile %s', file);
watchedFiles.set(file, callback);

return {
close() {
watchedFiles.delete(file);
}
};
},

watchFile: null,
watchDirectory(dir, callback) {
if (!fs.existsSync(dir)) return;

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

watcher.on('all', (type, path) => {
if (type === 'add' && path.endsWith('.ts')) {
fileChanged(type, path);
callback(path);
} else if (watchedFiles.has(path)) {
fileChanged(type, path);
watchedFiles.get(path)(path, type === 'change' ? 1 : 2);
} else {
debugWatcher('unknown file event %s %s', type, path);
callback(path);

if (path.endsWith('.ts') && callbacks.watchedFileChanged) {
callbacks.watchedFileChanged();
}
});

Expand Down