Skip to content

Commit

Permalink
adds --output-file flag
Browse files Browse the repository at this point in the history
  • Loading branch information
pepoeverton committed Apr 30, 2022
1 parent 2a42de2 commit acbf1a2
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 5 deletions.
5 changes: 5 additions & 0 deletions packages/next/cli/next-lint.ts
Expand Up @@ -71,10 +71,12 @@ const nextLint: cliCommand = async (argv) => {
'--cache-strategy': String,
'--error-on-unmatched-pattern': Boolean,
'--format': String,
'--output-file': String,

// Aliases
'-c': '--config',
'-f': '--format',
'-o': '--output-file',
}

let args: arg.Result<arg.Spec>
Expand Down Expand Up @@ -127,6 +129,7 @@ const nextLint: cliCommand = async (argv) => {
--max-warnings Int Number of warnings to trigger nonzero exit code - default: -1
Output:
-o, --output-file path::String Specify file to write report to
-f, --format String Use a specific output format - default: Next.js custom formatter
Inline configuration comments:
Expand Down Expand Up @@ -171,6 +174,7 @@ const nextLint: cliCommand = async (argv) => {
const maxWarnings = args['--max-warnings'] ?? -1
const formatter = args['--format'] || null
const strict = Boolean(args['--strict'])
const outputFile = args['--output-file'] || null

const distDir = join(baseDir, nextConfig.distDir)
const defaultCacheLocation = join(distDir, 'cache', 'eslint/')
Expand All @@ -183,6 +187,7 @@ const nextLint: cliCommand = async (argv) => {
reportErrorsOnly,
maxWarnings,
formatter,
outputFile,
strict
)
.then(async (lintResults) => {
Expand Down
4 changes: 3 additions & 1 deletion packages/next/lib/eslint/customFormatter.ts
Expand Up @@ -100,6 +100,7 @@ export function formatResults(
format: (r: LintResult[]) => string
): {
output: string
outputWithMessages: string
totalNextPluginErrorCount: number
totalNextPluginWarningCount: number
} {
Expand All @@ -124,7 +125,8 @@ export function formatResults(
.join('\n')

return {
output:
output: output,
outputWithMessages:
resultsWithMessages.length > 0
? output +
`\n\n${chalk.cyan(
Expand Down
14 changes: 10 additions & 4 deletions packages/next/lib/eslint/runLintCheck.ts
@@ -1,4 +1,4 @@
import { promises as fs } from 'fs'
import { promises as fs, promises, statSync } from 'fs'
import chalk from 'next/dist/compiled/chalk'
import path from 'path'

Expand All @@ -9,6 +9,7 @@ import * as CommentJson from 'next/dist/compiled/comment-json'
import { LintResult, formatResults } from './customFormatter'
import { writeDefaultConfig } from './writeDefaultConfig'
import { hasEslintConfiguration } from './hasEslintConfiguration'
import { writeOutputFile } from './writeOutputFile'

import { ESLINT_PROMPT_VALUES } from '../constants'
import { existsSync, findPagesDir } from '../find-pages-dir'
Expand Down Expand Up @@ -86,7 +87,8 @@ async function lint(
eslintOptions: any = null,
reportErrorsOnly: boolean = false,
maxWarnings: number = -1,
formatter: string | null = null
formatter: string | null = null,
outputFile: string | null = null
): Promise<
| string
| null
Expand Down Expand Up @@ -220,8 +222,10 @@ async function lint(
0
)

if (outputFile) await writeOutputFile(outputFile, formattedResult.output)

return {
output: formattedResult.output,
output: formattedResult.outputWithMessages,
isError:
ESLint.getErrorResults(results)?.length > 0 ||
(maxWarnings >= 0 && totalWarnings > maxWarnings),
Expand Down Expand Up @@ -265,6 +269,7 @@ export async function runLintCheck(
reportErrorsOnly: boolean = false,
maxWarnings: number = -1,
formatter: string | null = null,
outputFile: string | null = null,
strict: boolean = false
): ReturnType<typeof lint> {
try {
Expand Down Expand Up @@ -308,7 +313,8 @@ export async function runLintCheck(
eslintOptions,
reportErrorsOnly,
maxWarnings,
formatter
formatter,
outputFile
)
} else {
// Display warning if no ESLint configuration is present during "next build"
Expand Down
45 changes: 45 additions & 0 deletions packages/next/lib/eslint/writeOutputFile.ts
@@ -0,0 +1,45 @@
import { promises as fs } from 'fs'
import path from 'path'
import * as Log from '../../build/output/log'
import isError from '../../lib/is-error'

/**
* Check if a given file path is a directory or not.
* @param {string} filePath The path to a file to check.
* @returns {Promise<boolean>} `true` if the path is a directory.
*/
async function isDirectory(filePath: string): Promise<boolean> {
try {
return (await fs.stat(filePath)).isDirectory()
} catch (error) {
if (
isError(error) &&
(error.code === 'ENOENT' || error.code === 'ENOTDIR')
) {
return false
}
throw error
}
}
/**
* Create a file with eslint output data
* @param {string} outputFile The name file that needs to be created
* @param {string} outputData The data that needs to be inserted into the file
*/
export async function writeOutputFile(outputFile: string, outputData: string) {
const filePath = path.resolve(process.cwd(), outputFile)

if (await isDirectory(filePath)) {
Log.error(
`Cannot write to output file path, it is a directory: ${filePath}`
)
} else {
try {
await fs.mkdir(path.dirname(filePath), { recursive: true })
await fs.writeFile(filePath, outputData)
Log.info(`The output file has been created: ${filePath}`)
} catch {
Log.error(`There was a problem writing the output file: ${filePath}`)
}
}
}

0 comments on commit acbf1a2

Please sign in to comment.