-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from wattanx/feat-capi-legacy-convert-script
feat: capi legacy convert script
- Loading branch information
Showing
7 changed files
with
134 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
packages/nuxt-bridge-migration-tools/src/commands/capi-legacy.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import type { Arguments, Argv } from "yargs"; | ||
import capiLegacy from "../transformations/capi-legacy"; | ||
import { convertTargetFiles } from "../lib/convert-target-files"; | ||
import { executeTransform } from "../handlers/execute-transform"; | ||
|
||
type Options = { | ||
targetFilePaths: string[]; | ||
}; | ||
|
||
export const command = "capi-legacy [targetFilePaths...]"; | ||
export const desc = | ||
"convert from `@nuxtjs/composition-api` to the capi legacy of `@nuxt/bridge`."; | ||
|
||
export const builder = (yargs: Argv<Options>): Argv<Options> => | ||
yargs.positional("targetFilePaths", { | ||
array: true, | ||
type: "string", | ||
demandOption: true, | ||
description: | ||
"Path to the target vue file, which can be set with the glob pattern. eg: 'src/**/*.vue'", | ||
} as const); | ||
|
||
export const handler = async (argv: Arguments<Options>): Promise<void> => { | ||
const { targetFilePaths } = argv; | ||
|
||
const targetFiles = await convertTargetFiles(targetFilePaths); | ||
|
||
executeTransform(targetFiles, capiLegacy); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
packages/nuxt-bridge-migration-tools/src/transformations/capi-legacy.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { applyTransform } from "jscodeshift/src/testUtils"; | ||
import transform from "./capi-legacy"; | ||
|
||
test("capi migration", () => { | ||
const source = `import { defineComponent, useRoute, usefetch } from '@nuxtjs/composition-api'; | ||
export default defineComponent({ | ||
setup() { | ||
const { $fetch, $fetchState } = useFetch(async () => { | ||
$fetch('https://pokeapi.co/api/v2/pokemon/ditto'); | ||
}) | ||
const route = useRoute(); | ||
const path = route.value.path; | ||
} | ||
}); | ||
`; | ||
|
||
const expected = `import { defineComponent, useRoute, usefetch } from '@nuxtjs/composition-api'; | ||
export default defineComponent({ | ||
setup() { | ||
const { | ||
fetch, | ||
fetchState, | ||
} = useFetch(async () => { | ||
$fetch('https://pokeapi.co/api/v2/pokemon/ditto'); | ||
}) | ||
const route = useRoute(); | ||
const path = route.path; | ||
} | ||
});`; | ||
|
||
const result = applyTransform(transform, null, { | ||
source, | ||
}); | ||
expect(result).toBe(expected); | ||
}); |
34 changes: 34 additions & 0 deletions
34
packages/nuxt-bridge-migration-tools/src/transformations/capi-legacy.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import type { Identifier, Property } from "jscodeshift"; | ||
import { convertUseRoute } from "./use-route"; | ||
import { wrap, ASTTransformation } from "../wrapAstTransformation"; | ||
|
||
export const convertCapiLegacy: ASTTransformation<any> = (context, options) => { | ||
const { root, j } = context; | ||
|
||
const useFetchDeclarator = root.find(j.VariableDeclarator, { | ||
init: { | ||
callee: { | ||
name: "useFetch", | ||
}, | ||
}, | ||
}); | ||
|
||
useFetchDeclarator.find(j.ObjectPattern).forEach((x) => { | ||
const properties = x.node.properties as Property[]; | ||
properties.forEach((p) => { | ||
const key = p.key as Identifier; | ||
if (key.name === "$fetch") { | ||
key.name = "fetch"; | ||
} | ||
if (key.name === "$fetchState") { | ||
key.name = "fetchState"; | ||
} | ||
}); | ||
j(x).replaceWith(j.objectPattern(properties)); | ||
}); | ||
|
||
convertUseRoute(context, options); | ||
}; | ||
|
||
export default wrap(convertCapiLegacy); | ||
export const parser = "ts"; |
cab213d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
wattanx-converter – ./
wattanx-converter-git-main-wattanx.vercel.app
wattanx-converter-wattanx.vercel.app
wattanx-converter.vercel.app