Skip to content

Commit

Permalink
modify main executable so it works with commander updates
Browse files Browse the repository at this point in the history
- explicitly call .help() when no arguments are provided
- explicitly set name
- removed one of the ways to create a sample config file since it was
  dead code
- call .opts() to obtain the program's options, since they're no longer
  injected into the program object
  • Loading branch information
juanpcapurro committed Jan 19, 2023
1 parent 6beb38f commit 86a50db
Showing 1 changed file with 15 additions and 19 deletions.
34 changes: 15 additions & 19 deletions solhint.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ function init() {
program.version(version)

program
.name('solhint')
.usage('[options] <file> [...other_files]')
.option('-f, --formatter [name]', 'report formatter name (stylish, table, tap, unix)')
.option('-w, --max-warnings [maxWarningsNumber]', 'number of allowed warnings')
Expand All @@ -39,27 +40,22 @@ function init() {
.description('create configuration file for solhint')
.action(writeSampleConfigFile)

program.parse(process.argv)

if (program.init) {
writeSampleConfigFile()
}

if (program.args.length < 1) {
if (process.argv.length <= 2) {
program.help()
}
program.parse(process.argv)
}

function execMainAction() {
if (program.init) {
if (program.opts().init) {
writeSampleConfigFile()
}

let formatterFn

try {
// to check if is a valid formatter before execute linter
formatterFn = getFormatter(program.formatter)
formatterFn = getFormatter(program.opts().formatter)
} catch (ex) {
console.error(ex.message)
process.exit(1)
Expand All @@ -68,9 +64,9 @@ function execMainAction() {
const reportLists = program.args.filter(_.isString).map(processPath)
const reports = _.flatten(reportLists)
const warningsCount = reports.reduce((acc, i) => acc + i.warningCount, 0)
const warningsNumberExceeded = program.maxWarnings >= 0 && warningsCount > program.maxWarnings
const warningsNumberExceeded = program.opts().maxWarnings >= 0 && warningsCount > program.opts().maxWarnings

if (program.fix) {
if (program.opts().fix) {
for (const report of reports) {
const inputSrc = fs.readFileSync(report.filePath).toString()

Expand All @@ -88,16 +84,16 @@ function execMainAction() {
}
}

if (program.quiet) {
if (program.opts().quiet) {
// filter the list of reports, to set errors only.
reports[0].reports = reports[0].reports.filter(i => i.severity === 2)
}

if (printReports(reports, formatterFn)) {
if (program.maxWarnings && !reports[0].errorCount && warningsNumberExceeded) {
if (program.opts().maxWarnings && !reports[0].errorCount && warningsNumberExceeded) {
console.log(
'Solhint found more warnings than the maximum specified (maximum: %s)',
program.maxWarnings
program.opts().maxWarnings
)
process.exit(1)
}
Expand Down Expand Up @@ -139,8 +135,8 @@ function writeSampleConfigFile() {
const readIgnore = _.memoize(() => {
let ignoreFile = '.solhintignore'
try {
if (program.ignorePath) {
ignoreFile = program.ignorePath
if (program.opts().ignorePath) {
ignoreFile = program.opts().ignorePath
}

return fs
Expand All @@ -149,7 +145,7 @@ const readIgnore = _.memoize(() => {
.split('\n')
.map(i => i.trim())
} catch (e) {
if (program.ignorePath && e.code === 'ENOENT') {
if (program.opts().ignorePath && e.code === 'ENOENT') {
console.error(`\nERROR: ${ignoreFile} is not a valid path.`)
}
return []
Expand All @@ -160,7 +156,7 @@ const readConfig = _.memoize(() => {
let config = {}

try {
config = loadConfig(program.config)
config = loadConfig(program.opts().config)
} catch (e) {
console.log(e.message)
process.exit(1)
Expand Down Expand Up @@ -193,7 +189,7 @@ function getFormatter(formatter) {
try {
return require(`./lib/formatters/${formatterName}`)
} catch (ex) {
ex.message = `\nThere was a problem loading formatter option: ${program.formatter} \nError: ${
ex.message = `\nThere was a problem loading formatter option: ${program.opts().formatter} \nError: ${
ex.message
}`
throw ex
Expand Down

0 comments on commit 86a50db

Please sign in to comment.