'use strict'; var fs = require('fs'); var path = require('path'); var debug = require('debug'); var promises = require('fs/promises'); var gruntEslintignore = require('grunt-eslintignore'); const log = debug('grunt-escf'); async function exists(file) { try { log('checking existence of %s', file); await promises.access(file); log('%s was found', file); return true } catch (error) { if (error.code !== 'ENOENT') throw error log('%s was not found', file); } } async function findProjectRootDir(currentWorkingDir, maxDepthToRoot = 10) { let dir = currentWorkingDir || '.'; for (let i = 0; i < maxDepthToRoot; ++i) { log('looking for package.json in %s', dir); if (await exists(path.join(dir, 'package.json'))) { log('package.json found in %s', dir); return dir } dir = path.join('..', dir); } log('package.json not found in %d parent directories', maxDepthToRoot); throw new Error('Project root not found') } function findProjectRootDirSync(currentWorkingDir, maxDepthToRoot = 10) { let dir = currentWorkingDir || '.'; for (let i = 0; i < maxDepthToRoot; ++i) { log('looking for package.json in %s', dir); if (fs.existsSync(path.join(dir, 'package.json'))) { log('package.json found in %s', dir); return dir } dir = path.join('..', dir); } log('package.json not found in %d parent directories', maxDepthToRoot); throw new Error('Project root not found') } function transFormIgnore(ignore, relativePathToRoot) { let newIgnore = ignore; if (newIgnore.endsWith('/')) { newIgnore += '**/*'; } newIgnore = newIgnore.startsWith('!') ? `${path.join(relativePathToRoot, newIgnore.slice(1))}` : `!${path.join(relativePathToRoot, newIgnore)}`; log('transforming %s to %s', ignore, newIgnore); return newIgnore } function computeDirsAndPaths(currentWorkingDir, projectRootDir) { log('initial current directory is %s', currentWorkingDir); const gruntFileDir = currentWorkingDir ? path.resolve(currentWorkingDir) : process.cwd(); const configFileDir = path.isAbsolute(projectRootDir) ? projectRootDir : path.resolve(path.join(gruntFileDir, projectRootDir)); const relativePathToRoot = path.relative(gruntFileDir, configFileDir).replaceAll('\\', '/'); log('gruntfile directory is %s', gruntFileDir); log('config file directory is %s', configFileDir); log('relative path to root directory is %s', relativePathToRoot); return { configFileDir, relativePathToRoot } } const configFileNames = ['.eslintrc.js', '.eslintrc.cjs']; async function findConfigFile(configFileName, configFileDir) { if (configFileName) { log('checking config file %s', configFileName); const configFilePath = path.join(configFileDir, configFileName); if (await exists(configFilePath)) return configFilePath } else { for (configFileName of configFileNames) { log('trying config file %s', configFileName); const configFilePath = path.join(configFileDir, configFileName); if (await exists(configFilePath)) return configFilePath } } throw new Error('ESLint config file not found') } function findConfigFileSync(configFileName, configFileDir) { if (configFileName) { log('checking config file %s', configFileName); const configFilePath = path.join(configFileDir, configFileName); if (fs.existsSync(configFilePath)) return configFilePath } else { for (configFileName of configFileNames) { log('trying config file %s', configFileName); const configFilePath = path.join(configFileDir, configFileName); if (fs.existsSync(configFilePath)) return configFilePath } } throw new Error('ESLint config file not found') } function transformIgnores(relativePathToRoot, ignores) { log('transforming %d ignores to the path %s', ignores.length, relativePathToRoot); return ignores.map(ignore => transFormIgnore(ignore, relativePathToRoot)) } function extractIgnores(config) { const ignores = config.ignorePatterns || []; log('extracted %d ignores from config', ignores.length); return ignores } function transformConfig(configFilePath, relativePathToRoot, files, ignores) { return { options: { overrideConfigFile: configFilePath }, validate: (files || [path.join(relativePathToRoot, '**/*.{js,cjs,mjs}')]).concat(ignores) } } async function readEslintConfigFile({ configFileName, ignoreFileName, currentWorkingDir, projectRootDir, maxDepthToRoot, scriptFiles } = {}) { if (!projectRootDir) { projectRootDir = await findProjectRootDir(currentWorkingDir, maxDepthToRoot); } const { configFileDir, relativePathToRoot } = computeDirsAndPaths(currentWorkingDir, projectRootDir); const configFilePath = await findConfigFile(configFileName, configFileDir); log('importing %s', configFilePath); const config = require(configFilePath); let ignores = extractIgnores(config); ignores = ignores.length ? transformIgnores(relativePathToRoot, ignores) : await gruntEslintignore.readEslintIgnoreFile({ ignoreFileName, currentWorkingDir, projectRootDir, maxDepthToRoot }); return transformConfig(configFilePath, relativePathToRoot, scriptFiles, ignores) } function readEslintConfigFileSync({ configFileName, ignoreFileName, currentWorkingDir, projectRootDir, maxDepthToRoot, scriptFiles } = {}) { if (!projectRootDir) { projectRootDir = findProjectRootDirSync(currentWorkingDir, maxDepthToRoot); } const { configFileDir, relativePathToRoot } = computeDirsAndPaths(currentWorkingDir, projectRootDir); const configFilePath = findConfigFileSync(configFileName, configFileDir); log('importing %s', configFilePath); const config = require(configFilePath); let ignores = extractIgnores(config); ignores = ignores.length ? transformIgnores(relativePathToRoot, ignores) : gruntEslintignore.readEslintIgnoreFileSync({ ignoreFileName, currentWorkingDir, projectRootDir, maxDepthToRoot }); return transformConfig(configFilePath, relativePathToRoot, scriptFiles, ignores) } exports.readEslintConfigFile = readEslintConfigFile; exports.readEslintConfigFileSync = readEslintConfigFileSync; //# sourceMappingURL=index.cjs.map