|
| 1 | +/* THIS FILE GENERATED FROM .ts; see BUILD.bazel */ /* clang-format off */"use strict"; |
| 2 | +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { |
| 3 | + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } |
| 4 | + return new (P || (P = Promise))(function (resolve, reject) { |
| 5 | + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } |
| 6 | + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } |
| 7 | + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } |
| 8 | + step((generator = generator.apply(thisArg, _arguments || [])).next()); |
| 9 | + }); |
| 10 | +}; |
| 11 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 12 | +const fs = require("fs"); |
| 13 | +const ts = require("typescript"); |
| 14 | +const MNEMONIC = 'TsProject'; |
| 15 | +const worker = require('./worker'); |
| 16 | +let createWatchCompilerHost; |
| 17 | +const formatHost = { |
| 18 | + getCanonicalFileName: (path) => path, |
| 19 | + getCurrentDirectory: ts.sys.getCurrentDirectory, |
| 20 | + getNewLine: () => ts.sys.newLine, |
| 21 | +}; |
| 22 | +const reportDiagnostic = (diagnostic) => { |
| 23 | + worker.log(ts.formatDiagnostic(diagnostic, formatHost)); |
| 24 | +}; |
| 25 | +const reportWatchStatusChanged = (diagnostic) => { |
| 26 | + worker.debug(ts.formatDiagnostic(diagnostic, formatHost)); |
| 27 | +}; |
| 28 | +function createWatchProgram(options, tsconfigPath, setTimeout) { |
| 29 | + const host = createWatchCompilerHost(tsconfigPath, options, Object.assign(Object.assign({}, ts.sys), { setTimeout }), ts.createEmitAndSemanticDiagnosticsBuilderProgram, reportDiagnostic, reportWatchStatusChanged); |
| 30 | + return ts.createWatchProgram(host); |
| 31 | +} |
| 32 | +let workerRequestTimestamp; |
| 33 | +let cachedWatchedProgram; |
| 34 | +let consolidateChangesCallback; |
| 35 | +let cachedWatchProgramArgs; |
| 36 | +function getWatchProgram(args) { |
| 37 | + const newWatchArgs = args.join(' '); |
| 38 | + if (cachedWatchedProgram && cachedWatchProgramArgs && cachedWatchProgramArgs !== newWatchArgs) { |
| 39 | + cachedWatchedProgram.close(); |
| 40 | + cachedWatchedProgram = undefined; |
| 41 | + cachedWatchProgramArgs = undefined; |
| 42 | + } |
| 43 | + if (!cachedWatchedProgram) { |
| 44 | + const parsedArgs = ts.parseCommandLine(args); |
| 45 | + const tsconfigPath = args[args.indexOf('--project') + 1]; |
| 46 | + cachedWatchProgramArgs = newWatchArgs; |
| 47 | + cachedWatchedProgram = createWatchProgram(parsedArgs.options, tsconfigPath, (callback) => { |
| 48 | + consolidateChangesCallback = callback; |
| 49 | + }); |
| 50 | + } |
| 51 | + return cachedWatchedProgram; |
| 52 | +} |
| 53 | +function emitOnce(args) { |
| 54 | + var _a; |
| 55 | + return __awaiter(this, void 0, void 0, function* () { |
| 56 | + const watchProgram = getWatchProgram(args); |
| 57 | + if (consolidateChangesCallback) { |
| 58 | + consolidateChangesCallback(); |
| 59 | + } |
| 60 | + workerRequestTimestamp = Date.now(); |
| 61 | + const result = yield ((_a = watchProgram) === null || _a === void 0 ? void 0 : _a.getProgram().emit(undefined, undefined, { |
| 62 | + isCancellationRequested: function (timestamp) { |
| 63 | + return timestamp !== workerRequestTimestamp; |
| 64 | + }.bind(null, workerRequestTimestamp), |
| 65 | + throwIfCancellationRequested: function (timestamp) { |
| 66 | + if (timestamp !== workerRequestTimestamp) { |
| 67 | + throw new ts.OperationCanceledException(); |
| 68 | + } |
| 69 | + }.bind(null, workerRequestTimestamp), |
| 70 | + })); |
| 71 | + return Boolean(result && result.diagnostics.length === 0); |
| 72 | + }); |
| 73 | +} |
| 74 | +function main() { |
| 75 | + const typescriptRequirePath = process.argv[process.argv.indexOf('--typescript_require_path') + 1]; |
| 76 | + try { |
| 77 | + const customTypescriptModule = require(typescriptRequirePath); |
| 78 | + createWatchCompilerHost = customTypescriptModule.createWatchCompilerHost; |
| 79 | + } |
| 80 | + catch (e) { |
| 81 | + worker.log(`typescript_require_path '${typescriptRequirePath}' could not be resolved`); |
| 82 | + throw e; |
| 83 | + } |
| 84 | + if (process.argv.includes('--persistent_worker')) { |
| 85 | + worker.log(`Running ${MNEMONIC} as a Bazel worker`); |
| 86 | + worker.runWorkerLoop(emitOnce); |
| 87 | + } |
| 88 | + else { |
| 89 | + worker.log(`Running ${MNEMONIC} as a standalone process`); |
| 90 | + worker.log(`Started a new process to perform this action. Your build might be misconfigured, try |
| 91 | + --strategy=${MNEMONIC}=worker`); |
| 92 | + let argsFilePath = process.argv.pop(); |
| 93 | + if (argsFilePath.startsWith('@')) { |
| 94 | + argsFilePath = argsFilePath.slice(1); |
| 95 | + } |
| 96 | + const args = fs.readFileSync(argsFilePath).toString().split('\n'); |
| 97 | + emitOnce(args).finally(() => { var _a; return (_a = cachedWatchedProgram) === null || _a === void 0 ? void 0 : _a.close(); }); |
| 98 | + } |
| 99 | +} |
| 100 | +if (require.main === module) { |
| 101 | + main(); |
| 102 | +} |
0 commit comments