Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lint generated files #6218

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/cli/src/commands/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,10 @@ export const yargsDefaults = {
description: 'Generate TypeScript files',
type: 'boolean',
},
lint: {
alias: 'l',
default: true,
description: 'Lint generated files',
type: 'boolean',
},
}
17 changes: 16 additions & 1 deletion packages/cli/src/commands/generate/directive/directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import prompts from 'prompts'

import { getConfig } from '@redwoodjs/internal/dist/config'

import { getPaths, writeFilesTask, transformTSToJS } from '../../../lib'
import {
getPaths,
writeFilesTask,
transformTSToJS,
lintFilesTask,
} from '../../../lib'
import c from '../../../lib/colors'
import { yargsDefaults } from '../../generate'
import {
Expand Down Expand Up @@ -149,6 +154,16 @@ export const handler = async (args) => {
})
},
},
{
title: 'Linting ...',
task: (_ctx, task) => {
if (args.lint) {
return lintFilesTask(files({ ...args, type: directiveType })) // We pass directiveType to prevent files() error
} else {
task.skip('Skipping lint.')
}
},
},
{
title: 'Generating TypeScript definitions and GraphQL schemas ...',
task: () => {
Expand Down
37 changes: 35 additions & 2 deletions packages/cli/src/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,20 @@ export const writeFile = (
task.title = `Successfully wrote file \`./${path.relative(base, target)}\``
}

export const lintFile = (target, contents, task = {}) => {
const { base } = getPaths()
task.title = `Linting \`./${path.relative(base, target)}\``

// const filename = path.basename(target)
// const targetDir = target.replace(filename, '')
execa('yarn rw lint --fix --path ', [`./${path.relative(base, target)}`], {

Check warning

Code scanning / CodeQL

Unsafe shell command constructed from library input

[Shell argument](1) based on [library input](2) is later used in [shell command](3).
stdio: 'pipe',
shell: true,
})

task.title = `Successfully linted file \`./${path.relative(base, target)}\``
}

export const saveRemoteFileToDisk = (
url,
localPath,
Expand Down Expand Up @@ -288,6 +302,25 @@ export const deleteFilesTask = (files) => {
])
}

/**
* Creates a list of files to lint after creation.
*
* @param files - {[filepath]: contents}
*/
export const lintFilesTask = (files, options) => {
const { base } = getPaths()
return new Listr(
Object.keys(files).map((file) => {
const contents = files[file]
return {
title: `...waiting to lint file \`./${path.relative(base, file)}\`...`,
// TODO: Is it necessary to have (ctx, task)?
task: (ctx, task) => lintFile(file, contents, options, task),
}
})
)
}

/**
* @param files - {[filepath]: contents}
* Deletes any empty directrories that are more than three levels deep below the base directory
Expand Down Expand Up @@ -395,7 +428,7 @@ export const addScaffoldImport = () => {
return 'Added scaffold import to App.{js,tsx}'
}

const removeEmtpySet = (routesContent, layout) => {
const removeEmptySet = (routesContent, layout) => {
const setWithLayoutReg = new RegExp(
`\\s*<Set[^>]*wrap={${layout}}[^<]*>([^<]*)<\/Set>`
)
Expand Down Expand Up @@ -425,7 +458,7 @@ export const removeRoutesFromRouterTask = (routes, layout) => {
}, routesContent)

const routesWithoutEmptySet = layout
? removeEmtpySet(newRoutesContent, layout)
? removeEmptySet(newRoutesContent, layout)
: newRoutesContent

writeFile(redwoodPaths.web.routes, routesWithoutEmptySet, {
Expand Down