@nx/cypress
Version:
108 lines (107 loc) • 4.25 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.addPlugin = addPlugin;
exports.cypressInitGenerator = cypressInitGenerator;
exports.cypressInitGeneratorInternal = cypressInitGeneratorInternal;
const internal_1 = require("@nx/devkit/internal");
const devkit_1 = require("@nx/devkit");
const plugin_1 = require("../../plugins/plugin");
const assert_supported_cypress_version_1 = require("../../utils/assert-supported-cypress-version");
const versions_1 = require("../../utils/versions");
function setupE2ETargetDefaults(tree) {
const nxJson = (0, devkit_1.readNxJson)(tree);
if (!nxJson.namedInputs) {
return;
}
// E2e targets depend on all their project's sources + production sources of dependencies
const productionFileSet = !!nxJson.namedInputs?.production;
const existing = findExistingE2eDefault(nxJson.targetDefaults);
const patch = {};
if (existing?.cache === undefined)
patch.cache = true;
if (existing?.inputs === undefined) {
patch.inputs = ['default', productionFileSet ? '^production' : '^default'];
}
if (Object.keys(patch).length > 0) {
(0, internal_1.upsertTargetDefault)(tree, nxJson, { target: 'e2e', ...patch });
(0, devkit_1.updateNxJson)(tree, nxJson);
}
}
function findExistingE2eDefault(td) {
if (!td)
return undefined;
const value = td['e2e'];
if (value === undefined)
return undefined;
if (Array.isArray(value)) {
const found = value.find((e) => e.filter === undefined);
if (!found)
return undefined;
const { filter: _f, ...rest } = found;
return rest;
}
return value;
}
function updateDependencies(tree, options) {
const tasks = [];
tasks.push((0, devkit_1.removeDependenciesFromPackageJson)(tree, ['@nx/cypress'], []));
const devDependencies = {
['@nx/cypress']: versions_1.nxVersion,
};
if (!(0, versions_1.getInstalledCypressVersion)(tree)) {
devDependencies.cypress = versions_1.cypressVersion;
}
tasks.push((0, devkit_1.addDependenciesToPackageJson)(tree, {}, devDependencies, undefined, options.keepExistingVersions ?? true));
return (0, devkit_1.runTasksInSerial)(...tasks);
}
function addPlugin(tree, graph, updatePackageScripts) {
return (0, internal_1.addPlugin)(tree, graph, '@nx/cypress/plugin', plugin_1.createNodesV2, {
targetName: ['e2e', 'cypress:e2e', 'cypress-e2e'],
openTargetName: ['open-cypress', 'cypress-open'],
componentTestingTargetName: [
'component-test',
'cypress:component-test',
'cypress-component-test',
],
ciTargetName: ['e2e-ci', 'cypress:e2e-ci', 'cypress-e2e-ci'],
}, updatePackageScripts);
}
function updateProductionFileset(tree) {
const nxJson = (0, devkit_1.readNxJson)(tree);
const productionFileset = nxJson.namedInputs?.production;
if (productionFileset) {
nxJson.namedInputs.production = Array.from(new Set([
...productionFileset,
'!{projectRoot}/cypress/**/*',
'!{projectRoot}/**/*.cy.[jt]s?(x)',
'!{projectRoot}/cypress.config.[jt]s',
]));
}
(0, devkit_1.updateNxJson)(tree, nxJson);
}
async function cypressInitGenerator(tree, options) {
return cypressInitGeneratorInternal(tree, { addPlugin: false, ...options });
}
async function cypressInitGeneratorInternal(tree, options) {
(0, assert_supported_cypress_version_1.assertSupportedCypressVersion)(tree);
updateProductionFileset(tree);
const nxJson = (0, devkit_1.readNxJson)(tree);
options.addPlugin ??=
process.env.NX_ADD_PLUGINS !== 'false' &&
nxJson.useInferencePlugins !== false;
if (options.addPlugin) {
await addPlugin(tree, await (0, devkit_1.createProjectGraphAsync)(), options.updatePackageScripts);
}
else {
setupE2ETargetDefaults(tree);
}
let installTask = () => { };
if (!options.skipPackageJson) {
installTask = updateDependencies(tree, options);
}
if (!options.skipFormat) {
await (0, devkit_1.formatFiles)(tree);
}
return installTask;
}
exports.default = cypressInitGenerator;