@@ -4,7 +4,7 @@ import { prerelease, SemVer } from 'semver';
4
4
import yargsParser from 'yargs-parser' ;
5
5
import { IS_BAZEL } from './bazel' ;
6
6
import { getGlobalVariable } from './env' ;
7
- import { readFile , replaceInFile , writeFile } from './fs' ;
7
+ import { readFile , replaceInFile , writeFile , writeMultipleFiles } from './fs' ;
8
8
import { gitCommit } from './git' ;
9
9
import { findFreePort } from './network' ;
10
10
import { installWorkspacePackages , PkgInfo } from './packages' ;
@@ -187,6 +187,45 @@ export function useCIDefaults(projectName = 'test-project'): Promise<void> {
187
187
} ) ;
188
188
}
189
189
190
+ const KARMA_CONF_DEFAULT = `
191
+ module.exports = function (config) {
192
+ config.set({
193
+ basePath: '',
194
+ frameworks: ['jasmine', '@angular-devkit/build-angular'],
195
+ plugins: [
196
+ require('karma-jasmine'),
197
+ require('karma-chrome-launcher'),
198
+ require('karma-jasmine-html-reporter'),
199
+ require('karma-coverage'),
200
+ require('@angular-devkit/build-angular/plugins/karma')
201
+ ],
202
+ client: {
203
+ jasmine: {},
204
+ clearContext: false // leave Jasmine Spec Runner output visible in browser
205
+ },
206
+ jasmineHtmlReporter: {
207
+ suppressAll: true // removes the duplicated traces
208
+ },
209
+ coverageReporter: {
210
+ dir: require('path').join(__dirname, './coverage/$PROJECT_NAME$'),
211
+ subdir: '.',
212
+ reporters: [
213
+ { type: 'html' },
214
+ { type: 'text-summary' }
215
+ ]
216
+ },
217
+ reporters: ['progress', 'kjhtml'],
218
+ port: 9876,
219
+ colors: true,
220
+ logLevel: config.LOG_INFO,
221
+ autoWatch: true,
222
+ browsers: ['Chrome'],
223
+ singleRun: false,
224
+ restartOnFileChange: true
225
+ });
226
+ };
227
+ ` ;
228
+
190
229
export async function useCIChrome ( projectName : string , projectDir = '' ) : Promise < void > {
191
230
const protractorConf = path . join ( projectDir , 'protractor.conf.js' ) ;
192
231
if ( fs . existsSync ( protractorConf ) ) {
@@ -207,80 +246,32 @@ export async function useCIChrome(projectName: string, projectDir = ''): Promise
207
246
}
208
247
209
248
const karmaConf = path . join ( projectDir , 'karma.conf.js' ) ;
210
- // Modify existing karma config
211
- if ( fs . existsSync ( karmaConf ) ) {
212
- await replaceInFile (
213
- karmaConf ,
214
- / b r o w s e r s : .* \] \s * , / ,
215
- `
216
- browsers: ['ChromeHeadlessNoSandbox'],
217
- customLaunchers: {
218
- ChromeHeadlessNoSandbox: {
219
- base: 'ChromeHeadless',
220
- flags: [
221
- '--no-sandbox',
222
- '--headless',
223
- '--disable-gpu',
224
- '--disable-dev-shm-usage',
225
- ],
226
- }
227
- },
228
- ` ,
229
- ) ;
230
- // Otherwise need to create new standalone config to ensure ChromeHeadlessNoSandbox is used
231
- } else {
232
- await writeFile (
233
- karmaConf ,
234
- `
235
- const path = require('path');
236
- module.exports = function(config) {
237
- config.set({
238
- basePath: '',
239
- frameworks: ['jasmine', '@angular-devkit/build-angular'],
240
- plugins: [
241
- require('karma-jasmine'),
242
- require('karma-chrome-launcher'),
243
- require('karma-jasmine-html-reporter'),
244
- require('karma-coverage'),
245
- require('@angular-devkit/build-angular/plugins/karma'),
246
- ],
247
- client: {
248
- clearContext: false, // leave Jasmine Spec Runner output visible in browser
249
- },
250
- jasmineHtmlReporter: {
251
- suppressAll: true // removes the duplicated traces
252
- },
253
- coverageReporter: {
254
- dir: path.join(__dirname, './coverage'),
255
- subdir: '.',
256
- reporters: [
257
- {type: 'lcov'},
258
- ],
259
- },
260
- reporters: ['progress', 'kjhtml'],
261
- port: 9876,
262
- colors: true,
263
- logLevel: config.LOG_INFO,
264
- autoWatch: true,
265
- browsers: ['ChromeHeadlessNoSandbox'],
266
- customLaunchers: {
267
- ChromeHeadlessNoSandbox: {
268
- base: 'ChromeHeadless',
269
- flags: [
270
- '--no-sandbox',
271
- '--headless',
272
- '--disable-gpu',
273
- '--disable-dev-shm-usage',
274
- ],
275
- }
276
- },
277
- singleRun: false,
278
- });
279
- };
280
- ` ,
281
- ) ;
249
+
250
+ // Create one with default config if it doesn't exist
251
+ if ( ! fs . existsSync ( karmaConf ) ) {
252
+ await writeFile ( karmaConf , KARMA_CONF_DEFAULT . replace ( '$PROJECT_NAME$' , projectName ) ) ;
282
253
}
283
254
255
+ // Update to use the headless sandboxed chrome
256
+ await replaceInFile (
257
+ karmaConf ,
258
+ / b r o w s e r s : .* \] \s * , / ,
259
+ `
260
+ browsers: ['ChromeHeadlessNoSandbox'],
261
+ customLaunchers: {
262
+ ChromeHeadlessNoSandbox: {
263
+ base: 'ChromeHeadless',
264
+ flags: [
265
+ '--no-sandbox',
266
+ '--headless',
267
+ '--disable-gpu',
268
+ '--disable-dev-shm-usage',
269
+ ],
270
+ }
271
+ },
272
+ ` ,
273
+ ) ;
274
+
284
275
return updateJsonFile ( 'angular.json' , ( workspaceJson ) => {
285
276
const project = workspaceJson . projects [ projectName ] ;
286
277
const appTargets = project . targets || project . architect ;
0 commit comments