UNPKG

@o3r/schematics

Version:

Schematics module of the Otter framework

62 lines 2.55 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.applyEsLintFix = applyEsLintFix; const node_path_1 = require("node:path"); const schematics_1 = require("@angular-devkit/schematics"); const index_1 = require("../../tasks/index"); /** * Apply EsLint fix * @param _prootPath Root path * @param extension List of file extensions to lint * @param options Linter options */ function applyEsLintFix(_prootPath = '/', extension = ['ts'], options) { try { require.resolve('eslint/package.json'); } catch { return (0, schematics_1.noop)(); } const linterOptions = { continueOnError: true, hideWarnings: true, ...options }; return (tree, context) => { const filesToBeLint = tree.actions .filter((a) => a.kind !== 'd' && extension.includes((0, node_path_1.extname)(a.path))) .map((action) => action.path.substring(1)); if (tree.root.subfiles.some((f) => /eslint\.config\.{m,c,}[jt]s/.test(f))) { context.addTask(new index_1.EslintFixTask(Array.from(filesToBeLint), undefined, undefined, linterOptions), options?.dependencyTaskIds); return tree; } // directory of the deepest file let dir = tree.getDir((0, node_path_1.dirname)(filesToBeLint.reduce((acc, path) => { const level = path.split('/').length; if (acc.level < level) { return { level, path }; } return acc; }, { level: 0, path: '' }).path)); let eslintRcFile; do { eslintRcFile = dir.subfiles.find((f) => f.startsWith('.eslintrc')); if (eslintRcFile) { eslintRcFile = (0, node_path_1.join)(dir.path.substring(1), eslintRcFile); break; } dir = dir.parent; } while (dir !== null); if (dir === null || !eslintRcFile) { context.logger.warn(`Asked to run lint fixes, but could not find a eslintrc config file. You can consider to run later the following command to add otter linter rules: ng add @o3r/eslint-config`); return tree; } const files = new Set(filesToBeLint.filter((filePath) => filePath.startsWith(dir.path.substring(1)))); if (files.size > 0) { context.addTask(new index_1.EslintFixTask(Array.from(files), undefined, eslintRcFile, linterOptions), options?.dependencyTaskIds); } return tree; }; } //# sourceMappingURL=index.js.map