1
- /* eslint-disable @typescript-eslint/no-var-requires */
2
- 'use strict' ;
3
-
4
- const path = require ( 'path' ) ;
5
- const { EventEmitter } = require ( 'events' ) ;
6
- const async = require ( 'neo-async' ) ;
7
- const fs = require ( 'graceful-fs' ) ;
8
- const writeFileAtomic = require ( 'write-file-atomic' ) ;
9
- const { DEFAULT_EXTENSIONS } = require ( '@babel/core' ) ;
10
-
11
- const getParser = require ( 'jscodeshift/src/getParser' ) ;
12
- const jscodeshift = require ( 'jscodeshift/src/core' ) ;
13
-
14
- let presetEnv ;
15
- try {
16
- presetEnv = require ( '@babel/preset-env' ) ;
17
- } catch ( _ ) { }
18
-
19
- let emitter ;
20
- let finish ;
21
- let notify ;
1
+ import path from 'path' ;
2
+ import async from 'neo-async' ;
3
+ import fs from 'graceful-fs' ;
4
+ import writeFileAtomic from 'write-file-atomic' ;
5
+ import { fileURLToPath } from 'url' ;
6
+ import { register } from 'tsx/esm/api' ;
7
+
8
+ import getParser from 'jscodeshift/src/getParser.js' ;
9
+ import jscodeshift from 'jscodeshift/src/core.js' ;
10
+ import { workerData , isMainThread , parentPort } from 'worker_threads' ;
11
+
12
+ /**
13
+ * Register the TSX plugin to allow import TS(X) files.
14
+ */
15
+ register ( ) ;
16
+
22
17
let transform ;
23
18
let parserFromTransform ;
24
19
25
- if ( module . parent ) {
26
- emitter = new EventEmitter ( ) ;
27
- // @ts -expect-error
28
- emitter . send = data => run ( data ) ;
29
- finish = ( ) => emitter . emit ( 'disconnect' ) ;
30
- notify = data => emitter . emit ( 'message' , data ) ;
31
-
32
- module . exports = args => {
33
- setup ( args [ 0 ] , args [ 1 ] ) ;
34
- return emitter ;
35
- } ;
36
- } else {
37
- finish = ( ) => setImmediate ( ( ) => process . disconnect ( ) ) ;
38
- notify = data => process . send ( data ) ;
39
- process . on ( 'message' , data => run ( data ) ) ;
40
- setup ( process . argv [ 2 ] , process . argv [ 3 ] ) ;
20
+ // Get the __filename and __dirname equivalents for ESM
21
+ const __filename = fileURLToPath ( import . meta. url ) ;
22
+
23
+ if ( ! isMainThread ) {
24
+ await setup ( workerData . entrypointPath ) ;
25
+ parentPort . on ( 'message' , data => run ( data ) ) ;
41
26
}
42
27
43
28
function prepareJscodeshift ( options ) {
@@ -102,12 +87,12 @@ async function setup(entryPath) {
102
87
103
88
function updateStatus ( status , file , msg ) {
104
89
msg = msg ? file + ' ' + msg : file ;
105
- notify ( { action : 'status' , status, msg } ) ;
90
+ parentPort . postMessage ( { action : 'status' , status, msg } ) ;
106
91
}
107
92
108
93
function stats ( name , quantity ) {
109
94
quantity = typeof quantity !== 'number' ? 1 : quantity ;
110
- notify ( { action : 'update' , name : name , quantity : quantity } ) ;
95
+ parentPort . postMessage ( { action : 'update' , name : name , quantity : quantity } ) ;
111
96
}
112
97
113
98
function trimStackTrace ( trace ) {
@@ -130,9 +115,10 @@ function run(data) {
130
115
const options = data . options || { } ;
131
116
132
117
if ( ! files . length ) {
133
- finish ( ) ;
118
+ parentPort . close ( ) ;
134
119
return ;
135
120
}
121
+
136
122
async . each (
137
123
files ,
138
124
function ( file , callback ) {
@@ -153,7 +139,8 @@ function run(data) {
153
139
jscodeshift : jscodeshift ,
154
140
// eslint-disable-next-line @typescript-eslint/no-empty-function
155
141
stats : options . dry ? stats : ( ) => { } ,
156
- report : msg => notify ( { action : 'report' , file, msg } ) ,
142
+ report : msg =>
143
+ parentPort . postMessage ( { action : 'report' , file, msg } ) ,
157
144
} ,
158
145
options ,
159
146
) ;
@@ -196,7 +183,7 @@ function run(data) {
196
183
if ( err ) {
197
184
updateStatus ( 'error' , '' , 'This should never be shown!' ) ;
198
185
}
199
- notify ( { action : 'free' } ) ;
186
+ parentPort . postMessage ( { action : 'free' } ) ;
200
187
} ,
201
188
) ;
202
189
}
0 commit comments