1
1
/* eslint-disable @typescript-eslint/no-var-requires */
2
2
'use strict' ;
3
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' ) ;
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' ;
10
10
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' ;
13
13
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 ( ) ;
18
21
19
22
let emitter ;
20
23
let finish ;
21
24
let notify ;
22
25
let transform ;
23
26
let parserFromTransform ;
24
27
25
- if ( module . parent ) {
28
+ if ( import . meta . url . replace ( 'file://' , '' ) !== process . argv [ 1 ] ) {
26
29
emitter = new EventEmitter ( ) ;
27
30
// @ts -expect-error
28
31
emitter . send = data => run ( data ) ;
29
32
finish = ( ) => emitter . emit ( 'disconnect' ) ;
30
33
notify = data => emitter . emit ( 'message' , data ) ;
31
-
32
- module . exports = args => {
33
- setup ( args [ 0 ] , args [ 1 ] ) ;
34
- return emitter ;
35
- } ;
36
34
} else {
37
35
finish = ( ) => setImmediate ( ( ) => process . disconnect ( ) ) ;
38
36
notify = data => process . send ( data ) ;
39
37
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 ;
41
45
}
42
46
43
47
function prepareJscodeshift ( options ) {
@@ -59,59 +63,34 @@ function retrievePath(str) {
59
63
}
60
64
61
65
function getModule ( mod ) {
62
- return mod . hasOwnProperty ( ' default' ) ? mod . default : mod ;
66
+ return Boolean ( mod . default ) ? mod . default : mod ;
63
67
}
64
68
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
+ }
96
74
75
+ async function setup ( entryPath ) {
97
76
const transformId = retrieveTransformId ( entryPath ) ;
98
77
const presetId = retrievePresetId ( entryPath ) ;
99
78
100
79
let transformPkg ;
101
80
let transformModule ;
102
81
103
82
if ( transformId ) {
104
- transformPkg = getModule ( require ( path . resolve ( retrievePath ( entryPath ) ) ) ) ;
83
+ transformPkg = await getModuleName ( entryPath ) ;
105
84
transformModule = transformPkg . transforms [ transformId ] ;
106
85
}
107
86
108
87
if ( presetId ) {
109
- transformPkg = getModule ( require ( path . resolve ( retrievePath ( entryPath ) ) ) ) ;
88
+ transformPkg = await getModuleName ( entryPath ) ;
110
89
transformModule = transformPkg . presets [ presetId ] ;
111
90
}
112
91
113
92
if ( ! transformId && ! presetId ) {
114
- transformModule = require ( path . resolve ( entryPath ) ) ;
93
+ transformModule = await import ( path . resolve ( entryPath ) ) ;
115
94
}
116
95
117
96
transform = getModule ( transformModule ) ;
0 commit comments