-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwatch.ts
109 lines (92 loc) · 3.41 KB
/
watch.ts
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
99
100
101
102
103
104
105
106
107
108
109
import { assemble } from "./assembler";
import { compileProgram, CompilerOptions } from "./compile";
import { ObjectToDesyncedString } from "./dsconvert";
import fs from "fs";
import { basename, dirname } from "path";
import ts = require("typescript");
Promise.all([import("clipboardy")]).then(([{ default: clipboard }]) => {
const formatHost: ts.FormatDiagnosticsHost = {
getCanonicalFileName: (path) => path,
getCurrentDirectory: ts.sys.getCurrentDirectory,
getNewLine: () => ts.sys.newLine,
};
function compile(
filename: string,
program: ts.SemanticDiagnosticsBuilderProgram,
) {
const asm = compileProgram(filename, program.getProgram());
const dir = dirname(filename);
const asmFilename = dir + "/out/" + basename(filename) + ".asm";
fs.writeFileSync(asmFilename, asm);
const obj = assemble(asm);
fs.writeFileSync(asmFilename + ".json", JSON.stringify(obj, undefined, 2));
let typ = "C";
if ("frame" in obj) {
typ = "B";
}
const str = ObjectToDesyncedString(obj, typ);
clipboard.writeSync(str);
fs.writeFileSync(asmFilename + ".txt", str, {
flush: true,
});
}
function watchMain() {
// TypeScript can use several different program creation "strategies":
// * ts.createEmitAndSemanticDiagnosticsBuilderProgram,
// * ts.createSemanticDiagnosticsBuilderProgram
// * ts.createAbstractBuilder
// The first two produce "builder programs". These use an incremental strategy
// to only re-check and emit files whose contents may have changed, or whose
// dependencies may have changes which may impact change the result of prior
// type-check and emit.
// The last uses an ordinary program which does a full type check after every
// change.
// Between `createEmitAndSemanticDiagnosticsBuilderProgram` and
// `createSemanticDiagnosticsBuilderProgram`, the only difference is emit.
// For pure type-checking scenarios, or when another tool/process handles emit,
// using `createSemanticDiagnosticsBuilderProgram` may be more desirable.
const createProgram = ts.createSemanticDiagnosticsBuilderProgram;
const filename = process.argv[2];
const files = [filename, `${__dirname}/behavior.d.ts`];
const host = ts.createWatchCompilerHost(
files,
CompilerOptions,
ts.sys,
createProgram,
reportDiagnostic,
reportWatchStatusChanged,
);
const origPostProgramCreate = host.afterProgramCreate;
host.afterProgramCreate = (program) => {
try {
compile(filename, program);
console.log("** Compilation finished, copied to clipboard! **");
} catch (e) {
console.error(e);
}
origPostProgramCreate!(program);
};
// `createWatchProgram` creates an initial program, watches files, and updates
// the program over time.
ts.createWatchProgram(host);
}
function reportDiagnostic(diagnostic: ts.Diagnostic) {
console.error(
"Error",
diagnostic.code,
":",
ts.flattenDiagnosticMessageText(
diagnostic.messageText,
formatHost.getNewLine(),
),
);
}
/**
* Prints a diagnostic every time the watch status changes.
* This is mainly for messages like "Starting compilation" or "Compilation completed".
*/
function reportWatchStatusChanged(diagnostic: ts.Diagnostic) {
console.info(ts.formatDiagnostic(diagnostic, formatHost));
}
watchMain();
});