ph-dev-tools
Version:
Development Tools for PHibernate
94 lines • 4.22 kB
JavaScript
/**
* Created by Papa on 3/30/2016.
*/
;
const fs = require("fs");
const ts = require("typescript");
const EntityDefinitionGenerator_1 = require("./parser/EntityDefinitionGenerator");
const QEntityFileBuilder_1 = require("./builder/entity/QEntityFileBuilder");
const PathBuilder_1 = require("./builder/PathBuilder");
function watchFiles(configuration, options, rootFileNames) {
const files = {};
const pathBuilder = new PathBuilder_1.PathBuilder(configuration);
// initialize the list of files
rootFileNames.forEach(fileName => {
files[fileName] = { version: 0 };
});
// Create the language service host to allow the LS to communicate with the host
const servicesHost = {
getCompilationSettings: () => options,
getScriptFileNames: () => rootFileNames,
getScriptVersion: (fileName) => files[fileName] && files[fileName].version.toString(),
getScriptSnapshot: (fileName) => {
if (!fs.existsSync(fileName)) {
return undefined;
}
return ts.ScriptSnapshot.fromString(fs.readFileSync(fileName).toString());
},
getCurrentDirectory: () => process.cwd(),
getDefaultLibFileName: (options) => ts.getDefaultLibFilePath(options)
};
// Create the language service files
const services = ts.createLanguageService(servicesHost, ts.createDocumentRegistry());
// First time around, process all files
processFiles(rootFileNames, options);
// Now let's watch the files
rootFileNames.forEach(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;
}
// Update the version to signal a change in the file
files[fileName].version++;
// process file
processFiles([fileName], options);
});
});
function processFiles(rootFileNames, options) {
options.target = ts.ScriptTarget.ES5;
let entities = EntityDefinitionGenerator_1.generateEntityDefinitions(rootFileNames, options);
emitFiles(entities);
}
function emitFiles(entities) {
entities.forEach((entity) => {
// TODO: work here next - read all of the configured templates and generate all of the artifacts
let fullGenerationPath = pathBuilder.getFullPathToGeneratedSource(entity.path);
let entityFileBuilder = new QEntityFileBuilder_1.QEntityFileBuilder(entity, fullGenerationPath, pathBuilder.workingDirPath);
let generationPath = pathBuilder.setupFileForGeneration(entity.path);
var entitySourceString = entityFileBuilder.build();
fs.writeFileSync(generationPath, entitySourceString);
});
}
function emitFile(fileName) {
let output = services.getEmitOutput(fileName);
if (!output.emitSkipped) {
console.log(`Emitting ${fileName}`);
}
else {
console.log(`Emitting ${fileName} failed`);
logErrors(fileName);
}
output.outputFiles.forEach(o => {
fs.writeFileSync(o.name, o.text, "utf8");
});
}
function logErrors(fileName) {
let allDiagnostics = services.getCompilerOptionsDiagnostics()
.concat(services.getSyntacticDiagnostics(fileName))
.concat(services.getSemanticDiagnostics(fileName));
allDiagnostics.forEach(diagnostic => {
let message = ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n");
if (diagnostic.file) {
let { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
console.log(` Error ${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`);
}
else {
console.log(` Error: ${message}`);
}
});
}
}
exports.watchFiles = watchFiles;
//# sourceMappingURL=FileWatcher.js.map