@@ -13,29 +13,79 @@ import builtInTransformations from '../transformations'
13
13
import vueTransformations from '../vue-transformations'
14
14
import runTransformation from '../src/runTransformation'
15
15
16
+ import type { TransformationModule } from '../src/runTransformation'
17
+
16
18
const debug = createDebug ( 'vue-codemod' )
17
19
const log = console . log . bind ( console )
18
20
19
- const { _ : files , transformation : transformationName , params } = yargs
21
+ const {
22
+ _ : files ,
23
+ transformation : transformationName ,
24
+ runAllTransformation : runAllTransformation ,
25
+ params,
26
+ } = yargs
20
27
. usage ( 'Usage: $0 [file pattern]' )
21
28
. option ( 'transformation' , {
22
29
alias : 't' ,
23
30
type : 'string' ,
31
+ conflicts : 'runAllTransformation' ,
24
32
describe : 'Name or path of the transformation module' ,
25
33
} )
26
34
. option ( 'params' , {
27
35
alias : 'p' ,
28
36
describe : 'Custom params to the transformation' ,
29
37
} )
30
- . demandOption ( 'transformation' )
38
+ . option ( 'runAllTransformation' , {
39
+ alias : 'a' ,
40
+ type : 'boolean' ,
41
+ default : true ,
42
+ conflicts : 'transformation' ,
43
+ describe : 'run all transformation module' ,
44
+ } )
45
+ // example(command: string, description: string): Argv<T>;
46
+ . example ( [
47
+ [
48
+ 'npx vue-codemod ./src -a' ,
49
+ 'Run all rules to convert all relevant files in the ./src folder' ,
50
+ ] ,
51
+ [
52
+ 'npx vue-codemod ./src/components/HelloWorld.vue -t slot-attribute' ,
53
+ 'Run slot-attribute rule to convert HelloWorld.vue' ,
54
+ ] ,
55
+ ] )
31
56
. help ( ) . argv
32
57
33
58
// TODO: port the `Runner` interface of jscodeshift
34
59
async function main ( ) {
35
60
const resolvedPaths = globby . sync ( files as string [ ] )
36
- const transformationModule = loadTransformationModule ( transformationName )
61
+ if ( transformationName != undefined ) {
62
+ const transformationModule = loadTransformationModule ( transformationName )
63
+ processTransformation (
64
+ resolvedPaths ,
65
+ transformationName ,
66
+ transformationModule
67
+ )
68
+ }
37
69
38
- log ( `Processing ${ resolvedPaths . length } files…` )
70
+ if ( runAllTransformation ) {
71
+ for ( let key in builtInTransformations ) {
72
+ processTransformation ( resolvedPaths , key , builtInTransformations [ key ] )
73
+ }
74
+
75
+ for ( let key in vueTransformations ) {
76
+ processTransformation ( resolvedPaths , key , vueTransformations [ key ] )
77
+ }
78
+ }
79
+ }
80
+
81
+ function processTransformation (
82
+ resolvedPaths : string [ ] ,
83
+ transformationName : string ,
84
+ transformationModule : TransformationModule
85
+ ) {
86
+ log (
87
+ `Processing ${ resolvedPaths . length } files… use ${ transformationName } transformation`
88
+ )
39
89
40
90
const extensions = [ '.js' , '.ts' , '.vue' , '.jsx' , '.tsx' ]
41
91
for ( const p of resolvedPaths ) {
@@ -45,7 +95,7 @@ async function main() {
45
95
source : fs . readFileSync ( p ) . toString ( ) ,
46
96
}
47
97
const extension = ( / \. ( [ ^ . ] * ) $ / . exec ( fileInfo . path ) || [ ] ) [ 0 ]
48
- if ( ! extensions . includes ( extension ) ) {
98
+ if ( ! extensions . includes ( extension ) ) {
49
99
continue
50
100
}
51
101
try {
@@ -65,7 +115,11 @@ main().catch((err) => {
65
115
console . error ( err )
66
116
process . exit ( 1 )
67
117
} )
68
-
118
+ /**
119
+ * load Transformation Module
120
+ * @param nameOrPath
121
+ * @returns
122
+ */
69
123
function loadTransformationModule ( nameOrPath : string ) {
70
124
let jsTransformation = builtInTransformations [ nameOrPath ]
71
125
let vueTransformation = vueTransformations [ nameOrPath ]
0 commit comments