Skip to content

Commit

Permalink
Fix issue where generate scaffold would crash if using double quotes (#…
Browse files Browse the repository at this point in the history
…3040)

* Fix issues where generate scaffold would crash if using double quotes

* Failure to add set import should be skip, not pass
  • Loading branch information
zackdotcomputer authored Jul 15, 2021
1 parent 5a0d625 commit be5b3c3
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions packages/cli/src/commands/generate/scaffold/scaffold.js
Original file line number Diff line number Diff line change
Expand Up @@ -493,20 +493,29 @@ const addLayoutImport = ({ model: name, path: scaffoldPath = '' }) => {
const routesContent = readFile(routesPath).toString()

const newRoutesContent = routesContent.replace(
/'@redwoodjs\/router'(\s*)/,
/['"]@redwoodjs\/router['"](\s*)/,
`'@redwoodjs/router'\n${importLayout}$1`
)
writeFile(routesPath, newRoutesContent, { overwriteExisting: true })

return 'Added layout import to Routes.{js,tsx}'
}

const addSetImport = () => {
const addSetImport = (task) => {
const routesPath = getPaths().web.routes
const routesContent = readFile(routesPath).toString()
const [redwoodRouterImport, importStart, spacing, importContent, importEnd] =
routesContent.match(/(import {)(\s*)([^]*)(} from '@redwoodjs\/router')/) ||
[]
routesContent.match(
/(import {)(\s*)([^]*)(} from ['"]@redwoodjs\/router['"])/
) || []

if (!redwoodRouterImport) {
task.skip(
"Couldn't add Set import from @redwoodjs/router to Routes.{js,tsx}"
)
return undefined
}

const routerImports = importContent.replace(/\s/g, '').split(',')
if (routerImports.includes(PACKAGE_SET)) {
return 'Skipping Set import'
Expand Down Expand Up @@ -574,7 +583,7 @@ const tasks = ({ model, path, force, tests, typescript, javascript }) => {
},
{
title: 'Adding set import...',
task: async () => addSetImport({ model, path }),
task: async (_, task) => addSetImport(task),
},
{
title: 'Adding scaffold routes...',
Expand Down

0 comments on commit be5b3c3

Please sign in to comment.