@nx/playwright
Version:
89 lines (88 loc) • 4.4 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/playwright/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');
const ast = tsquery_1.tsquery.ast(configFileContents);
const CI_WEBSERVER_COMMAND_SELECTOR = 'PropertyAssignment:has(Identifier[name=webServer]) PropertyAssignment:has(Identifier[name=command]) > StringLiteral';
const nodes = (0, tsquery_1.tsquery)(ast, CI_WEBSERVER_COMMAND_SELECTOR, {
visitAllChildren: true,
});
if (!nodes.length) {
continue;
}
const ciWebServerCommand = nodes[0].getText().replace(/['"]/g, '');
let serveStaticProject;
let serveStaticTarget;
let serveStaticConfiguration;
if (ciWebServerCommand.includes('nx run')) {
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);
serveStaticProject = project;
serveStaticTarget = target;
serveStaticConfiguration = configuration;
}
else {
const NX_PROJECT_REGEX = 'nx\\s+([^ ]+)\\s+([^ ]+)';
const matches = ciWebServerCommand.match(NX_PROJECT_REGEX);
if (!matches) {
return;
}
serveStaticTarget = matches[1];
serveStaticProject = matches[2];
}
const resolvedServeStaticTarget = graph.nodes[serveStaticProject]?.data?.targets?.[serveStaticTarget];
if (!resolvedServeStaticTarget) {
continue;
}
let resolvedBuildTarget;
if (resolvedServeStaticTarget.dependsOn) {
resolvedBuildTarget = resolvedServeStaticTarget.dependsOn.join(',');
}
else {
resolvedBuildTarget =
(serveStaticConfiguration
? resolvedServeStaticTarget.configurations[serveStaticConfiguration]
.buildTarget
: resolvedServeStaticTarget.options.buildTarget) ?? 'build';
}
const targetDependsOnSelf = graph.nodes[serveStaticProject].data.root === (0, path_1.dirname)(configFile);
const buildTarget = `${targetDependsOnSelf ? '' : '^'}${resolvedBuildTarget}`;
await (0, target_defaults_utils_1.addE2eCiTargetDefaults)(tree, pluginName, buildTarget, configFile);
}
}
}