-
Notifications
You must be signed in to change notification settings - Fork 0
/
watch-electron.js
98 lines (86 loc) · 3.14 KB
/
watch-electron.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
var path = require('path');
var fs = require('fs');
var ts = require('typescript');
fs.exists(__dirname + '/dist/', function(exists) {
if (!exists) {
fs.mkdir(__dirname + '/dist/', function() {
watchFiles();
});
} else {
watchFiles();
}
});
function watchFiles() {
watch([
'electron/server.ts',
'electron/config.ts',
'electron/database.ts',
'electron/main.ts'
]);
}
function watch(rootFileNames) {
var options = {module: ts.ModuleKind.CommonJS};
var files = {};
rootFileNames.forEach(function(fileName) {
files[fileName] = {version: 0};
});
var servicesHost = {
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(),
getCompilationSettings: () => options,
getDefaultLibFileName: (options) => ts.getDefaultLibFilePath(options),
fileExists: ts.sys.fileExists,
readFile: ts.sys.readFile,
readDirectory: ts.sys.readDirectory
};
var services = ts.createLanguageService(servicesHost, ts.createDocumentRegistry());
rootFileNames.forEach(function(fileName) {
emitFile(fileName);
fs.watchFile(fileName,
{
persistent: true,
interval: 250
},
function(curr, prev) {
if (+curr.mtime <= +prev.mtime) {
return;
}
files[fileName].version++;
emitFile(fileName);
}
);
});
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(function(o) {
console.log(path.basename(o.name));
fs.writeFileSync(__dirname + '/dist/' + path.basename(o.name), o.text, "utf8");
});
}
function logErrors(fileName) {
var allDiagnostics = services.getCompilerOptionsDiagnostics().concat(services.getSyntacticDiagnostics(fileName)).concat(services.getSemanticDiagnostics(fileName));
allDiagnostics.forEach(function(diagnostic) {
var 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}`);
}
});
}
}