@@ -4,56 +4,53 @@ const require = topLevelCreateRequire(import.meta.url);
44import path from 'path' ;
55
66import { workerData , parentPort } from 'node:worker_threads' ;
7- import fs from 'fs/promises' ;
7+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
8+ import fs from 'fs/promises' ; // do not delete this line
89
910import { Logger } from '../logger.mjs' ;
1011
1112Logger . setVerbose ( workerData . verbose ) ;
12- // eslint-disable-next-line @typescript-eslint/no-unused-vars
13- const __dirname = path . resolve (
14- path . join (
15- ...[ workerData . projectDirname , workerData . subfolder , 'x' ] . filter ( ( p ) => p ) ,
16- ) ,
17- ) ;
1813
1914Logger . verbose ( `[CDK] [Worker] Started` ) ;
2015
2116parentPort . on ( 'message' , async ( data ) => {
22- // this is global variable to store the data from the CDK code once it is executed
23- global . lambdas = [ ] ;
24-
25- Logger . verbose ( `[Worker ${ workerData . workerId } ] Received message` , data ) ;
26-
27- // execute code to get the data into global.lambdas
28- const codeFile = await fs . readFile ( data . compileOutput , 'utf8' ) ;
29-
30- await fixCdkPaths ( workerData . awsCdkLibPath ) ;
31-
32- eval ( codeFile ) ;
33-
34- if ( ! global . lambdas || global . lambdas ?. length === 0 ) {
35- throw new Error ( 'No Lambda functions found in the CDK code' ) ;
17+ try {
18+ // this is global variable to store the data from the CDK code once it is executed
19+ global . lambdas = [ ] ;
20+
21+ Logger . verbose ( `[Worker ${ workerData . workerId } ] Received message` , data ) ;
22+
23+ // execute code to get the data into global.lambdas
24+ await fixCdkPaths ( workerData . awsCdkLibPath ) ;
25+ await import ( data . compileOutput ) ;
26+
27+ if ( ! global . lambdas || global . lambdas ?. length === 0 ) {
28+ throw new Error ( 'No Lambda functions found in the CDK code' ) ;
29+ }
30+
31+ const lambdas = global . lambdas . map ( ( lambda ) => ( {
32+ handler : lambda . handler ,
33+ stackName : lambda . stackName ,
34+ codePath : lambda . codePath ,
35+ code : {
36+ path : lambda . code ?. path ,
37+ } ,
38+ cdkPath : lambda . node . defaultChild . node . path ,
39+ bundling : {
40+ ...lambda . bundling ,
41+ commandHooks : undefined , // can not be serialized
42+ } ,
43+ } ) ) ;
44+
45+ Logger . verbose (
46+ `[CDK] [Worker] Sending found lambdas` ,
47+ JSON . stringify ( lambdas , null , 2 ) ,
48+ ) ;
49+ parentPort . postMessage ( lambdas ) ;
50+ } catch ( error ) {
51+ Logger . error ( `[CDK] [Worker] Error` , error ) ;
52+ throw error ;
3653 }
37-
38- const lambdas = global . lambdas . map ( ( lambda ) => ( {
39- handler : lambda . handler ,
40- stackName : lambda . stackName ,
41- codePath : lambda . codePath ,
42- code : {
43- path : lambda . code ?. path ,
44- } ,
45- cdkPath : lambda . node . defaultChild . node . path ,
46- bundling : {
47- ...lambda . bundling ,
48- commandHooks : undefined , // can not be serialized
49- } ,
50- } ) ) ;
51-
52- Logger . verbose (
53- `[CDK] [Worker] Sending found lambdas` ,
54- JSON . stringify ( lambdas , null , 2 ) ,
55- ) ;
56- parentPort . postMessage ( lambdas ) ;
5754} ) ;
5855
5956/**
@@ -105,3 +102,7 @@ async function fixCdkPaths(awsCdkLibPath) {
105102 exports : pathProxy ,
106103 } ;
107104}
105+
106+ process . on ( 'unhandledRejection' , ( error ) => {
107+ Logger . error ( `[CDK] [Worker] Unhandled Rejection` , error ) ;
108+ } ) ;
0 commit comments