@@ -12,23 +12,33 @@ import { isDeepStrictEqual } from 'node:util';
12
12
import { relativePathToWorkspaceRoot } from '../../utility/paths' ;
13
13
import { KarmaConfigAnalysis , KarmaConfigValue , analyzeKarmaConfig } from './karma-config-analyzer' ;
14
14
15
+ /**
16
+ * Represents the difference between two Karma configurations.
17
+ */
15
18
export interface KarmaConfigDiff {
19
+ /** A map of settings that were added in the project's configuration. */
16
20
added : Map < string , KarmaConfigValue > ;
21
+
22
+ /** A map of settings that were removed from the project's configuration. */
17
23
removed : Map < string , KarmaConfigValue > ;
24
+
25
+ /** A map of settings that were modified between the two configurations. */
18
26
modified : Map < string , { projectValue : KarmaConfigValue ; defaultValue : KarmaConfigValue } > ;
27
+
28
+ /** A boolean indicating if the comparison is reliable (i.e., no unsupported values were found). */
19
29
isReliable : boolean ;
20
30
}
21
31
22
32
/**
23
33
* 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.
26
36
* @param needDevkitPlugin A boolean indicating if the devkit plugin is needed.
27
37
* @returns The content of the default `karma.conf.js` file.
28
38
*/
29
39
export async function generateDefaultKarmaConfig (
30
40
relativePathToWorkspaceRoot : string ,
31
- folderName : string ,
41
+ projectName : string ,
32
42
needDevkitPlugin : boolean ,
33
43
) : Promise < string > {
34
44
const templatePath = path . join ( __dirname , '../../config/files/karma.conf.js.template' ) ;
@@ -40,7 +50,7 @@ export async function generateDefaultKarmaConfig(
40
50
/ < % = 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,
41
51
path . normalize ( relativePathToWorkspaceRoot ) . replace ( / \\ / g, '/' ) ,
42
52
)
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 ) ;
44
54
45
55
const devkitPluginRegex = / < % i f \( n e e d D e v k i t P l u g i n \) { % > ( .* ?) < % } % > / gs;
46
56
const replacement = needDevkitPlugin ? '$1' : '' ;
@@ -52,8 +62,8 @@ export async function generateDefaultKarmaConfig(
52
62
/**
53
63
* Compares two Karma configuration analyses and returns the difference.
54
64
* @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 .
57
67
*/
58
68
export function compareKarmaConfigs (
59
69
projectAnalysis : KarmaConfigAnalysis ,
@@ -93,8 +103,8 @@ export function compareKarmaConfigs(
93
103
94
104
/**
95
105
* 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.
98
108
*/
99
109
export function hasDifferences ( diff : KarmaConfigDiff ) : boolean {
100
110
return diff . added . size > 0 || diff . removed . size > 0 || diff . modified . size > 0 ;
@@ -103,41 +113,46 @@ export function hasDifferences(diff: KarmaConfigDiff): boolean {
103
113
/**
104
114
* Compares a project's Karma configuration with the default configuration.
105
115
* @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.
108
119
* @returns A diff object representing the changes.
109
120
*/
110
121
export async function compareKarmaConfigToDefault (
111
122
projectConfigContent : string ,
112
123
projectRoot : string ,
113
124
needDevkitPlugin : boolean ,
125
+ karmaConfigPath ?: string ,
114
126
) : Promise < KarmaConfigDiff > ;
115
127
116
128
/**
117
129
* Compares a project's Karma configuration with the default configuration.
118
130
* @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.
121
134
* @returns A diff object representing the changes.
122
135
*/
123
136
export async function compareKarmaConfigToDefault (
124
137
projectAnalysis : KarmaConfigAnalysis ,
125
138
projectRoot : string ,
126
139
needDevkitPlugin : boolean ,
140
+ karmaConfigPath ?: string ,
127
141
) : Promise < KarmaConfigDiff > ;
128
142
129
143
export async function compareKarmaConfigToDefault (
130
144
projectConfigOrAnalysis : string | KarmaConfigAnalysis ,
131
145
projectRoot : string ,
132
146
needDevkitPlugin : boolean ,
147
+ karmaConfigPath ?: string ,
133
148
) : Promise < KarmaConfigDiff > {
134
149
const projectAnalysis =
135
150
typeof projectConfigOrAnalysis === 'string'
136
151
? analyzeKarmaConfig ( projectConfigOrAnalysis )
137
152
: projectConfigOrAnalysis ;
138
153
139
154
const defaultContent = await generateDefaultKarmaConfig (
140
- relativePathToWorkspaceRoot ( projectRoot ) ,
155
+ relativePathToWorkspaceRoot ( karmaConfigPath ? path . dirname ( karmaConfigPath ) : projectRoot ) ,
141
156
path . basename ( projectRoot ) ,
142
157
needDevkitPlugin ,
143
158
) ;
0 commit comments