(importMetaMain: boolean, importMetaPath: string)
| 112 | export const dummySpan = createSpan('dummy'); |
| 113 | |
| 114 | export function task(importMetaMain: boolean, importMetaPath: string) { |
| 115 | return (fn: (span: Span, onCleanup: (cb: () => Promise<void> | void) => void) => Promise<unknown>, customName?: string) => { |
| 116 | const taskName = customName ?? basename(importMetaPath, extname(importMetaPath)); |
| 117 | let cleanup: () => Promise<void> | void = noop; |
| 118 | const onCleanup = (cb: () => void) => { |
| 119 | cleanup = cb; |
| 120 | }; |
| 121 | |
| 122 | if (importMetaMain) { |
| 123 | const innerSpan = createSpan(taskName); |
| 124 | |
| 125 | process.on('uncaughtException', (error) => { |
| 126 | console.error('Uncaught exception:', error); |
| 127 | process.exit(1); |
| 128 | }); |
| 129 | process.on('unhandledRejection', (reason) => { |
| 130 | console.error('Unhandled rejection:', reason); |
| 131 | process.exit(1); |
| 132 | }); |
| 133 | |
| 134 | innerSpan.traceChildAsync('dummy', (childSpan) => fn(childSpan, onCleanup)).finally(() => { |
| 135 | innerSpan.stop(); |
| 136 | printTraceResult(innerSpan.traceResult); |
| 137 | process.nextTick(whyIsNodeRunning); |
| 138 | process.nextTick(() => process.exit(0)); |
| 139 | }); |
| 140 | } |
| 141 | |
| 142 | let runSpan: Span; |
| 143 | async function run(parentSpan?: Span | null): Promise<TraceResult> { |
| 144 | if (parentSpan) { |
| 145 | runSpan = parentSpan.traceChild(taskName); |
| 146 | } else { |
| 147 | runSpan = createSpan(taskName); |
| 148 | } |
| 149 | |
| 150 | try { |
| 151 | await fn(runSpan, onCleanup); |
| 152 | } finally { |
| 153 | runSpan.stop(); |
| 154 | cleanup(); |
| 155 | } |
| 156 | |
| 157 | return runSpan.traceResult; |
| 158 | } |
| 159 | |
| 160 | return Object.assign(run, { |
| 161 | getInternalTraceResult: () => runSpan.traceResult |
| 162 | }); |
| 163 | }; |
| 164 | } |
| 165 | |
| 166 | export async function whyIsNodeRunning() { |
| 167 | if (isCI && process.env.RUNNER_DEBUG === '1') { |
no test coverage detected