Skip to content

Commit

Permalink
Fix: The configureFastify codemod now handles missing config case (#6383
Browse files Browse the repository at this point in the history
)
  • Loading branch information
dthyresson authored Sep 14, 2022
1 parent adeaf8f commit fdae7cf
Showing 1 changed file with 37 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from 'fs'
import path from 'path'

import { fetch } from 'cross-undici-fetch'
import fg from 'fast-glob'
import task from 'tasuku'

Expand All @@ -13,29 +14,47 @@ export const description =
'(v2.x.x->v2.x.x) Updates api side’s server.config.js to configure Fastify'

export const handler = () => {
task('Configure Fastify', async ({ setOutput }: task.TaskInnerApi) => {
task('Configure Fastify', async ({ setOutput }) => {
const [API_SERVER_CONFIG_PATH] = fg.sync('server.config.{js,ts}', {
cwd: getRWPaths().api.base,
absolute: true,
})

await runTransform({
transformPath: path.join(__dirname, 'configureFastify.js'),
targetPaths: [API_SERVER_CONFIG_PATH],
})
if (fs.existsSync(API_SERVER_CONFIG_PATH)) {
await runTransform({
transformPath: path.join(__dirname, 'configureFastify.js'),
targetPaths: [API_SERVER_CONFIG_PATH],
})

// The transform generates two extra semicolons for some reason:
//
// ```js
// module.exports = { config };;
// ```
//
// They don't show up in tests cause we run prettier. Let's do the same here.
fs.writeFileSync(
API_SERVER_CONFIG_PATH,
prettify(fs.readFileSync(API_SERVER_CONFIG_PATH, 'utf-8'))
)

setOutput('All done!')
} else {
const res = await fetch(
'https://raw.githubusercontent.com/redwoodjs/redwood/main/packages/create-redwood-app/template/api/server.config.js'
)
const text = await res.text()

const NEW_API_SERVER_CONFIG_PATH = path.join(
getRWPaths().api.base,
'server.config.js'
)

fs.writeFileSync(NEW_API_SERVER_CONFIG_PATH, prettify(text))

// The transform generates two extra semicolons for some reason:
//
// ```js
// module.exports = { config };;
// ```
//
// They don't show up in tets cause we run prettier. Let's do the same here.
fs.writeFileSync(
API_SERVER_CONFIG_PATH,
prettify(fs.readFileSync(API_SERVER_CONFIG_PATH, 'utf-8'))
)

setOutput('All done! Run `yarn rw lint --fix` to prettify your code')
setOutput(
'Done! No server.config.js found, so we updated your project to use the latest version.'
)
}
})
}

0 comments on commit fdae7cf

Please sign in to comment.