UNPKG

@interaktiv/dia-scripts

Version:

CLI toolbox with common scripts for most sort of projects at DIA

62 lines (48 loc) 2.33 kB
"use strict"; const path = require('path'); const exit = require('exit'); const isCI = require('is-ci'); const spawn = require('cross-spawn'); const yargsParser = require('yargs-parser'); const { fromRoot, hasFile, hasPkgProp, ifFile, isJsFile, isSfdxProject, resolveBin } = require('../../utils'); let rawArgv = process.argv.slice(2); const here = p => path.join(__dirname, p); const hereRelative = p => here(p).replace(process.cwd(), '.'); const argv = yargsParser(rawArgv); const useBuiltinConfig = !rawArgv.includes('--config') && !hasFile('.eslintrc.json') && !hasFile('.eslintrc.js') && !hasPkgProp('eslintConfig'); let config = []; let configFileGiven = useBuiltinConfig || rawArgv.includes('--config'); if (useBuiltinConfig) { config = ['--config', hereRelative('../../config/eslintrc.js')]; } else { const configCandidates = [ifFile('.eslintrc.json', fromRoot('.eslintrc.json')), ifFile('.eslintrc.js', fromRoot('.eslintrc.js'))].filter(Boolean); configFileGiven = configCandidates.length > 0; if (configFileGiven) config = ['--config', configCandidates.pop()]; } const useBuiltinIgnore = !rawArgv.includes('--ignore-path') && !hasFile('.eslintignore') && !hasPkgProp('eslintIgnore'); const ignore = useBuiltinIgnore ? ['--ignore-path', hereRelative('../../config/eslintignore')] : []; const cache = rawArgv.includes('--no-cache') ? [] : ['--cache', '--cache-location', fromRoot('node_modules/.cache/.eslintcache')]; const reportDirectives = rawArgv.includes('--no-report-unused-directives') ? [] : ['--report-unused-disable-directives']; const filesGiven = argv._.length > 0; const filesToApply = filesGiven ? [] : ['.']; const quiet = isCI ? ['--quiet'] : []; let noEslintRc = []; if (isSfdxProject() && configFileGiven) noEslintRc = ['--no-eslintrc']; if (filesGiven) { // We need to take all the flag-less arguments (the files that should be linted) // and filter out the ones that aren't js files. Otherwise json or css files // may be passed through, cause of pre-commit hook for example rawArgv = rawArgv.filter(a => argv._.includes(a) === false || isJsFile(a)); } const result = spawn.sync(resolveBin('eslint'), [...config, ...ignore, ...cache, ...reportDirectives, ...quiet, ...noEslintRc, ...rawArgv, ...filesToApply], { stdio: 'inherit' }); exit(result.status);