@nx/cypress
Version:
75 lines (74 loc) • 3.67 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = addE2eCiTargetDefaults;
const devkit_1 = require("@nx/devkit");
const target_defaults_utils_1 = require("@nx/devkit/src/generators/target-defaults-utils");
const loaded_nx_plugin_1 = require("nx/src/project-graph/plugins/loaded-nx-plugin");
const devkit_internals_1 = require("nx/src/devkit-internals");
const tsquery_1 = require("@phenomnomnominal/tsquery");
const path_1 = require("path");
async function addE2eCiTargetDefaults(tree) {
const pluginName = '@nx/cypress/plugin';
const graph = await (0, devkit_1.createProjectGraphAsync)();
const nxJson = (0, devkit_1.readNxJson)(tree);
const matchingPluginRegistrations = nxJson.plugins?.filter((p) => typeof p === 'string' ? p === pluginName : p.plugin === pluginName);
if (!matchingPluginRegistrations) {
return;
}
const { createNodesV2, } = await Promise.resolve(`${pluginName}`).then(s => require(s));
for (const plugin of matchingPluginRegistrations) {
let projectConfigs;
try {
const loadedPlugin = new loaded_nx_plugin_1.LoadedNxPlugin({ createNodesV2, name: pluginName }, plugin);
projectConfigs = await (0, devkit_internals_1.retrieveProjectConfigurations)([loadedPlugin], tree.root, nxJson);
}
catch (e) {
if (e instanceof devkit_internals_1.ProjectConfigurationsError) {
projectConfigs = e.partialProjectConfigurationsResult;
}
else {
throw e;
}
}
for (const configFile of projectConfigs.matchingProjectFiles) {
const configFileContents = tree.read(configFile, 'utf-8');
if (!configFileContents.includes('ciWebServerCommand')) {
continue;
}
const ast = tsquery_1.tsquery.ast(configFileContents);
const CI_WEBSERVER_COMMAND_SELECTOR = 'ObjectLiteralExpression PropertyAssignment:has(Identifier[name=ciWebServerCommand]) > StringLiteral';
const nodes = (0, tsquery_1.tsquery)(ast, CI_WEBSERVER_COMMAND_SELECTOR, {
visitAllChildren: true,
});
if (!nodes.length) {
continue;
}
const ciWebServerCommand = nodes[0].getText();
const NX_TARGET_REGEX = "(?<=nx run )[^']+";
const matches = ciWebServerCommand.match(NX_TARGET_REGEX);
if (!matches) {
continue;
}
const targetString = matches[0];
const { project, target, configuration } = (0, devkit_1.parseTargetString)(targetString, graph);
const targetDependsOnSelf = graph.nodes[project].data.root === (0, path_1.dirname)(configFile);
const serveStaticTarget = graph.nodes[project].data.targets[target];
if (!serveStaticTarget) {
continue;
}
let resolvedBuildTarget;
if (serveStaticTarget?.dependsOn) {
resolvedBuildTarget = serveStaticTarget.dependsOn.join(',');
}
else {
resolvedBuildTarget =
(configuration
? serveStaticTarget.configurations[configuration].buildTarget
: serveStaticTarget.options.buildTarget) ?? 'build';
}
const buildTarget = `${targetDependsOnSelf ? '' : '^'}${resolvedBuildTarget}`;
await (0, target_defaults_utils_1.addE2eCiTargetDefaults)(tree, pluginName, buildTarget, configFile);
}
}
await (0, devkit_1.formatFiles)(tree);
}