11/* eslint-disable @typescript-eslint/no-var-requires */
22'use strict' ;
33
4- const fs = require ( 'fs' ) ;
5- const mkdirp = require ( 'mkdirp' ) ;
6- const path = require ( 'path' ) ;
7- const temp = require ( 'temp' ) ;
8-
9- function renameFileTo ( oldPath , newFilename ) {
4+ import path from 'path' ;
5+ import fs from 'fs' ;
6+ // @ts -expect-error
7+ import mkdirp from 'mkdirp' ;
8+ // @ts -expect-error
9+ import temp from 'temp' ;
10+
11+ function renameFileTo ( oldPath : string , newFilename : string ) {
1012 const projectPath = path . dirname ( oldPath ) ;
1113 const newPath = path . join ( projectPath , newFilename ) ;
1214 mkdirp . sync ( path . dirname ( newPath ) ) ;
1315 fs . renameSync ( oldPath , newPath ) ;
1416 return newPath ;
1517}
1618
17- function createTempFileWith ( content , filename , extension ) {
19+ function createTempFileWith (
20+ content : string ,
21+ filename ?: string ,
22+ extension ?: string ,
23+ ) {
1824 const info = temp . openSync ( { suffix : extension } ) ;
1925 let filePath = info . path ;
2026 fs . writeSync ( info . fd , content ) ;
@@ -27,38 +33,39 @@ function createTempFileWith(content, filename, extension) {
2733
2834// Test transform files need a js extension to work with @babel /register
2935// .ts or .tsx work as well
30- function createTransformWith ( content , ext = '.js' ) {
36+ function createTransformWith ( content : string , ext = '.js' ) {
3137 return createTempFileWith (
3238 'module.exports = function(fileInfo, api, options) { ' + content + ' }' ,
3339 undefined ,
3440 ext ,
3541 ) ;
3642}
3743
38- function getFileContent ( filePath ) {
44+ function getFileContent ( filePath : string ) {
3945 return fs . readFileSync ( filePath ) . toString ( ) ;
4046}
4147
4248describe ( 'Worker API' , ( ) => {
43- it ( 'transforms files' , done => {
44- const worker = require ( './Worker' ) ;
49+ it ( 'transforms files' , async ( ) => {
50+ // @ts -expect-error
51+ const worker = await import ( './Worker.js' ) ;
4552 const transformPath = createTransformWith (
4653 'return fileInfo.source + " changed";' ,
4754 ) ;
4855 const sourcePath = createTempFileWith ( 'foo' ) ;
4956 const emitter = worker ( [ transformPath ] ) ;
5057
5158 emitter . send ( { files : [ sourcePath ] } ) ;
52- emitter . once ( 'message' , data => {
59+ emitter . once ( 'message' , ( data : any ) => {
5360 expect ( data . status ) . toBe ( 'ok' ) ;
5461 expect ( data . msg ) . toBe ( sourcePath ) ;
5562 expect ( getFileContent ( sourcePath ) ) . toBe ( 'foo changed' ) ;
56- done ( ) ;
5763 } ) ;
5864 } ) ;
5965
60- it ( 'transforms files with tranformId as extension' , done => {
61- const worker = require ( './Worker' ) ;
66+ it ( 'transforms files with tranformId as extension' , async ( ) => {
67+ // @ts -expect-error
68+ const worker = await import ( './Worker.js' ) ;
6269 const configPath = createTempFileWith (
6370 `
6471 const transfomer = (fileInfo) => fileInfo.source + " changed";
@@ -71,16 +78,16 @@ describe('Worker API', () => {
7178 const emitter = worker ( [ configPath + '@1.0.0' ] ) ;
7279
7380 emitter . send ( { files : [ sourcePath ] } ) ;
74- emitter . once ( 'message' , data => {
81+ emitter . once ( 'message' , ( data : any ) => {
7582 expect ( data . status ) . toBe ( 'ok' ) ;
7683 expect ( data . msg ) . toBe ( sourcePath ) ;
7784 expect ( getFileContent ( sourcePath ) ) . toBe ( 'foo changed' ) ;
78- done ( ) ;
7985 } ) ;
8086 } ) ;
8187
82- it ( 'transforms files with presetId as extension' , done => {
83- const worker = require ( './Worker' ) ;
88+ it ( 'transforms files with presetId as extension' , async ( ) => {
89+ // @ts -expect-error
90+ const worker = await import ( './Worker.js' ) ;
8491 const configPath = createTempFileWith (
8592 `
8693 const transfomer = (fileInfo) => fileInfo.source + " changed";
@@ -93,16 +100,16 @@ describe('Worker API', () => {
93100 const emitter = worker ( [ configPath + '#my-preset' ] ) ;
94101
95102 emitter . send ( { files : [ sourcePath ] } ) ;
96- emitter . once ( 'message' , data => {
103+ emitter . once ( 'message' , ( data : any ) => {
97104 expect ( data . status ) . toBe ( 'ok' ) ;
98105 expect ( data . msg ) . toBe ( sourcePath ) ;
99106 expect ( getFileContent ( sourcePath ) ) . toBe ( 'foo changed' ) ;
100- done ( ) ;
101107 } ) ;
102108 } ) ;
103109
104- it ( 'passes j as argument' , done => {
105- const worker = require ( './Worker' ) ;
110+ it ( 'passes j as argument' , async ( ) => {
111+ // @ts -expect-error
112+ const worker = await import ( './Worker.js' ) ;
106113 const transformPath = createTempFileWith (
107114 `module.exports = function (file, api) {
108115 return api.j(file.source).toSource() + ' changed';
@@ -112,15 +119,14 @@ describe('Worker API', () => {
112119
113120 const emitter = worker ( [ transformPath ] ) ;
114121 emitter . send ( { files : [ sourcePath ] } ) ;
115- emitter . once ( 'message' , data => {
122+ emitter . once ( 'message' , ( data : any ) => {
116123 expect ( data . status ) . toBe ( 'ok' ) ;
117124 expect ( getFileContent ( sourcePath ) ) . toBe ( 'const x = 10;' + ' changed' ) ;
118- done ( ) ;
119125 } ) ;
120126 } ) ;
121127
122128 describe ( 'custom parser' , ( ) => {
123- function getTransformForParser ( parser ) {
129+ function getTransformForParser ( parser : string ) {
124130 return createTempFileWith (
125131 `function transform(fileInfo, api) {
126132 api.jscodeshift(fileInfo.source);
@@ -136,67 +142,67 @@ describe('Worker API', () => {
136142 return createTempFileWith ( 'const x = (a: Object, b: string): void => {}' ) ;
137143 }
138144
139- it ( 'errors if new flow type code is parsed with babel v5' , done => {
140- const worker = require ( './Worker' ) ;
145+ it ( 'errors if new flow type code is parsed with babel v5' , async ( ) => {
146+ // @ts -expect-error
147+ const worker = await import ( './Worker.js' ) ;
141148 const transformPath = createTransformWith (
142149 'api.jscodeshift(fileInfo.source); return "changed";' ,
143150 ) ;
144151 const sourcePath = getSourceFile ( ) ;
145152 const emitter = worker ( [ transformPath ] ) ;
146153
147154 emitter . send ( { files : [ sourcePath ] } ) ;
148- emitter . once ( 'message' , data => {
155+ emitter . once ( 'message' , ( data : any ) => {
149156 expect ( data . status ) . toBe ( 'error' ) ;
150157 expect ( data . msg ) . toMatch ( 'SyntaxError' ) ;
151- done ( ) ;
152158 } ) ;
153159 } ) ;
154160
155161 [ 'flow' , 'babylon' ] . forEach ( parser => {
156- it ( `uses ${ parser } if configured as such` , done => {
157- const worker = require ( './Worker' ) ;
162+ it ( `uses ${ parser } if configured as such` , async ( ) => {
163+ // @ts -expect-error
164+ const worker = await import ( './Worker.js' ) ;
158165 const transformPath = getTransformForParser ( parser ) ;
159166 const sourcePath = getSourceFile ( ) ;
160167 const emitter = worker ( [ transformPath ] ) ;
161168
162169 emitter . send ( { files : [ sourcePath ] } ) ;
163- emitter . once ( 'message' , data => {
170+ emitter . once ( 'message' , ( data : any ) => {
164171 expect ( data . status ) . toBe ( 'ok' ) ;
165172 expect ( getFileContent ( sourcePath ) ) . toBe ( 'changed' ) ;
166- done ( ) ;
167173 } ) ;
168174 } ) ;
169175 } ) ;
170176
171177 [ 'babylon' , 'flow' , 'tsx' ] . forEach ( parser => {
172- it ( `can parse JSX with ${ parser } ` , done => {
173- const worker = require ( './Worker' ) ;
178+ it ( `can parse JSX with ${ parser } ` , async ( ) => {
179+ // @ts -expect-error
180+ const worker = await import ( './Worker.js' ) ;
174181 const transformPath = getTransformForParser ( parser ) ;
175182 const sourcePath = createTempFileWith (
176183 'var component = <div>{foobar}</div>;' ,
177184 ) ;
178185 const emitter = worker ( [ transformPath ] ) ;
179186
180187 emitter . send ( { files : [ sourcePath ] } ) ;
181- emitter . once ( 'message' , data => {
188+ emitter . once ( 'message' , ( data : any ) => {
182189 expect ( data . status ) . toBe ( 'ok' ) ;
183190 expect ( getFileContent ( sourcePath ) ) . toBe ( 'changed' ) ;
184- done ( ) ;
185191 } ) ;
186192 } ) ;
187193 } ) ;
188194
189- it ( 'can parse enums with flow' , done => {
190- const worker = require ( './Worker' ) ;
195+ it ( 'can parse enums with flow' , async ( ) => {
196+ // @ts -expect-error
197+ const worker = await import ( './Worker.js' ) ;
191198 const transformPath = getTransformForParser ( 'flow' ) ;
192199 const sourcePath = createTempFileWith ( 'enum E {A, B}' ) ;
193200 const emitter = worker ( [ transformPath ] ) ;
194201
195202 emitter . send ( { files : [ sourcePath ] } ) ;
196- emitter . once ( 'message' , data => {
203+ emitter . once ( 'message' , ( data : any ) => {
197204 expect ( data . status ) . toBe ( 'ok' ) ;
198205 expect ( getFileContent ( sourcePath ) ) . toBe ( 'changed' ) ;
199- done ( ) ;
200206 } ) ;
201207 } ) ;
202208 } ) ;
0 commit comments