'use strict'; var fs = require('fs'); var promises = require('fs/promises'); var path = require('path'); var debug = require('debug'); const log = debug('grunt-esig'); 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(ignoreFileName, currentWorkingDir, projectRootDir) { const gruntFileDir = currentWorkingDir ? path.resolve(currentWorkingDir) : process.cwd(); const ignoreFileDir = path.isAbsolute(projectRootDir) ? projectRootDir : path.resolve(path.join(gruntFileDir, projectRootDir)); const ignoreFilePath = path.join(ignoreFileDir, ignoreFileName || '.eslintignore'); log('gruntfile directory is %s', gruntFileDir); log('ignore file directory is %s', ignoreFileDir); log('ignore file path is %s', ignoreFilePath); return { gruntFileDir, ignoreFileDir, ignoreFilePath } } function transformIgnoreLines(ignoreFileDir, gruntFileDir, ignoreLines) { const relativePathToRoot = path.relative(gruntFileDir, ignoreFileDir).replaceAll('\\', '/'); log('transforming %d ignore lines to path %s', ignoreLines.length, relativePathToRoot); const ignores = ignoreLines .map(line => line.trim()) .filter(line => line && !line.startsWith('#')); const skippedLines = ignoreLines.length - ignores.length; if (skippedLines) { log('skipping %d ignore line(s)', skippedLines); } return ignores.map(ignore => transFormIgnore(ignore, relativePathToRoot)) } async function readEslintIgnoreFile({ ignoreFileName, currentWorkingDir, projectRootDir, maxDepthToRoot } = {}) { if (!projectRootDir) { projectRootDir = await findProjectRootDir(currentWorkingDir, maxDepthToRoot); } const { gruntFileDir, ignoreFileDir, ignoreFilePath } = computeDirsAndPaths(ignoreFileName, currentWorkingDir, projectRootDir); if (!await exists(ignoreFilePath)) { log('ignore file not found'); return [] } log('reading %s', ignoreFilePath); const ignoreLines = (await promises.readFile(ignoreFilePath, 'utf8')).split(/\r?\n/); return transformIgnoreLines(ignoreFileDir, gruntFileDir, ignoreLines) } function readEslintIgnoreFileSync({ ignoreFileName, currentWorkingDir, projectRootDir, maxDepthToRoot } = {}) { if (!projectRootDir) { projectRootDir = findProjectRootDirSync(currentWorkingDir, maxDepthToRoot); } const { gruntFileDir, ignoreFileDir, ignoreFilePath } = computeDirsAndPaths(ignoreFileName, currentWorkingDir, projectRootDir); if (!fs.existsSync(ignoreFilePath)) { log('ignore file not found'); return [] } log('reading %s', ignoreFilePath); const ignoreLines = fs.readFileSync(ignoreFilePath, 'utf8').split(/\r?\n/); return transformIgnoreLines(ignoreFileDir, gruntFileDir, ignoreLines) } exports.readEslintIgnoreFile = readEslintIgnoreFile; exports.readEslintIgnoreFileSync = readEslintIgnoreFileSync; //# sourceMappingURL=index.cjs.map