@@ -12,23 +12,33 @@ import { isDeepStrictEqual } from 'node:util';
1212import { relativePathToWorkspaceRoot } from '../../utility/paths' ;
1313import { KarmaConfigAnalysis , KarmaConfigValue , analyzeKarmaConfig } from './karma-config-analyzer' ;
1414
15+ /**
16+ * Represents the difference between two Karma configurations.
17+ */
1518export interface KarmaConfigDiff {
19+ /** A map of settings that were added in the project's configuration. */
1620 added : Map < string , KarmaConfigValue > ;
21+
22+ /** A map of settings that were removed from the project's configuration. */
1723 removed : Map < string , KarmaConfigValue > ;
24+
25+ /** A map of settings that were modified between the two configurations. */
1826 modified : Map < string , { projectValue : KarmaConfigValue ; defaultValue : KarmaConfigValue } > ;
27+
28+ /** A boolean indicating if the comparison is reliable (i.e., no unsupported values were found). */
1929 isReliable : boolean ;
2030}
2131
2232/**
2333 * Generates the default Karma configuration file content as a string.
24- * @param relativePathToWorkspaceRoot The relative path from the project root to the workspace root.
25- * @param folderName The name of the project folder .
34+ * @param relativePathToWorkspaceRoot The relative path from the Karma config file to the workspace root.
35+ * @param projectName The name of the project.
2636 * @param needDevkitPlugin A boolean indicating if the devkit plugin is needed.
2737 * @returns The content of the default `karma.conf.js` file.
2838 */
2939export async function generateDefaultKarmaConfig (
3040 relativePathToWorkspaceRoot : string ,
31- folderName : string ,
41+ projectName : string ,
3242 needDevkitPlugin : boolean ,
3343) : Promise < string > {
3444 const templatePath = path . join ( __dirname , '../../config/files/karma.conf.js.template' ) ;
@@ -40,7 +50,7 @@ export async function generateDefaultKarmaConfig(
4050 / < % = r e l a t i v e P a t h T o W o r k s p a c e R o o t % > / g,
4151 path . normalize ( relativePathToWorkspaceRoot ) . replace ( / \\ / g, '/' ) ,
4252 )
43- . replace ( / < % = f o l d e r N a m e % > / g, folderName ) ;
53+ . replace ( / < % = f o l d e r N a m e % > / g, projectName ) ;
4454
4555 const devkitPluginRegex = / < % i f \( n e e d D e v k i t P l u g i n \) { % > ( .* ?) < % } % > / gs;
4656 const replacement = needDevkitPlugin ? '$1' : '' ;
@@ -52,8 +62,8 @@ export async function generateDefaultKarmaConfig(
5262/**
5363 * Compares two Karma configuration analyses and returns the difference.
5464 * @param projectAnalysis The analysis of the project's configuration.
55- * @param defaultAnalysis The analysis of the default configuration.
56- * @returns A diff object representing the changes.
65+ * @param defaultAnalysis The analysis of the default configuration to compare against .
66+ * @returns A diff object representing the changes between the two configurations .
5767 */
5868export function compareKarmaConfigs (
5969 projectAnalysis : KarmaConfigAnalysis ,
@@ -93,8 +103,8 @@ export function compareKarmaConfigs(
93103
94104/**
95105 * Checks if there are any differences in the provided Karma configuration diff.
96- * @param diff The Karma configuration diff object.
97- * @returns True if there are any differences, false otherwise.
106+ * @param diff The Karma configuration diff object to check .
107+ * @returns True if there are any differences; false otherwise.
98108 */
99109export function hasDifferences ( diff : KarmaConfigDiff ) : boolean {
100110 return diff . added . size > 0 || diff . removed . size > 0 || diff . modified . size > 0 ;
@@ -103,41 +113,46 @@ export function hasDifferences(diff: KarmaConfigDiff): boolean {
103113/**
104114 * Compares a project's Karma configuration with the default configuration.
105115 * @param projectConfigContent The content of the project's `karma.conf.js` file.
106- * @param projectRoot The root of the project's project.
107- * @param needDevkitPlugin A boolean indicating if the devkit plugin is needed.
116+ * @param projectRoot The root directory of the project.
117+ * @param needDevkitPlugin A boolean indicating if the devkit plugin is needed for the default config.
118+ * @param karmaConfigPath The path to the Karma configuration file, used to resolve relative paths.
108119 * @returns A diff object representing the changes.
109120 */
110121export async function compareKarmaConfigToDefault (
111122 projectConfigContent : string ,
112123 projectRoot : string ,
113124 needDevkitPlugin : boolean ,
125+ karmaConfigPath ?: string ,
114126) : Promise < KarmaConfigDiff > ;
115127
116128/**
117129 * Compares a project's Karma configuration with the default configuration.
118130 * @param projectAnalysis The analysis of the project's configuration.
119- * @param projectRoot The root of the project's project.
120- * @param needDevkitPlugin A boolean indicating if the devkit plugin is needed.
131+ * @param projectRoot The root directory of the project.
132+ * @param needDevkitPlugin A boolean indicating if the devkit plugin is needed for the default config.
133+ * @param karmaConfigPath The path to the Karma configuration file, used to resolve relative paths.
121134 * @returns A diff object representing the changes.
122135 */
123136export async function compareKarmaConfigToDefault (
124137 projectAnalysis : KarmaConfigAnalysis ,
125138 projectRoot : string ,
126139 needDevkitPlugin : boolean ,
140+ karmaConfigPath ?: string ,
127141) : Promise < KarmaConfigDiff > ;
128142
129143export async function compareKarmaConfigToDefault (
130144 projectConfigOrAnalysis : string | KarmaConfigAnalysis ,
131145 projectRoot : string ,
132146 needDevkitPlugin : boolean ,
147+ karmaConfigPath ?: string ,
133148) : Promise < KarmaConfigDiff > {
134149 const projectAnalysis =
135150 typeof projectConfigOrAnalysis === 'string'
136151 ? analyzeKarmaConfig ( projectConfigOrAnalysis )
137152 : projectConfigOrAnalysis ;
138153
139154 const defaultContent = await generateDefaultKarmaConfig (
140- relativePathToWorkspaceRoot ( projectRoot ) ,
155+ relativePathToWorkspaceRoot ( karmaConfigPath ? path . dirname ( karmaConfigPath ) : projectRoot ) ,
141156 path . basename ( projectRoot ) ,
142157 needDevkitPlugin ,
143158 ) ;
0 commit comments