Skip to content

Instantly share code, notes, and snippets.

@basarat
Created January 22, 2015 05:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save basarat/84caf94c8611ac890e37 to your computer and use it in GitHub Desktop.
Save basarat/84caf94c8611ac890e37 to your computer and use it in GitHub Desktop.
/// <reference path="typings/node/node.d.ts" />
/// <reference path="typings/typescript/typescript.d.ts" />
import fs = require("fs");
import ts = require("typescript");
import path = require("path");
function watch(filenames: string[], options: ts.CompilerOptions) {
var files: ts.Map<{ version: number; text: string; }> = {};
// Add the default library file
filenames.unshift(path.join(path.dirname(require.resolve('typescript')), 'lib.d.ts'));
// initialize the list of files
filenames.forEach(filename => {
files[filename] = { version: 0, text: fs.readFileSync(filename).toString() };
});
// Create the language service host to allow the LS to communicate with the host
var servicesHost: ts.LanguageServiceHost = {
getScriptFileNames: () => filenames,
getScriptVersion: (filename) => files[filename] && files[filename].version.toString(),
getScriptSnapshot: (filename) => {
var file = files[filename];
return {
getText: (start, end) => file.text.substring(start, end),
getLength: () => file.text.length,
getLineStartPositions: () => [],
getChangeRange: (oldSnapshot) => undefined
};
},
getCurrentDirectory: () => process.cwd(),
getScriptIsOpen: () => true,
getCompilationSettings: () => options,
getDefaultLibFilename:(options) => 'lib.d.ts',
log: (message) => console.log(message)
};
// Create the language service files
var services = ts.createLanguageService(servicesHost, ts.createDocumentRegistry())
// Now let's watch the files
filenames.forEach(filename => {
// First time around, emit all files
emitFile(filename);
// Add a watch on the file to handle next change
fs.watchFile(filename,
{ persistent: true, interval: 250 },
(curr, prev) => {
// Check timestamp
if (+curr.mtime <= +prev.mtime) {
return;
}
var file = files[filename];
// Update the version to signal a change in the file
file.version++;
// Clear the text to force a new read
file.text = fs.readFileSync(filename).toString();
// write the changes to disk
emitFile(filename);
});
});
function emitFile(filename: string) {
var output = services.getEmitOutput(filename);
if (output.emitOutputStatus === ts.EmitReturnStatus.Succeeded) {
console.log(`Emitting ${filename}`);
}
else {
console.log(`Emitting ${filename} failed`);
var allDiagnostics = services.getCompilerOptionsDiagnostics()
.concat(services.getSyntacticDiagnostics(filename))
.concat(services.getSemanticDiagnostics(filename));
allDiagnostics.forEach(diagnostic => {
var lineChar = diagnostic.file.getLineAndCharacterFromPosition(diagnostic.start);
console.log(` ${diagnostic.file && diagnostic.file.filename} (${lineChar.line},${lineChar.character}): ${diagnostic.messageText}`);
});
}
output.outputFiles.forEach(o => {
fs.writeFileSync(o.name, o.text, "utf8");
});
}
}
// Initialize files constituting the program as all .ts files in the current directory
var currentDirectoryFiles = fs.readdirSync(process.cwd()).
filter(filename=> filename.length >= 3 && filename.substr(filename.length - 3, 3) === ".ts");
//map(filename => path.join(process.cwd(), filename));
// Start the watcher
w
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment