File tree 2 files changed +45
-3
lines changed
2 files changed +45
-3
lines changed Original file line number Diff line number Diff line change @@ -23,6 +23,7 @@ new Vue({
23
23
}).$mount('#app')
24
24
` . trim ( ) )
25
25
fs . writeFileSync ( path . resolve ( templateDir , 'empty-entry.js' ) , `;` )
26
+ fs . writeFileSync ( path . resolve ( templateDir , 'main.ts' ) , `const a: string = 'hello';` )
26
27
fs . writeFileSync ( path . resolve ( templateDir , 'hello.vue' ) , `
27
28
<template>
28
29
<p>Hello, {{ msg }}</p>
@@ -530,6 +531,23 @@ test('api: injectImports to empty file', async () => {
530
531
expect ( fs . readFileSync ( '/main.js' , 'utf-8' ) ) . toMatch ( / i m p o r t f o o f r o m ' f o o ' \r ? \n i m p o r t b a r f r o m ' b a r ' / )
531
532
} )
532
533
534
+ test ( 'api: injectImports to typescript file' , async ( ) => {
535
+ const generator = new Generator ( '/' , { plugins : [
536
+ {
537
+ id : 'test' ,
538
+ apply : api => {
539
+ api . injectImports ( 'main.ts' , `import foo from 'foo'` )
540
+ api . render ( {
541
+ 'main.ts' : path . join ( templateDir , 'main.ts' )
542
+ } )
543
+ }
544
+ }
545
+ ] } )
546
+
547
+ await generator . generate ( )
548
+ expect ( fs . readFileSync ( '/main.ts' , 'utf-8' ) ) . toMatch ( / i m p o r t f o o f r o m ' f o o ' / )
549
+ } )
550
+
533
551
test ( 'api: addEntryDuplicateImport' , async ( ) => {
534
552
const generator = new Generator ( '/' , { plugins : [
535
553
{
Original file line number Diff line number Diff line change 1
- const jscodeshift = require ( 'jscodeshift' )
2
1
const adapt = require ( 'vue-jscodeshift-adapter' )
2
+ let jscodeshift = require ( 'jscodeshift' )
3
3
4
- module . exports = function runCodemod ( transform , fileInfo , options ) {
5
- return adapt ( transform ) ( fileInfo , { jscodeshift } , options || { } )
4
+ module . exports = function runCodemod ( transformModule , fileInfo , options = { } ) {
5
+ const transform = typeof transformModule . default === 'function'
6
+ ? transformModule . default
7
+ : transformModule
8
+
9
+ let parser = transformModule . parser || options . parser
10
+ if ( ! parser ) {
11
+ if ( fileInfo . path . endsWith ( ( '.ts' ) ) ) {
12
+ parser = 'ts'
13
+ } else if ( fileInfo . path . endsWith ( '.tsx' ) ) {
14
+ parser = 'tsx'
15
+ }
16
+ }
17
+
18
+ if ( parser ) {
19
+ jscodeshift = jscodeshift . withParser ( parser )
20
+ }
21
+
22
+ const api = {
23
+ jscodeshift,
24
+ j : jscodeshift ,
25
+ stats : ( ) => { } ,
26
+ report : ( ) => { }
27
+ }
28
+
29
+ return adapt ( transform ) ( fileInfo , api , options )
6
30
}
You can’t perform that action at this time.
0 commit comments