UNPKG

@nx/cypress

Version:

The Nx Plugin for Cypress contains executors and generators allowing your workspace to use the powerful Cypress integration testing capabilities.

111 lines (110 loc) 5.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.addLinterToCyProject = addLinterToCyProject; const devkit_1 = require("@nx/devkit"); const eslint_1 = require("@nx/eslint"); const global_eslint_config_1 = require("@nx/eslint/src/generators/init/global-eslint-config"); const eslint_file_1 = require("@nx/eslint/src/generators/utils/eslint-file"); const flat_config_1 = require("@nx/eslint/src/utils/flat-config"); const versions_1 = require("./versions"); async function addLinterToCyProject(tree, options) { if (options.linter === 'none') { return () => { }; } const tasks = []; const projectConfig = (0, devkit_1.readProjectConfiguration)(tree, options.project); const eslintFile = (0, eslint_file_1.findEslintFile)(tree, projectConfig.root); if (!eslintFile) { tasks.push(await (0, eslint_1.lintProjectGenerator)(tree, { project: options.project, linter: options.linter, skipFormat: true, tsConfigPaths: [(0, devkit_1.joinPathFragments)(projectConfig.root, 'tsconfig.json')], setParserOptionsProject: options.setParserOptionsProject, skipPackageJson: options.skipPackageJson, rootProject: options.rootProject, addPlugin: options.addPlugin, })); } if (!options.linter || options.linter !== 'eslint') { return (0, devkit_1.runTasksInSerial)(...tasks); } options.overwriteExisting = options.overwriteExisting || !eslintFile; if (!options.skipPackageJson) { const pkgVersions = (0, versions_1.versions)(tree); tasks.push((0, devkit_1.addDependenciesToPackageJson)(tree, {}, { 'eslint-plugin-cypress': pkgVersions.eslintPluginCypressVersion })); } if ((0, eslint_file_1.isEslintConfigSupported)(tree, projectConfig.root) || (0, eslint_file_1.isEslintConfigSupported)(tree)) { const overrides = []; if ((0, flat_config_1.useFlatConfig)(tree)) { (0, eslint_file_1.addPredefinedConfigToFlatLintConfig)(tree, projectConfig.root, 'recommended', 'cypress', 'eslint-plugin-cypress/flat', false, false); (0, eslint_file_1.addOverrideToLintConfig)(tree, projectConfig.root, { files: ['*.ts', '*.js'], rules: {}, }); } else { if (options.rootProject) { (0, eslint_file_1.addPluginsToLintConfig)(tree, projectConfig.root, '@nx'); overrides.push(global_eslint_config_1.typeScriptOverride); overrides.push(global_eslint_config_1.javaScriptOverride); } const addExtendsTask = (0, eslint_file_1.addExtendsToLintConfig)(tree, projectConfig.root, 'plugin:cypress/recommended'); tasks.push(addExtendsTask); } const cyVersion = (0, versions_1.getInstalledCypressMajorVersion)(tree); /** * We need this override because we enabled allowJS in the tsconfig to allow for JS based Cypress tests. * That however leads to issues with the CommonJS Cypress plugin file. */ const cy6Override = { files: [`${options.cypressDir}/plugins/index.js`], rules: { '@typescript-eslint/no-var-requires': 'off', 'no-undef': 'off', }, }; const addCy6Override = cyVersion && cyVersion < 7; if (options.overwriteExisting) { overrides.unshift({ files: (0, flat_config_1.useFlatConfig)(tree) ? // For flat configs we don't need to specify the files undefined : ['*.ts', '*.tsx', '*.js', '*.jsx'], parserOptions: !options.setParserOptionsProject ? undefined : { project: `${projectConfig.root}/tsconfig.*?.json`, }, rules: {}, }); if (addCy6Override) { overrides.push(cy6Override); } (0, eslint_file_1.replaceOverridesInLintConfig)(tree, projectConfig.root, overrides); } else { overrides.unshift({ files: (0, flat_config_1.useFlatConfig)(tree) ? // For flat configs we don't need to specify the files undefined : [ '*.cy.{ts,js,tsx,jsx}', `${options.cypressDir}/**/*.{ts,js,tsx,jsx}`, ], parserOptions: !options.setParserOptionsProject ? undefined : { project: `${projectConfig.root}/tsconfig.*?.json`, }, rules: {}, }); if (addCy6Override) { overrides.push(cy6Override); } overrides.forEach((override) => (0, eslint_file_1.addOverrideToLintConfig)(tree, projectConfig.root, override)); } } return (0, devkit_1.runTasksInSerial)(...tasks); }