Skip to content

Commit e935add

Browse files
committed
feat: add param -a to vue-codemod command
2 parents c02668a + 6925c32 commit e935add

File tree

2 files changed

+64
-8
lines changed

2 files changed

+64
-8
lines changed

bin/vue-codemod.ts

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,79 @@ import builtInTransformations from '../transformations'
1313
import vueTransformations from '../vue-transformations'
1414
import runTransformation from '../src/runTransformation'
1515

16+
import type { TransformationModule } from '../src/runTransformation'
17+
1618
const debug = createDebug('vue-codemod')
1719
const log = console.log.bind(console)
1820

19-
const { _: files, transformation: transformationName, params } = yargs
21+
const {
22+
_: files,
23+
transformation: transformationName,
24+
runAllTransformation: runAllTransformation,
25+
params,
26+
} = yargs
2027
.usage('Usage: $0 [file pattern]')
2128
.option('transformation', {
2229
alias: 't',
2330
type: 'string',
31+
conflicts: 'runAllTransformation',
2432
describe: 'Name or path of the transformation module',
2533
})
2634
.option('params', {
2735
alias: 'p',
2836
describe: 'Custom params to the transformation',
2937
})
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+
])
3156
.help().argv
3257

3358
// TODO: port the `Runner` interface of jscodeshift
3459
async function main() {
3560
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+
}
3769

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+
)
3989

4090
const extensions = ['.js', '.ts', '.vue', '.jsx', '.tsx']
4191
for (const p of resolvedPaths) {
@@ -45,7 +95,7 @@ async function main() {
4595
source: fs.readFileSync(p).toString(),
4696
}
4797
const extension = (/\.([^.]*)$/.exec(fileInfo.path) || [])[0]
48-
if (!extensions.includes(extension)){
98+
if (!extensions.includes(extension)) {
4999
continue
50100
}
51101
try {
@@ -65,7 +115,11 @@ main().catch((err) => {
65115
console.error(err)
66116
process.exit(1)
67117
})
68-
118+
/**
119+
* load Transformation Module
120+
* @param nameOrPath
121+
* @returns
122+
*/
69123
function loadTransformationModule(nameOrPath: string) {
70124
let jsTransformation = builtInTransformations[nameOrPath]
71125
let vueTransformation = vueTransformations[nameOrPath]

src/runTransformation.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ type VueTransformationModule =
3333
default: VueTransformation,
3434
}
3535

36-
type TransformationModule = JSTransformationModule | VueTransformationModule
36+
export type TransformationModule =
37+
| JSTransformationModule
38+
| VueTransformationModule
3739

3840
export default function runTransformation(
3941
fileInfo: FileInfo,
@@ -74,7 +76,7 @@ export default function runTransformation(
7476
fileInfo.source =
7577
source.slice(0, contentStart) +
7678
descriptor.template.content +
77-
source.slice(contentEnd)
79+
source.slice(contentEnd, contentEnd + 11)
7880

7981
const out = transformation(fileInfo, params)
8082

0 commit comments

Comments
 (0)