|
9 | 9 | /// <reference types="node"/> |
10 | 10 | /// <reference lib="es2020"/> |
11 | 11 |
|
12 | | -import {Extractor, ExtractorConfig, IConfigFile, IExtractorConfigPrepareOptions,} from '@microsoft/api-extractor'; |
| 12 | +import {runAsWorker, runWorkerLoop} from '@bazel/worker'; |
| 13 | +import {Extractor, ExtractorConfig, ExtractorMessage, IConfigFile, IExtractorConfigPrepareOptions} from '@microsoft/api-extractor'; |
13 | 14 | import * as fs from 'fs'; |
14 | 15 | import * as path from 'path'; |
15 | 16 |
|
@@ -55,7 +56,8 @@ export async function runMain( |
55 | 56 | }; |
56 | 57 |
|
57 | 58 | const extractorConfig = ExtractorConfig.prepare(options); |
58 | | - const {succeeded} = Extractor.invoke(extractorConfig); |
| 59 | + const {succeeded} = |
| 60 | + Extractor.invoke(extractorConfig, {messageCallback: handleApiExtractorMessage}); |
59 | 61 |
|
60 | 62 | if (!succeeded) { |
61 | 63 | throw new Error('Type bundling failed. See error above.'); |
@@ -89,12 +91,51 @@ function stripAmdModuleDirectiveComments(content: string): string { |
89 | 91 | return content.replace(/^\/\/\/ <amd-module name=.*\/>[\r\n]+/gm, ''); |
90 | 92 | } |
91 | 93 |
|
92 | | -// Entry point |
93 | | -const [entryPointExecpath, outputExecpath, packageJsonExecpath, licenseBannerExecpath] = |
94 | | - process.argv.slice(2); |
| 94 | +/** |
| 95 | + * Handles logging messages from API extractor. |
| 96 | + * |
| 97 | + * Certain info messages should be omitted and other messages should be printed |
| 98 | + * to stderr to avoid worker protocol conflicts. |
| 99 | + */ |
| 100 | +function handleApiExtractorMessage(msg: ExtractorMessage): void { |
| 101 | + msg.handled = true; |
| 102 | + |
| 103 | + if (msg.messageId === 'console-compiler-version-notice' || msg.messageId === 'console-preamble') { |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + if (msg.logLevel !== 'verbose' && msg.logLevel !== 'none') { |
| 108 | + console.error(msg.text); |
| 109 | + } |
| 110 | +} |
95 | 111 |
|
96 | | -runMain({entryPointExecpath, outputExecpath, packageJsonExecpath, licenseBannerExecpath}) |
97 | | - .catch(e => { |
98 | | - console.error(e); |
| 112 | +/** Runs one build using the specified build action command line arguments. */ |
| 113 | +async function runOneBuild(args: string[]): Promise<boolean> { |
| 114 | + const [entryPointExecpath, outputExecpath, packageJsonExecpath, licenseBannerExecpath] = args; |
| 115 | + |
| 116 | + try { |
| 117 | + await runMain({entryPointExecpath, outputExecpath, packageJsonExecpath, licenseBannerExecpath}); |
| 118 | + return true; |
| 119 | + } catch (e) { |
| 120 | + console.error(e); |
| 121 | + return false; |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +// Entry-point. |
| 126 | +const processArgs = process.argv.slice(2); |
| 127 | + |
| 128 | +if (runAsWorker(processArgs)) { |
| 129 | + runWorkerLoop(runOneBuild); |
| 130 | +} else { |
| 131 | + // In non-worker mode we need to manually read the flag file and omit |
| 132 | + // the leading `@` that is added as part of the worker requirements. |
| 133 | + const flagFile = processArgs[0].substring(1); |
| 134 | + const args = fs.readFileSync(flagFile, 'utf8').split('\n'); |
| 135 | + |
| 136 | + runOneBuild(args).then(success => { |
| 137 | + if (!success) { |
99 | 138 | process.exitCode = 1; |
100 | | - }); |
| 139 | + } |
| 140 | + }); |
| 141 | +} |
0 commit comments