UNPKG

@nx/angular

Version:

The Nx Plugin for Angular contains executors, generators, and utilities for managing Angular applications and libraries within an Nx workspace. It provides: - Integration with libraries such as Storybook, Jest, ESLint, Tailwind CSS, Playwright and Cypre

146 lines (144 loc) 6.13 kB
"use strict"; /** * @license * Copyright Google LLC All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.dev/license */ Object.defineProperty(exports, "__esModule", { value: true }); exports.generateDefaultKarmaConfig = generateDefaultKarmaConfig; exports.compareKarmaConfigs = compareKarmaConfigs; exports.hasDifferences = hasDifferences; exports.compareKarmaConfigToDefault = compareKarmaConfigToDefault; /** * Adapts the private utility from Angular CLI to be used in the migration. */ const promises_1 = require("node:fs/promises"); const node_path_1 = require("node:path"); const node_util_1 = require("node:util"); const karma_config_analyzer_1 = require("./karma-config-analyzer"); /** * Generates the default Karma configuration file content as a string. * @param relativePathToWorkspaceRoot The relative path from the Karma config file to the workspace root. * @param projectName The name of the project. * @param needDevkitPlugin A boolean indicating if the devkit plugin is needed. * @returns The content of the default `karma.conf.js` file. */ async function generateDefaultKarmaConfig(relativePathToWorkspaceRoot, projectName, needDevkitPlugin) { let template = await getKarmaConfigTemplate(); // TODO: Replace this with the actual schematic templating logic. template = template .replace(/<%= relativePathToWorkspaceRoot %>/g, (0, node_path_1.normalize)(relativePathToWorkspaceRoot).replace(/\\/g, '/')) .replace(/<%= folderName %>/g, projectName); const devkitPluginRegex = /<% if \(needDevkitPlugin\) { %>(.*?)<% } %>/gs; const replacement = needDevkitPlugin ? '$1' : ''; template = template.replace(devkitPluginRegex, replacement); return template; } /** * Compares two Karma configuration analyses and returns the difference. * @param projectAnalysis The analysis of the project's configuration. * @param defaultAnalysis The analysis of the default configuration to compare against. * @returns A diff object representing the changes between the two configurations. */ function compareKarmaConfigs(projectAnalysis, defaultAnalysis) { const added = new Map(); const removed = new Map(); const modified = new Map(); const allKeys = new Set([ ...projectAnalysis.settings.keys(), ...defaultAnalysis.settings.keys(), ]); for (const key of allKeys) { const projectValue = projectAnalysis.settings.get(key); const defaultValue = defaultAnalysis.settings.get(key); if (projectValue !== undefined && defaultValue === undefined) { added.set(key, projectValue); } else if (projectValue === undefined && defaultValue !== undefined) { removed.set(key, defaultValue); } else if (projectValue !== undefined && defaultValue !== undefined) { if (!(0, node_util_1.isDeepStrictEqual)(projectValue, defaultValue)) { modified.set(key, { projectValue, defaultValue }); } } } return { added, removed, modified, isReliable: !projectAnalysis.hasUnsupportedValues && !defaultAnalysis.hasUnsupportedValues, }; } /** * Checks if there are any differences in the provided Karma configuration diff. * @param diff The Karma configuration diff object to check. * @returns True if there are any differences; false otherwise. */ function hasDifferences(diff) { return diff.added.size > 0 || diff.removed.size > 0 || diff.modified.size > 0; } async function compareKarmaConfigToDefault(projectConfigOrAnalysis, projectName, karmaConfigPath, needDevkitPlugin) { const projectAnalysis = typeof projectConfigOrAnalysis === 'string' ? (0, karma_config_analyzer_1.analyzeKarmaConfig)(projectConfigOrAnalysis) : projectConfigOrAnalysis; const defaultContent = await generateDefaultKarmaConfig(relativePathToWorkspaceRoot((0, node_path_1.dirname)(karmaConfigPath)), projectName, needDevkitPlugin); const defaultAnalysis = (0, karma_config_analyzer_1.analyzeKarmaConfig)(defaultContent); return compareKarmaConfigs(projectAnalysis, defaultAnalysis); } function relativePathToWorkspaceRoot(projectRoot) { if (!projectRoot) { return '.'; } return (0, node_path_1.relative)((0, node_path_1.join)('/', projectRoot), '/') || '.'; } const karmaConfigTemplateFallback = `// Karma configuration file, see link for more information // https://karma-runner.github.io/1.0/config/configuration-file.html module.exports = function (config) { config.set({ basePath: '', frameworks: ['jasmine'<% if (needDevkitPlugin) { %>, '@angular-devkit/build-angular'<% } %>], plugins: [ require('karma-jasmine'), require('karma-chrome-launcher'), require('karma-jasmine-html-reporter'), require('karma-coverage'),<% if (needDevkitPlugin) { %> require('@angular-devkit/build-angular/plugins/karma')<% } %> ], client: { jasmine: { // you can add configuration options for Jasmine here // the possible options are listed at https://jasmine.github.io/api/edge/Configuration.html // for example, you can disable the random execution with \`random: false\` // or set a specific seed with \`seed: 4321\` }, }, jasmineHtmlReporter: { suppressAll: true // removes the duplicated traces }, coverageReporter: { dir: require('path').join(__dirname, '<%= relativePathToWorkspaceRoot %>/coverage/<%= folderName %>'), subdir: '.', reporters: [ { type: 'html' }, { type: 'text-summary' } ] }, reporters: ['progress', 'kjhtml'], browsers: ['Chrome'], restartOnFileChange: true }); }; `; async function getKarmaConfigTemplate() { try { const templatePath = require.resolve('@schematics/angular/config/files/karma.conf.js.template'); return await (0, promises_1.readFile)(templatePath, 'utf-8'); } catch (e) { return karmaConfigTemplateFallback; } }