diff --git a/packages/cli/src/commands/generate.js b/packages/cli/src/commands/generate.js index 2fd524f4d6f7..930242bbaf1f 100644 --- a/packages/cli/src/commands/generate.js +++ b/packages/cli/src/commands/generate.js @@ -45,4 +45,10 @@ export const yargsDefaults = { description: 'Generate TypeScript files', type: 'boolean', }, + lint: { + alias: 'l', + default: true, + description: 'Lint generated files', + type: 'boolean', + }, } diff --git a/packages/cli/src/commands/generate/directive/directive.js b/packages/cli/src/commands/generate/directive/directive.js index a9bba3ac4014..c4f57320d6ec 100644 --- a/packages/cli/src/commands/generate/directive/directive.js +++ b/packages/cli/src/commands/generate/directive/directive.js @@ -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 { @@ -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: () => { diff --git a/packages/cli/src/lib/index.js b/packages/cli/src/lib/index.js index d33cb44c71f0..954a75521a6c 100644 --- a/packages/cli/src/lib/index.js +++ b/packages/cli/src/lib/index.js @@ -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)}`], { + stdio: 'pipe', + shell: true, + }) + + task.title = `Successfully linted file \`./${path.relative(base, target)}\`` +} + export const saveRemoteFileToDisk = ( url, localPath, @@ -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 @@ -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*]*wrap={${layout}}[^<]*>([^<]*)<\/Set>` ) @@ -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, {