11/* eslint-disable @typescript-eslint/no-var-requires */
22'use strict' ;
33
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' ) ;
4+ import path from 'path' ;
5+ import { fileURLToPath } from 'url' ;
6+ import fs from 'graceful-fs' ;
7+ import { EventEmitter } from 'events' ;
8+ import async from 'neo-async' ;
9+ import writeFileAtomic from 'write-file-atomic' ;
1010
11- const getParser = require ( 'jscodeshift/src/getParser' ) ;
12- const jscodeshift = require ( 'jscodeshift/src/core' ) ;
11+ import getParser from 'jscodeshift/src/getParser.js' ;
12+ import jscodeshift from 'jscodeshift/src/core.js' ;
1313
14- let presetEnv ;
15- try {
16- presetEnv = require ( '@babel/preset-env' ) ;
17- } catch ( _ ) { }
14+ const __filename = fileURLToPath ( import . meta. url ) ;
15+
16+ /**
17+ * Register the TSX plugin to allow require TS(X) files.
18+ */
19+ import { register } from 'tsx/esm/api' ;
20+ register ( ) ;
1821
1922let emitter ;
2023let finish ;
2124let notify ;
2225let transform ;
2326let parserFromTransform ;
2427
25- if ( module . parent ) {
28+ if ( import . meta . url . replace ( 'file://' , '' ) !== process . argv [ 1 ] ) {
2629 emitter = new EventEmitter ( ) ;
2730 // @ts -expect-error
2831 emitter . send = data => run ( data ) ;
2932 finish = ( ) => emitter . emit ( 'disconnect' ) ;
3033 notify = data => emitter . emit ( 'message' , data ) ;
31-
32- module . exports = args => {
33- setup ( args [ 0 ] , args [ 1 ] ) ;
34- return emitter ;
35- } ;
3634} else {
3735 finish = ( ) => setImmediate ( ( ) => process . disconnect ( ) ) ;
3836 notify = data => process . send ( data ) ;
3937 process . on ( 'message' , data => run ( data ) ) ;
40- setup ( process . argv [ 2 ] , process . argv [ 3 ] ) ;
38+ await setup ( process . argv [ 2 ] , process . argv [ 3 ] ) ;
39+ }
40+
41+ // Used by `run-in-band` to run the worker in the same process
42+ export default async function main ( args ) {
43+ await setup ( args [ 0 ] , args [ 1 ] ) ;
44+ return emitter ;
4145}
4246
4347function prepareJscodeshift ( options ) {
@@ -59,59 +63,34 @@ function retrievePath(str) {
5963}
6064
6165function getModule ( mod ) {
62- return mod . hasOwnProperty ( ' default' ) ? mod . default : mod ;
66+ return Boolean ( mod . default ) ? mod . default : mod ;
6367}
6468
65- function setup ( entryPath , babel ) {
66- if ( babel === 'babel' ) {
67- const presets = [ ] ;
68- if ( presetEnv ) {
69- presets . push ( [ presetEnv . default , { targets : { node : true } } ] ) ;
70- }
71-
72- presets . push ( require ( '@babel/preset-typescript' ) . default ) ;
73-
74- require ( '@babel/register' ) ( {
75- configFile : false ,
76- babelrc : false ,
77- presets,
78- plugins : [
79- require ( '@babel/plugin-proposal-class-properties' ) . default ,
80- require ( '@babel/plugin-proposal-nullish-coalescing-operator' ) . default ,
81- require ( '@babel/plugin-proposal-optional-chaining' ) . default ,
82- require ( '@babel/plugin-transform-modules-commonjs' ) . default ,
83- ] ,
84- extensions : [ ...DEFAULT_EXTENSIONS , '.ts' , '.tsx' ] ,
85- // By default, babel register only compiles things inside the current working directory.
86- // https://github.com/babel/babel/blob/2a4f16236656178e84b05b8915aab9261c55782c/packages/babel-register/src/node.js#L140-L157
87- ignore : [
88- // Ignore parser related files
89- / @ b a b e l \/ p a r s e r / ,
90- / \/ f l o w - p a r s e r \/ / ,
91- / \/ r e c a s t \/ / ,
92- / \/ a s t - t y p e s \/ / ,
93- ] ,
94- } ) ;
95- }
69+ async function getModuleName ( path ) {
70+ const moduleName = retrievePath ( path ) . split ( 'node_modules/' ) [ 1 ] ;
71+ const pkg = await import ( moduleName ) ;
72+ return getModule ( pkg ) ;
73+ }
9674
75+ async function setup ( entryPath ) {
9776 const transformId = retrieveTransformId ( entryPath ) ;
9877 const presetId = retrievePresetId ( entryPath ) ;
9978
10079 let transformPkg ;
10180 let transformModule ;
10281
10382 if ( transformId ) {
104- transformPkg = getModule ( require ( path . resolve ( retrievePath ( entryPath ) ) ) ) ;
83+ transformPkg = await getModuleName ( entryPath ) ;
10584 transformModule = transformPkg . transforms [ transformId ] ;
10685 }
10786
10887 if ( presetId ) {
109- transformPkg = getModule ( require ( path . resolve ( retrievePath ( entryPath ) ) ) ) ;
88+ transformPkg = await getModuleName ( entryPath ) ;
11089 transformModule = transformPkg . presets [ presetId ] ;
11190 }
11291
11392 if ( ! transformId && ! presetId ) {
114- transformModule = require ( path . resolve ( entryPath ) ) ;
93+ transformModule = await import ( path . resolve ( entryPath ) ) ;
11594 }
11695
11796 transform = getModule ( transformModule ) ;
0 commit comments