Skip to content

Commit

Permalink
add vite codemod to command
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh-Walker-GM committed May 6, 2024
1 parent da7d237 commit 41fab0f
Showing 1 changed file with 39 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export async function handler({ force }: { force: boolean }) {
}

const transformResult = await runTransform({
transformPath: path.join(__dirname, 'codemod.js'),
transformPath: path.join(__dirname, 'codemodMiddleware.js'),
targetPaths: [serverEntryPath],
})

Expand All @@ -49,27 +49,47 @@ export async function handler({ force }: { force: boolean }) {
},
},
{
title: 'Prettifying changes files',
task: async (_ctx, task) => {
const serverEntryPath = rwPaths.web.entryServer
if (serverEntryPath === null) {
throw new Error(
'Could not find the server entry file. Is your project using the default structure?',
)
title: 'Add OG Image vite plugin ...',
task: async () => {
const viteConfigPath = rwPaths.web.viteConfig
if (viteConfigPath === null) {
throw new Error('Could not find the Vite config file')
}

try {
const source = fs.readFileSync(serverEntryPath, 'utf-8')
const prettierOptions = await getPrettierOptions()
const prettifiedApp = await format(source, {
...prettierOptions,
parser: 'babel-ts',
})
const transformResult = await runTransform({
transformPath: path.join(__dirname, 'codemodVitePlugin.js'),
targetPaths: [viteConfigPath],
})

if (transformResult.error) {
throw new Error(transformResult.error)
}
},
},
{
title: 'Prettifying changes files',
task: async (_ctx, task) => {
const prettifyPaths = [
rwPaths.web.entryServer,
rwPaths.web.viteConfig,
]
for (const prettifyPath of prettifyPaths) {
if (prettifyPath === null) {
throw new Error('Could not find the file to be prettified')
}
try {
const source = fs.readFileSync(prettifyPath, 'utf-8')
const prettierOptions = await getPrettierOptions()
const prettifiedApp = await format(source, {
...prettierOptions,
parser: 'babel-ts',
})

fs.writeFileSync(serverEntryPath, prettifiedApp, 'utf-8')
} catch (error) {
task.output =
"Couldn't prettify the changes. Please reformat the files manually if needed."
fs.writeFileSync(prettifyPath, prettifiedApp, 'utf-8')
} catch (error) {
task.output =
"Couldn't prettify the changes. Please reformat the files manually if needed."
}
}
},
},
Expand Down

0 comments on commit 41fab0f

Please sign in to comment.