UNPKG

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