Files
cypress/npm/eslint-plugin-dev/lib/scripts/utils.js
Bill Glesias f14a11aecf chore: update eslint from version 7 to version 8 (#29355)
* chore: (for eslint-plugin-dev only is breaking) update eslint-plugin dev minimum to eslint 7. Remove support for coffeescript and reconfigured required peer deps

* correctly configure eslint-plugin-json-format for the monorepo and run linting on all json files (previously was not running)

* properly support no duplicate imports
2024-04-26 14:42:33 -04:00

113 lines
2.5 KiB
JavaScript

const path = require('path')
const _ = require('lodash')
const EE = require('events')
const sh = require('shelljs')
// const chalk = require('chalk')
const Promise = require('bluebird')
const debug = require('debug')('lint/util')
const filesRegex = /\.(js|jsx|ts|tsx|coffee|json|eslintrc)$/
Promise.config({
warnings: true,
longStackTraces: true,
})
module.exports = {
lintFilesByText: (options) => {
sh.config.silent = true
EE.defaultMaxListeners = 100
const opts = _.defaults(options, {
getFilenames: null,
getFileText: null,
})
const filenames = opts.getFilenames().filter((v) => filesRegex.test(v))
debug(`linting:
${filenames.join('\n\t')}
`)
return Promise.map(filenames, (f) => {
debug('started linting', f)
const fileText = opts.getFileText(f)
debugTerse('file text:', fileText)
if (!fileText.toString()) return
const lintCommand = `./node_modules/.bin/eslint --stdin --stdin-filename ${sh.ShellString(f)} --color=true`
return Promise.promisify(fileText.exec)(
lintCommand,
{ silent: false, async: true },
)
.tapCatch(debugTerse)
.return(false)
.catchReturn(true)
.finally(() => {
debug('finished linting ', f)
})
}, { concurrency: 0 })
.then((results) => {
const failCount = _.filter(results).length
debug({ failCount })
return { failCount, filenames }
})
},
lintFilesByName: (options) => {
sh.config.silent = true
const opts = _.defaults(options, {
getFilenames: null,
fix: false,
})
const filenames = opts.getFilenames().filter((v) => filesRegex.test(v))
debug(`linting:
${filenames.join('\n\t')}
`)
const filenamesString = sh.ShellString(filenames.join(' '))
const lintCommand = opts.fix ?
`npx eslint --color=true --fix ${filenamesString}`
: `npx eslint --color=true ${filenamesString}`
// always run command in the root of the monorepo!
return Promise.promisify(sh.exec)(
lintCommand,
{ silent: false, async: true, cwd: path.resolve(__dirname, '../../../../') },
)
.tapCatch(debugTerse)
.return(false)
.catchReturn(true)
.then((failed) => {
return {
failed,
filenames,
}
})
},
}
const debugTerse = (...args) => {
args = args.map((arg) => {
let truncated = arg.toString().slice(0, 15)
if (truncated !== arg.toString()) {
truncated = `${truncated}...`
}
return truncated
})
debug(...args)
}