@nx/cypress
Version:
93 lines (92 loc) • 4.99 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.migrateCypressProject = migrateCypressProject;
const devkit_1 = require("@nx/devkit");
const executor_options_utils_1 = require("@nx/devkit/src/generators/executor-options-utils");
const versions_1 = require("../../utils/versions");
const conversion_util_1 = require("./conversion.util");
function migrateCypressExecutorOptions(tree, projectName, target, configuration, previousCypressConfigs) {
try {
const projectConfig = (0, devkit_1.readProjectConfiguration)(tree, projectName);
const { cypressConfigPathJson, cypressConfigPathTs } = (0, conversion_util_1.findCypressConfigs)(tree, projectConfig, target, configuration);
if (!cypressConfigPathJson && !cypressConfigPathTs) {
return;
}
// a matching cypress ts file hasn't been made yet. need to migrate.
if (tree.exists(cypressConfigPathJson) &&
!tree.exists(cypressConfigPathTs)) {
let cypressConfigs = (0, conversion_util_1.createNewCypressConfig)(tree, projectConfig, cypressConfigPathJson);
cypressConfigs = (0, conversion_util_1.updatePluginFile)(tree, projectConfig, cypressConfigs);
(0, conversion_util_1.writeNewConfig)(tree, cypressConfigPathTs, cypressConfigs);
previousCypressConfigs.set(cypressConfigPathJson, cypressConfigs);
tree.delete(cypressConfigPathJson);
}
// ts file has been made and matching json file has been removed only need to update the project config
if (!tree.exists(cypressConfigPathJson) &&
tree.exists(cypressConfigPathTs)) {
const cypressConfigs = previousCypressConfigs.get(cypressConfigPathJson);
(0, conversion_util_1.updateProjectPaths)(tree, projectConfig, cypressConfigs);
(0, conversion_util_1.addConfigToTsConfig)(tree, (configuration
? projectConfig.targets?.[target]?.configurations?.[configuration]
?.tsConfig
: projectConfig.targets?.[target]?.options?.tsConfig) ||
(0, devkit_1.joinPathFragments)(projectConfig.root, 'tsconfig.json'), cypressConfigPathTs);
if (configuration) {
projectConfig.targets[target].configurations[configuration] = {
...projectConfig.targets[target].configurations[configuration],
cypressConfig: cypressConfigPathTs,
};
}
else {
projectConfig.targets[target].options = {
...projectConfig.targets[target].options,
cypressConfig: cypressConfigPathTs,
};
}
projectConfig.targets[target].options.testingType = 'e2e';
(0, devkit_1.updateProjectConfiguration)(tree, projectName, projectConfig);
}
}
catch (e) {
devkit_1.logger.error((0, devkit_1.stripIndents) `
NX There was an error converting ${projectName}:${target}.
You can manually update the project by following the migration guide if need be.
https://nx.dev/cypress/v10-migration-guide
`);
throw e;
}
}
async function migrateCypressProject(tree) {
(0, versions_1.assertMinimumCypressVersion)(8);
if ((0, versions_1.getInstalledCypressMajorVersion)(tree) >= 10) {
devkit_1.logger.info('NX This workspace is already using Cypress v10+');
return;
}
// keep history of cypress configs as some workspaces share configs
// if we don't have the already migrated configs
// it prevents us from being able to rename files for those projects
const previousCypressConfigs = new Map();
(0, executor_options_utils_1.forEachExecutorOptions)(tree, '@nx/cypress:cypress', (currentValue, projectName, target, configuration) => {
migrateCypressExecutorOptions(tree, projectName, target, configuration, previousCypressConfigs);
});
(0, executor_options_utils_1.forEachExecutorOptions)(tree, '@nrwl/cypress:cypress', (currentValue, projectName, target, configuration) => {
migrateCypressExecutorOptions(tree, projectName, target, configuration, previousCypressConfigs);
});
(0, devkit_1.updateJson)(tree, 'package.json', (json) => {
json.devDependencies['cypress'] = '^11.2.0';
return json;
});
await (0, devkit_1.formatFiles)(tree);
if (tree.exists('cypress.json')) {
devkit_1.logger.warn((0, devkit_1.stripIndents) `A root cypress.json file was found.
You should remove this file as it will cause an error when running Cypress.
If you want to share options between Cypress projects.
You can create a root ts file and import it into each project's cypress config file.
More Info: https://github.com/nrwl/nx/issues/11512#issuecomment-1213420638
`);
}
return () => {
(0, devkit_1.installPackagesTask)(tree);
};
}
exports.default = migrateCypressProject;