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+
2217let transform ;
2318let parserFromTransform ;
2419
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 ) ) ;
4126}
4227
4328function prepareJscodeshift ( options ) {
@@ -102,12 +87,12 @@ async function setup(entryPath) {
10287
10388function updateStatus ( status , file , msg ) {
10489 msg = msg ? file + ' ' + msg : file ;
105- notify ( { action : 'status' , status, msg } ) ;
90+ parentPort . postMessage ( { action : 'status' , status, msg } ) ;
10691}
10792
10893function stats ( name , quantity ) {
10994 quantity = typeof quantity !== 'number' ? 1 : quantity ;
110- notify ( { action : 'update' , name : name , quantity : quantity } ) ;
95+ parentPort . postMessage ( { action : 'update' , name : name , quantity : quantity } ) ;
11196}
11297
11398function trimStackTrace ( trace ) {
@@ -130,9 +115,10 @@ function run(data) {
130115 const options = data . options || { } ;
131116
132117 if ( ! files . length ) {
133- finish ( ) ;
118+ parentPort . close ( ) ;
134119 return ;
135120 }
121+
136122 async . each (
137123 files ,
138124 function ( file , callback ) {
@@ -153,7 +139,8 @@ function run(data) {
153139 jscodeshift : jscodeshift ,
154140 // eslint-disable-next-line @typescript-eslint/no-empty-function
155141 stats : options . dry ? stats : ( ) => { } ,
156- report : msg => notify ( { action : 'report' , file, msg } ) ,
142+ report : msg =>
143+ parentPort . postMessage ( { action : 'report' , file, msg } ) ,
157144 } ,
158145 options ,
159146 ) ;
@@ -196,7 +183,7 @@ function run(data) {
196183 if ( err ) {
197184 updateStatus ( 'error' , '' , 'This should never be shown!' ) ;
198185 }
199- notify ( { action : 'free' } ) ;
186+ parentPort . postMessage ( { action : 'free' } ) ;
200187 } ,
201188 ) ;
202189}
0 commit comments