()
| 519 | } |
| 520 | |
| 521 | private executeJs(): any { |
| 522 | const { transpiledFiles } = this.getJsResult(); |
| 523 | // Custom require for extra files. Really basic. Global support is hacky |
| 524 | // TODO Should be replaced with vm.Module https://nodejs.org/api/vm.html#vm_class_vm_module |
| 525 | // once stable |
| 526 | const globalContext: any = {}; |
| 527 | const mainExports = {}; |
| 528 | globalContext.exports = mainExports; |
| 529 | globalContext.module = { exports: mainExports }; |
| 530 | globalContext.require = (fileName: string) => { |
| 531 | // create clean export object for "module" |
| 532 | const moduleExports = {}; |
| 533 | globalContext.exports = moduleExports; |
| 534 | globalContext.module = { exports: moduleExports }; |
| 535 | const baseName = fileName.replace("./", ""); |
| 536 | const transpiledExtraFile = transpiledFiles.find(({ sourceFiles }) => |
| 537 | sourceFiles.some(f => f.fileName === baseName + ".ts" || f.fileName === baseName + "/index.ts") |
| 538 | ); |
| 539 | |
| 540 | if (transpiledExtraFile?.js) { |
| 541 | vm.runInContext(transpiledExtraFile.js, globalContext); |
| 542 | } |
| 543 | |
| 544 | // Have to return globalContext.module.exports |
| 545 | // becuase module.exports might no longer be equal to moduleExports (export assignment) |
| 546 | const result = globalContext.module.exports; |
| 547 | // Reset module/export |
| 548 | globalContext.exports = mainExports; |
| 549 | globalContext.module = { exports: mainExports }; |
| 550 | return result; |
| 551 | }; |
| 552 | |
| 553 | vm.createContext(globalContext); |
| 554 | |
| 555 | let result: unknown; |
| 556 | try { |
| 557 | result = vm.runInContext(this.getJsCodeWithWrapper(), globalContext); |
| 558 | } catch (error) { |
| 559 | const hasMessage = (error: any): error is { message: string } => error.message !== undefined; |
| 560 | assert(hasMessage(error)); |
| 561 | return new ExecutionError(error.message); |
| 562 | } |
| 563 | |
| 564 | function removeUndefinedFields(obj: any): any { |
| 565 | if (obj === null) { |
| 566 | return undefined; |
| 567 | } |
| 568 | |
| 569 | if (Array.isArray(obj)) { |
| 570 | return obj.map(removeUndefinedFields); |
| 571 | } |
| 572 | |
| 573 | if (typeof obj === "object") { |
| 574 | const copy: any = {}; |
| 575 | for (const [key, value] of Object.entries(obj)) { |
| 576 | if (obj[key] !== undefined) { |
| 577 | copy[key] = removeUndefinedFields(value); |
| 578 | } |
nothing calls this directly
no test coverage detected