-
Notifications
You must be signed in to change notification settings - Fork 522
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(typescript): create a better ts_project worker
- Loading branch information
Dan Muller
committed
Jan 26, 2021
1 parent
3eb4260
commit c8be6a7
Showing
13 changed files
with
328 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1807,7 +1807,7 @@ has-values@^1.0.0: | |
is-number "^3.0.0" | ||
kind-of "^4.0.0" | ||
|
||
has@^1.0.1: | ||
has@^1.0.1, has@^1.0.3: | ||
version "1.0.3" | ||
resolved "https://registry.yarnpkg.com/has/-/has-1.0.3.tgz#722d7cbfc1f6aa8241f16dd814e011e1f41e8796" | ||
integrity sha512-f2dvO0VU6Oej7RkWJGrehjbzMAjFp5/VKPp5tTpWIV4JHHZK1/BxbFRtf/siA2SWTe09caDmVtYYzWEIbBS4zw== | ||
|
@@ -2109,6 +2109,13 @@ is-buffer@^1.1.5: | |
resolved "https://registry.yarnpkg.com/is-buffer/-/is-buffer-1.1.6.tgz#efaa2ea9daa0d7ab2ea13a97b2b8ad51fefbe8be" | ||
integrity sha512-NcdALwpXkTm5Zvvbk7owOUSvVvBKDgKP5/ewfXEznmQFfs4ZRmanOeKBTjRVjka3QFoN6XJ+9F3USqfHqTaU5w== | ||
|
||
is-core-module@^2.1.0: | ||
version "2.2.0" | ||
resolved "https://registry.yarnpkg.com/is-core-module/-/is-core-module-2.2.0.tgz#97037ef3d52224d85163f5597b2b63d9afed981a" | ||
integrity sha512-XRAfAdyyY5F5cOXn7hYQDqh2Xmii+DEfIcQGxK/uNwMHhIkPWO0g8msXcbzLe+MpGoR951MlqM/2iIlU4vKDdQ== | ||
dependencies: | ||
has "^1.0.3" | ||
|
||
is-data-descriptor@^0.1.4: | ||
version "0.1.4" | ||
resolved "https://registry.yarnpkg.com/is-data-descriptor/-/is-data-descriptor-0.1.4.tgz#0b5ee648388e2c860282e793f1856fec3f301b56" | ||
|
@@ -3032,6 +3039,11 @@ path-key@^2.0.0, path-key@^2.0.1: | |
resolved "https://registry.yarnpkg.com/path-key/-/path-key-2.0.1.tgz#411cadb574c5a140d3a4b1910d40d80cc9f40b40" | ||
integrity sha1-QRyttXTFoUDTpLGRDUDYDMn0C0A= | ||
|
||
path-parse@^1.0.6: | ||
version "1.0.6" | ||
resolved "https://registry.yarnpkg.com/path-parse/-/path-parse-1.0.6.tgz#d62dbb5679405d72c4737ec58600e9ddcf06d24c" | ||
integrity sha512-GSmOT2EbHrINBf9SR7CDELwlJ8AENk3Qn7OikK4nFYAu3Ote2+JYNVvkpAEQm3/TLNEJFD/xZJjzyxg3KBWOzw== | ||
|
||
[email protected]: | ||
version "0.1.7" | ||
resolved "https://registry.yarnpkg.com/path-to-regexp/-/path-to-regexp-0.1.7.tgz#df604178005f522f15eb4490e7247a1bfaa67f8c" | ||
|
@@ -3462,6 +3474,14 @@ resolve-url@^0.2.1: | |
resolved "https://registry.yarnpkg.com/resolve-url/-/resolve-url-0.2.1.tgz#2c637fe77c893afd2a663fe21aa9080068e2052a" | ||
integrity sha1-LGN/53yJOv0qZj/iGqkIAGjiBSo= | ||
|
||
resolve@>=1.9.0: | ||
version "1.19.0" | ||
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.19.0.tgz#1af5bf630409734a067cae29318aac7fa29a267c" | ||
integrity sha512-rArEXAgsBG4UgRGcynxWIWKFvh/XZCcS8UJdHhwy91zwAvCZIbcs+vAbflgBnNjYMs/i/i+/Ux6IZhML1yPvxg== | ||
dependencies: | ||
is-core-module "^2.1.0" | ||
path-parse "^1.0.6" | ||
|
||
ret@~0.1.10: | ||
version "0.1.15" | ||
resolved "https://registry.yarnpkg.com/ret/-/ret-0.1.15.tgz#b8a4825d5bdb1fc3f6f53c2bc33f81388681c7bc" | ||
|
@@ -4080,6 +4100,13 @@ [email protected]: | |
resolved "https://registry.yarnpkg.com/tty-browserify/-/tty-browserify-0.0.0.tgz#a157ba402da24e9bf957f9aa69d524eed42901a6" | ||
integrity sha1-oVe6QC2iTpv5V/mqadUk7tQpAaY= | ||
|
||
ttypescript@^1.5.12: | ||
version "1.5.12" | ||
resolved "https://registry.yarnpkg.com/ttypescript/-/ttypescript-1.5.12.tgz#27a8356d7d4e719d0075a8feb4df14b52384f044" | ||
integrity sha512-1ojRyJvpnmgN9kIHmUnQPlEV1gq+VVsxVYjk/NfvMlHSmYxjK5hEvOOU2MQASrbekTUiUM7pR/nXeCc8bzvMOQ== | ||
dependencies: | ||
resolve ">=1.9.0" | ||
|
||
type-is@~1.6.17, type-is@~1.6.18: | ||
version "1.6.18" | ||
resolved "https://registry.yarnpkg.com/type-is/-/type-is-1.6.18.tgz#4e552cd05df09467dcbc4ef739de89f2cf37c131" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* THIS FILE GENERATED FROM .ts; see BUILD.bazel */ /* clang-format off */"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const fs = require("fs"); | ||
const ts = require("typescript"); | ||
const MNEMONIC = "TsProject"; | ||
const worker = require("./worker"); | ||
let createWatchCompilerHost; | ||
const formatHost = { | ||
getCanonicalFileName: (path) => path, | ||
getCurrentDirectory: ts.sys.getCurrentDirectory, | ||
getNewLine: () => ts.sys.newLine, | ||
}; | ||
const reportDiagnostic = (diagnostic) => { | ||
worker.log(ts.formatDiagnostic(diagnostic, formatHost)); | ||
}; | ||
const reportWatchStatusChanged = (diagnostic) => { | ||
worker.debug(ts.formatDiagnostic(diagnostic, formatHost)); | ||
}; | ||
function createWatchProgram(options, tsconfigPath, setTimeout) { | ||
const host = createWatchCompilerHost(tsconfigPath, options, Object.assign(Object.assign({}, ts.sys), { setTimeout }), ts.createEmitAndSemanticDiagnosticsBuilderProgram, reportDiagnostic, reportWatchStatusChanged); | ||
return ts.createWatchProgram(host); | ||
} | ||
let workerRequestTimestamp; | ||
let cachedWatchedProgram; | ||
let consolidateChangesCallback; | ||
let cachedWatchProgramArgs; | ||
function getWatchProgram(args) { | ||
const newWatchArgs = args.join(" "); | ||
if (cachedWatchedProgram && | ||
cachedWatchProgramArgs && | ||
cachedWatchProgramArgs !== newWatchArgs) { | ||
cachedWatchedProgram.close(); | ||
cachedWatchedProgram = undefined; | ||
cachedWatchProgramArgs = undefined; | ||
} | ||
if (!cachedWatchedProgram) { | ||
const parsedArgs = ts.parseCommandLine(args); | ||
const tsconfigPath = args[args.indexOf("--project") + 1]; | ||
cachedWatchProgramArgs = newWatchArgs; | ||
cachedWatchedProgram = createWatchProgram(parsedArgs.options, tsconfigPath, (callback) => { | ||
consolidateChangesCallback = callback; | ||
}); | ||
} | ||
return cachedWatchedProgram; | ||
} | ||
function emitOnce(args) { | ||
const watchProgram = getWatchProgram(args); | ||
if (consolidateChangesCallback) { | ||
consolidateChangesCallback(); | ||
} | ||
return new Promise((res) => { | ||
var _a; | ||
workerRequestTimestamp = Date.now(); | ||
const result = (_a = watchProgram) === null || _a === void 0 ? void 0 : _a.getProgram().emit(undefined, undefined, { | ||
isCancellationRequested: function (timestamp) { | ||
return timestamp !== workerRequestTimestamp; | ||
}.bind(null, workerRequestTimestamp), | ||
throwIfCancellationRequested: function (timestamp) { | ||
if (timestamp !== workerRequestTimestamp) { | ||
throw new ts.OperationCanceledException(); | ||
} | ||
}.bind(null, workerRequestTimestamp), | ||
}); | ||
res(result && result.diagnostics.length === 0); | ||
}); | ||
} | ||
function main() { | ||
const typescriptModuleName = process.argv[process.argv.indexOf('--typescript_module_name') + 1]; | ||
try { | ||
const customTypescriptModule = require(typescriptModuleName); | ||
createWatchCompilerHost = customTypescriptModule.createWatchCompilerHost; | ||
} | ||
catch (e) { | ||
worker.log('typescript_module_name must be a requirable module path'); | ||
throw e; | ||
} | ||
if (process.argv.includes("--persistent_worker")) { | ||
worker.log(`Running ${MNEMONIC} as a Bazel worker`); | ||
worker.runWorkerLoop(emitOnce); | ||
} | ||
else { | ||
worker.log(`Running ${MNEMONIC} as a standalone process`); | ||
worker.log(`Started a new process to perform this action. Your build might be misconfigured, try | ||
--strategy=${MNEMONIC}=worker`); | ||
let argsFilePath = process.argv.pop(); | ||
if (argsFilePath.startsWith('@')) { | ||
argsFilePath = argsFilePath.slice(1); | ||
} | ||
const args = fs.readFileSync(argsFilePath).toString().split('\n'); | ||
emitOnce(args).finally(() => { var _a; return (_a = cachedWatchedProgram) === null || _a === void 0 ? void 0 : _a.close(); }); | ||
} | ||
} | ||
main(); |
Oops, something went wrong.