|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import { |
| 10 | + Rule, |
| 11 | + SchematicsException, |
| 12 | + apply, |
| 13 | + applyTemplates, |
| 14 | + filter, |
| 15 | + mergeWith, |
| 16 | + move, |
| 17 | + strings, |
| 18 | + url, |
| 19 | +} from '@angular-devkit/schematics'; |
| 20 | +import { AngularBuilder, readWorkspace, updateWorkspace } from '@schematics/angular/utility'; |
| 21 | +import { posix as path } from 'path'; |
| 22 | +import { relativePathToWorkspaceRoot } from '../utility/paths'; |
| 23 | +import { Schema as ConfigOptions, Type as ConfigType } from './schema'; |
| 24 | + |
| 25 | +export default function (options: ConfigOptions): Rule { |
| 26 | + switch (options.type) { |
| 27 | + case ConfigType.Karma: |
| 28 | + return addKarmaConfig(options); |
| 29 | + case ConfigType.Browserslist: |
| 30 | + return addBrowserslistConfig(options); |
| 31 | + default: |
| 32 | + throw new SchematicsException(`"${options.type}" is an unknown configuration file type.`); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +function addBrowserslistConfig(options: ConfigOptions): Rule { |
| 37 | + return async (host) => { |
| 38 | + const workspace = await readWorkspace(host); |
| 39 | + const project = workspace.projects.get(options.project); |
| 40 | + if (!project) { |
| 41 | + throw new SchematicsException(`Project name "${options.project}" doesn't not exist.`); |
| 42 | + } |
| 43 | + |
| 44 | + return mergeWith( |
| 45 | + apply(url('./files'), [ |
| 46 | + filter((p) => p.endsWith('.browserslistrc.template')), |
| 47 | + applyTemplates({}), |
| 48 | + move(project.root), |
| 49 | + ]), |
| 50 | + ); |
| 51 | + }; |
| 52 | +} |
| 53 | + |
| 54 | +function addKarmaConfig(options: ConfigOptions): Rule { |
| 55 | + return updateWorkspace((workspace) => { |
| 56 | + const project = workspace.projects.get(options.project); |
| 57 | + if (!project) { |
| 58 | + throw new SchematicsException(`Project name "${options.project}" doesn't not exist.`); |
| 59 | + } |
| 60 | + |
| 61 | + const testTarget = project.targets.get('test'); |
| 62 | + if (!testTarget) { |
| 63 | + throw new SchematicsException( |
| 64 | + `No "test" target found for project "${options.project}".` + |
| 65 | + ' A "test" target is required to generate a karma configuration.', |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + if (testTarget.builder !== AngularBuilder.Karma) { |
| 70 | + throw new SchematicsException( |
| 71 | + `Cannot add a karma configuration as builder for "test" target in project does not use "${AngularBuilder.Karma}".`, |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + testTarget.options ??= {}; |
| 76 | + testTarget.options.karmaConfig = path.join(project.root, 'karma.conf.js'); |
| 77 | + |
| 78 | + // If scoped project (i.e. "@foo/bar"), convert dir to "foo/bar". |
| 79 | + let folderName = options.project.startsWith('@') ? options.project.slice(1) : options.project; |
| 80 | + if (/[A-Z]/.test(folderName)) { |
| 81 | + folderName = strings.dasherize(folderName); |
| 82 | + } |
| 83 | + |
| 84 | + return mergeWith( |
| 85 | + apply(url('./files'), [ |
| 86 | + filter((p) => p.endsWith('karma.conf.js.template')), |
| 87 | + applyTemplates({ |
| 88 | + relativePathToWorkspaceRoot: relativePathToWorkspaceRoot(project.root), |
| 89 | + folderName, |
| 90 | + }), |
| 91 | + move(project.root), |
| 92 | + ]), |
| 93 | + ); |
| 94 | + }); |
| 95 | +} |
0 commit comments