UNPKG

@nx/plugin

Version:

This plugin is used to create Nx plugins! It contains generators for generating common plugin features like generators, executors, migrations and more.

197 lines (196 loc) • 9.19 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = pluginLintCheckGenerator; exports.addMigrationJsonChecks = addMigrationJsonChecks; exports.getEsLintOptions = getEsLintOptions; const devkit_1 = require("@nx/devkit"); const logger_1 = require("nx/src/utils/logger"); const package_json_1 = require("nx/src/utils/package-json"); const eslint_file_1 = require("@nx/eslint/src/generators/utils/eslint-file"); const flat_config_1 = require("@nx/eslint/src/utils/flat-config"); async function pluginLintCheckGenerator(host, options) { const project = (0, devkit_1.readProjectConfiguration)(host, options.projectName); const packageJson = (0, devkit_1.readJson)(host, (0, devkit_1.joinPathFragments)(project.root, 'package.json')); // This rule is eslint **only** if (projectIsEsLintEnabled(host, project)) { updateRootEslintConfig(host); updateProjectEslintConfig(host, project, packageJson); updateProjectTarget(host, options, packageJson); // Project is setup for vscode if (host.exists('.vscode')) { setupVsCodeLintingForJsonFiles(host); } } else { devkit_1.logger.error(`${logger_1.NX_PREFIX} plugin lint checks can only be added to plugins which use eslint for linting`); } if (!options.skipFormat) { await (0, devkit_1.formatFiles)(host); } } function addMigrationJsonChecks(host, options, packageJson) { const projectConfiguration = (0, devkit_1.readProjectConfiguration)(host, options.projectName); if (!projectIsEsLintEnabled(host, projectConfiguration)) { return; } const [eslintTarget, eslintTargetConfiguration] = getEsLintOptions(projectConfiguration) ?? []; const relativeMigrationsJsonPath = (0, package_json_1.readNxMigrateConfig)(packageJson).migrations; if (!relativeMigrationsJsonPath) { return; } const migrationsJsonPath = (0, devkit_1.joinPathFragments)(projectConfiguration.root, relativeMigrationsJsonPath); if (!eslintTarget) { return; } // Add path to lintFilePatterns if different than default "{projectRoot}" if (eslintTargetConfiguration.options?.lintFilePatterns && !eslintTargetConfiguration.options.lintFilePatterns.includes(migrationsJsonPath)) { eslintTargetConfiguration.options.lintFilePatterns.push(migrationsJsonPath); (0, devkit_1.updateProjectConfiguration)(host, options.projectName, projectConfiguration); } // Update project level eslintrc (0, eslint_file_1.updateOverrideInLintConfig)(host, projectConfiguration.root, (o) => Object.keys(o.rules ?? {})?.includes('@nx/nx-plugin-checks') || Object.keys(o.rules ?? {})?.includes('@nrwl/nx/nx-plugin-checks'), (o) => { const fileSet = new Set(Array.isArray(o.files) ? o.files : [o.files]); fileSet.add(relativeMigrationsJsonPath); return { ...o, files: formatFilesEntries(host, Array.from(fileSet)), }; }); } function formatFilesEntries(tree, files) { if (!(0, flat_config_1.useFlatConfig)(tree)) { return files; } const filesAfter = files.map((f) => { const after = f.startsWith('./') ? f.replace('./', '**/') : f; return after; }); return filesAfter; } function updateProjectTarget(host, options, packageJson) { const project = (0, devkit_1.readProjectConfiguration)(host, options.projectName); if (!project.targets) { return; } for (const [target, configuration] of Object.entries(project.targets)) { if (configuration.executor === '@nx/eslint:lint' && // only add patterns if there are already hardcoded ones configuration.options?.lintFilePatterns) { const opts = configuration.options ?? {}; opts.lintFilePatterns ??= []; if (packageJson.generators) { opts.lintFilePatterns.push((0, devkit_1.joinPathFragments)(project.root, packageJson.generators)); } if (packageJson.schematics && packageJson.schematics !== packageJson.generators) { opts.lintFilePatterns.push((0, devkit_1.joinPathFragments)(project.root, packageJson.schematics)); } if (packageJson.executors) { opts.lintFilePatterns.push((0, devkit_1.joinPathFragments)(project.root, packageJson.executors)); } if (packageJson.builders && packageJson.builders !== packageJson.executors) { opts.lintFilePatterns.push((0, devkit_1.joinPathFragments)(project.root, packageJson.builders)); } opts.lintFilePatterns.push(`${project.root}/package.json`); opts.lintFilePatterns = [...new Set(opts.lintFilePatterns)]; project.targets[target].options = opts; } } (0, devkit_1.updateProjectConfiguration)(host, options.projectName, project); } function updateProjectEslintConfig(host, options, packageJson) { if ((0, eslint_file_1.isEslintConfigSupported)(host, options.root)) { const lookup = (o) => Object.keys(o.rules ?? {}).includes('@nx/nx-plugin-checks') || Object.keys(o.rules ?? {}).includes('@nrwl/nx/nx-plugin-checks'); const files = [ './package.json', packageJson.generators, packageJson.executors, packageJson.schematics, packageJson.builders, (0, package_json_1.readNxMigrateConfig)(packageJson).migrations, ].filter((f) => !!f); const parser = (0, flat_config_1.useFlatConfig)(host) ? { languageOptions: { parser: 'jsonc-eslint-parser' } } : { parser: 'jsonc-eslint-parser' }; if ((0, eslint_file_1.lintConfigHasOverride)(host, options.root, lookup)) { // update it (0, eslint_file_1.updateOverrideInLintConfig)(host, options.root, lookup, (o) => ({ ...o, files: formatFilesEntries(host, [ ...new Set([ ...(Array.isArray(o.files) ? o.files : [o.files]), ...files, ]), ]), ...parser, rules: { ...o.rules, '@nx/nx-plugin-checks': 'error', }, })); } else { // add it (0, eslint_file_1.addOverrideToLintConfig)(host, options.root, { files: formatFilesEntries(host, files), ...parser, rules: { '@nx/nx-plugin-checks': 'error', }, }); } } } // Update the root eslint to specify a parser for json files // This is required, otherwise every json file that is not overriden // will display false errors in the IDE function updateRootEslintConfig(host) { if ((0, eslint_file_1.isEslintConfigSupported)(host)) { if (!(0, eslint_file_1.lintConfigHasOverride)(host, '', (o) => Array.isArray(o.files) ? o.files.some((f) => f.match(/\.json$/)) : !!o.files?.match(/\.json$/), true)) { (0, eslint_file_1.addOverrideToLintConfig)(host, '', { files: '*.json', parser: 'jsonc-eslint-parser', rules: {}, }, { checkBaseConfig: true }); } } else { devkit_1.output.note({ title: 'Unable to update root eslint config.', bodyLines: [ 'We only automatically update the root eslint config if it is json or flat config.', 'If you are using a different format, you will need to update it manually.', 'You need to set the parser to jsonc-eslint-parser for json files.', ], }); } } function setupVsCodeLintingForJsonFiles(host) { let existing = {}; if (host.exists('.vscode/settings.json')) { existing = (0, devkit_1.readJson)(host, '.vscode/settings.json'); } else { devkit_1.logger.info(`${logger_1.NX_PREFIX} We've updated the vscode settings for this repository to ensure that plugin lint checks show up inside your IDE. This created .vscode/settings.json. To read more about this file, check vscode's documentation. It is frequently not committed, so other developers may need to add similar settings if they'd like to see the lint checks in the IDE rather than only during linting.`); } // setup eslint validation for json files const eslintValidate = existing['eslint.validate'] ?? []; if (!eslintValidate.includes('json')) { existing['eslint.validate'] = [...eslintValidate, 'json']; } (0, devkit_1.writeJson)(host, '.vscode/settings.json', existing); } function projectIsEsLintEnabled(tree, project) { return !!(0, eslint_file_1.findEslintFile)(tree, project.root); } function getEsLintOptions(project) { return Object.entries(project.targets || {}).find(([, x]) => x.executor === '@nx/eslint:lint' || x.executor === '@nx/linter:eslint' || x.executor === '@nrwl/linter:eslint'); }