|
| 1 | +//// [APISample_Watch.ts] |
| 2 | +/* |
| 3 | + * Note: This test is a public API sample. The sample sources can be found |
| 4 | + at: https://github.com/Microsoft/TypeScript-wiki/blob/master/Using-the-Compiler-API.md#writing-an-incremental-program-watcher |
| 5 | + * Please log a "breaking change" issue for any API breaking change affecting this issue |
| 6 | + */ |
| 7 | + |
| 8 | +declare var process: any; |
| 9 | +declare var console: any; |
| 10 | +declare var os: any; |
| 11 | + |
| 12 | +import ts = require("typescript"); |
| 13 | + |
| 14 | +const formatHost: ts.FormatDiagnosticsHost = { |
| 15 | + getCanonicalFileName: path => path, |
| 16 | + getCurrentDirectory: ts.sys.getCurrentDirectory, |
| 17 | + getNewLine: () => ts.sys.newLine, |
| 18 | +} |
| 19 | + |
| 20 | +function watchMain() { |
| 21 | + const configPath = ts.findConfigFile(/*searchPath*/ "./", ts.sys.fileExists, "tsconfig.json"); |
| 22 | + if (!configPath) { |
| 23 | + throw new Error("Could not find a valid 'tsconfig.json'."); |
| 24 | + } |
| 25 | + |
| 26 | + // TypeScript can use several different program creation "strategies": |
| 27 | + // * ts.createEmitAndSemanticDiagnosticsBuilderProgram, |
| 28 | + // * ts.createSemanticDiagnosticsBuilderProgram |
| 29 | + // * ts.createAbstractBuilder |
| 30 | + // The first two produce "builder programs". These use an incremental strategy to only re-check and emit files whose |
| 31 | + // contents may have changed, or whose dependencies may have changes which may impact change the result of prior type-check and emit. |
| 32 | + // The last uses an ordinary program which does a full type check after every change. |
| 33 | + // Between `createEmitAndSemanticDiagnosticsBuilderProgram` and `createSemanticDiagnosticsBuilderProgram`, the only difference is emit. |
| 34 | + // For pure type-checking scenarios, or when another tool/process handles emit, using `createSemanticDiagnosticsBuilderProgram` may be more desirable. |
| 35 | + |
| 36 | + // Note that there is another overload for `createWatchCompilerHost` that takes a set of root files. |
| 37 | + const host = ts.createWatchCompilerHost(configPath, {}, ts.sys, |
| 38 | + ts.createSemanticDiagnosticsBuilderProgram, |
| 39 | + reportDiagnostic, |
| 40 | + reportWatchStatusChanged, |
| 41 | + ); |
| 42 | + |
| 43 | + // You can technically override any given hook on the host, though you probably don't need to. |
| 44 | + // Note that we're assuming `origCreateProgram` and `origPostProgramCreate` doesn't use `this` at all. |
| 45 | + const origCreateProgram = host.createProgram; |
| 46 | + host.createProgram = (rootNames: ReadonlyArray<string>, options, host, oldProgram) => { |
| 47 | + console.log("** We're about to create the program! **"); |
| 48 | + return origCreateProgram(rootNames, options, host, oldProgram); |
| 49 | + } |
| 50 | + const origPostProgramCreate = host.afterProgramCreate; |
| 51 | + |
| 52 | + host.afterProgramCreate = program => { |
| 53 | + console.log("** We finished making the program! **"); |
| 54 | + origPostProgramCreate!(program); |
| 55 | + }; |
| 56 | + |
| 57 | + // `createWatchProgram` creates an initial program, watches files, and updates the program over time. |
| 58 | + ts.createWatchProgram(host); |
| 59 | +} |
| 60 | + |
| 61 | +function reportDiagnostic(diagnostic: ts.Diagnostic) { |
| 62 | + console.error("Error", diagnostic.code, ":", |
| 63 | + ts.flattenDiagnosticMessageText(diagnostic.messageText, formatHost.getNewLine()) |
| 64 | + ); |
| 65 | +} |
| 66 | + |
| 67 | +/** |
| 68 | + * Prints a diagnostic every time the watch status changes. |
| 69 | + * This is mainly for messages like "Starting compilation" or "Compilation completed". |
| 70 | + */ |
| 71 | +function reportWatchStatusChanged(diagnostic: ts.Diagnostic) { |
| 72 | + console.info(ts.formatDiagnostic(diagnostic, formatHost)); |
| 73 | +} |
| 74 | + |
| 75 | +watchMain(); |
| 76 | + |
| 77 | + |
| 78 | +//// [APISample_Watch.js] |
| 79 | +"use strict"; |
| 80 | +/* |
| 81 | + * Note: This test is a public API sample. The sample sources can be found |
| 82 | + at: https://github.com/Microsoft/TypeScript-wiki/blob/master/Using-the-Compiler-API.md#writing-an-incremental-program-watcher |
| 83 | + * Please log a "breaking change" issue for any API breaking change affecting this issue |
| 84 | + */ |
| 85 | +exports.__esModule = true; |
| 86 | +var ts = require("typescript"); |
| 87 | +var formatHost = { |
| 88 | + getCanonicalFileName: function (path) { return path; }, |
| 89 | + getCurrentDirectory: ts.sys.getCurrentDirectory, |
| 90 | + getNewLine: function () { return ts.sys.newLine; } |
| 91 | +}; |
| 92 | +function watchMain() { |
| 93 | + var configPath = ts.findConfigFile(/*searchPath*/ "./", ts.sys.fileExists, "tsconfig.json"); |
| 94 | + if (!configPath) { |
| 95 | + throw new Error("Could not find a valid 'tsconfig.json'."); |
| 96 | + } |
| 97 | + // TypeScript can use several different program creation "strategies": |
| 98 | + // * ts.createEmitAndSemanticDiagnosticsBuilderProgram, |
| 99 | + // * ts.createSemanticDiagnosticsBuilderProgram |
| 100 | + // * ts.createAbstractBuilder |
| 101 | + // The first two produce "builder programs". These use an incremental strategy to only re-check and emit files whose |
| 102 | + // contents may have changed, or whose dependencies may have changes which may impact change the result of prior type-check and emit. |
| 103 | + // The last uses an ordinary program which does a full type check after every change. |
| 104 | + // Between `createEmitAndSemanticDiagnosticsBuilderProgram` and `createSemanticDiagnosticsBuilderProgram`, the only difference is emit. |
| 105 | + // For pure type-checking scenarios, or when another tool/process handles emit, using `createSemanticDiagnosticsBuilderProgram` may be more desirable. |
| 106 | + // Note that there is another overload for `createWatchCompilerHost` that takes a set of root files. |
| 107 | + var host = ts.createWatchCompilerHost(configPath, {}, ts.sys, ts.createSemanticDiagnosticsBuilderProgram, reportDiagnostic, reportWatchStatusChanged); |
| 108 | + // You can technically override any given hook on the host, though you probably don't need to. |
| 109 | + // Note that we're assuming `origCreateProgram` and `origPostProgramCreate` doesn't use `this` at all. |
| 110 | + var origCreateProgram = host.createProgram; |
| 111 | + host.createProgram = function (rootNames, options, host, oldProgram) { |
| 112 | + console.log("** We're about to create the program! **"); |
| 113 | + return origCreateProgram(rootNames, options, host, oldProgram); |
| 114 | + }; |
| 115 | + var origPostProgramCreate = host.afterProgramCreate; |
| 116 | + host.afterProgramCreate = function (program) { |
| 117 | + console.log("** We finished making the program! **"); |
| 118 | + origPostProgramCreate(program); |
| 119 | + }; |
| 120 | + // `createWatchProgram` creates an initial program, watches files, and updates the program over time. |
| 121 | + ts.createWatchProgram(host); |
| 122 | +} |
| 123 | +function reportDiagnostic(diagnostic) { |
| 124 | + console.error("Error", diagnostic.code, ":", ts.flattenDiagnosticMessageText(diagnostic.messageText, formatHost.getNewLine())); |
| 125 | +} |
| 126 | +/** |
| 127 | + * Prints a diagnostic every time the watch status changes. |
| 128 | + * This is mainly for messages like "Starting compilation" or "Compilation completed". |
| 129 | + */ |
| 130 | +function reportWatchStatusChanged(diagnostic) { |
| 131 | + console.info(ts.formatDiagnostic(diagnostic, formatHost)); |
| 132 | +} |
| 133 | +watchMain(); |
0 commit comments