Skip to content

Commit

Permalink
implement handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh-Walker-GM committed Apr 23, 2024
1 parent 73ffc4a commit 6138213
Showing 1 changed file with 30 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Listr } from 'listr2'
import { format } from 'prettier'

import { addWebPackages, getPrettierOptions } from '@redwoodjs/cli-helpers'
import { getConfig, getPaths, resolveFile } from '@redwoodjs/project-config'
import { getConfig, getPaths } from '@redwoodjs/project-config'

import { runTransform } from '../../../../lib/runTransform'

Expand All @@ -31,33 +31,46 @@ export async function handler({ force }: { force: boolean }) {
{
title: 'Add OG Image middleware ...',
task: async () => {
const graphqlPath = resolveFile(
path.join(getPaths().api.functions, 'graphql'),
)

if (!graphqlPath) {
throw new Error('Could not find a GraphQL handler in your project.')
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?',
)
}

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

if (transformResult.error) {
throw new Error(transformResult.error)
}
},
},
{
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?',
)
}

const source = fs.readFileSync(graphqlPath, 'utf-8')

const prettierOptions = await getPrettierOptions()

const prettifiedApp = await format(source, {
...prettierOptions,
parser: 'babel-ts',
})
try {
const source = fs.readFileSync(serverEntryPath, 'utf-8')
const prettierOptions = await getPrettierOptions()
const prettifiedApp = await format(source, {
...prettierOptions,
parser: 'babel-ts',
})

fs.writeFileSync(graphqlPath, prettifiedApp, 'utf-8')
fs.writeFileSync(serverEntryPath, 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 6138213

Please sign in to comment.