[6a3a178] | 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 | */
|
---|
| 9 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
| 10 | exports.TargetDefinitionCollection = exports.ProjectDefinitionCollection = void 0;
|
---|
| 11 | class DefinitionCollection {
|
---|
| 12 | constructor(initial, _listener) {
|
---|
| 13 | this._listener = _listener;
|
---|
| 14 | this._map = new Map(initial && Object.entries(initial));
|
---|
| 15 | }
|
---|
| 16 | delete(key) {
|
---|
| 17 | const value = this._map.get(key);
|
---|
| 18 | const result = this._map.delete(key);
|
---|
| 19 | if (result && value !== undefined && this._listener) {
|
---|
| 20 | this._listener(key, 'remove', undefined, value, this);
|
---|
| 21 | }
|
---|
| 22 | return result;
|
---|
| 23 | }
|
---|
| 24 | set(key, value) {
|
---|
| 25 | const existing = this.get(key);
|
---|
| 26 | this._map.set(key, value);
|
---|
| 27 | if (this._listener) {
|
---|
| 28 | this._listener(key, existing !== undefined ? 'replace' : 'add', value, existing, this);
|
---|
| 29 | }
|
---|
| 30 | return this;
|
---|
| 31 | }
|
---|
| 32 | forEach(callbackfn, thisArg) {
|
---|
| 33 | this._map.forEach((value, key) => callbackfn(value, key, this), thisArg);
|
---|
| 34 | }
|
---|
| 35 | get(key) {
|
---|
| 36 | return this._map.get(key);
|
---|
| 37 | }
|
---|
| 38 | has(key) {
|
---|
| 39 | return this._map.has(key);
|
---|
| 40 | }
|
---|
| 41 | get size() {
|
---|
| 42 | return this._map.size;
|
---|
| 43 | }
|
---|
| 44 | [Symbol.iterator]() {
|
---|
| 45 | return this._map[Symbol.iterator]();
|
---|
| 46 | }
|
---|
| 47 | entries() {
|
---|
| 48 | return this._map.entries();
|
---|
| 49 | }
|
---|
| 50 | keys() {
|
---|
| 51 | return this._map.keys();
|
---|
| 52 | }
|
---|
| 53 | values() {
|
---|
| 54 | return this._map.values();
|
---|
| 55 | }
|
---|
| 56 | }
|
---|
| 57 | function isJsonValue(value) {
|
---|
| 58 | const visited = new Set();
|
---|
| 59 | switch (typeof value) {
|
---|
| 60 | case 'boolean':
|
---|
| 61 | case 'number':
|
---|
| 62 | case 'string':
|
---|
| 63 | return true;
|
---|
| 64 | case 'object':
|
---|
| 65 | if (value === null) {
|
---|
| 66 | return true;
|
---|
| 67 | }
|
---|
| 68 | visited.add(value);
|
---|
| 69 | for (const property of Object.values(value)) {
|
---|
| 70 | if (typeof value === 'object' && visited.has(property)) {
|
---|
| 71 | continue;
|
---|
| 72 | }
|
---|
| 73 | if (!isJsonValue(property)) {
|
---|
| 74 | return false;
|
---|
| 75 | }
|
---|
| 76 | }
|
---|
| 77 | return true;
|
---|
| 78 | default:
|
---|
| 79 | return false;
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
| 82 | class ProjectDefinitionCollection extends DefinitionCollection {
|
---|
| 83 | constructor(initial, listener) {
|
---|
| 84 | super(initial, listener);
|
---|
| 85 | }
|
---|
| 86 | add(definition) {
|
---|
| 87 | if (this.has(definition.name)) {
|
---|
| 88 | throw new Error('Project name already exists.');
|
---|
| 89 | }
|
---|
| 90 | this._validateName(definition.name);
|
---|
| 91 | const project = {
|
---|
| 92 | root: definition.root,
|
---|
| 93 | prefix: definition.prefix,
|
---|
| 94 | sourceRoot: definition.sourceRoot,
|
---|
| 95 | targets: new TargetDefinitionCollection(),
|
---|
| 96 | extensions: {},
|
---|
| 97 | };
|
---|
| 98 | if (definition.targets) {
|
---|
| 99 | for (const [name, target] of Object.entries(definition.targets)) {
|
---|
| 100 | if (target) {
|
---|
| 101 | project.targets.set(name, target);
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
| 104 | }
|
---|
| 105 | for (const [name, value] of Object.entries(definition)) {
|
---|
| 106 | switch (name) {
|
---|
| 107 | case 'name':
|
---|
| 108 | case 'root':
|
---|
| 109 | case 'sourceRoot':
|
---|
| 110 | case 'prefix':
|
---|
| 111 | case 'targets':
|
---|
| 112 | break;
|
---|
| 113 | default:
|
---|
| 114 | if (isJsonValue(value)) {
|
---|
| 115 | project.extensions[name] = value;
|
---|
| 116 | }
|
---|
| 117 | else {
|
---|
| 118 | throw new TypeError(`"${name}" must be a JSON value.`);
|
---|
| 119 | }
|
---|
| 120 | break;
|
---|
| 121 | }
|
---|
| 122 | }
|
---|
| 123 | super.set(definition.name, project);
|
---|
| 124 | return project;
|
---|
| 125 | }
|
---|
| 126 | set(name, value) {
|
---|
| 127 | this._validateName(name);
|
---|
| 128 | super.set(name, value);
|
---|
| 129 | return this;
|
---|
| 130 | }
|
---|
| 131 | _validateName(name) {
|
---|
| 132 | if (typeof name !== 'string' || !/^(?:@\w[\w\.-]*\/)?\w[\w\.-]*$/.test(name)) {
|
---|
| 133 | throw new Error('Project name must be a valid npm package name.');
|
---|
| 134 | }
|
---|
| 135 | }
|
---|
| 136 | }
|
---|
| 137 | exports.ProjectDefinitionCollection = ProjectDefinitionCollection;
|
---|
| 138 | class TargetDefinitionCollection extends DefinitionCollection {
|
---|
| 139 | constructor(initial, listener) {
|
---|
| 140 | super(initial, listener);
|
---|
| 141 | }
|
---|
| 142 | add(definition) {
|
---|
| 143 | if (this.has(definition.name)) {
|
---|
| 144 | throw new Error('Target name already exists.');
|
---|
| 145 | }
|
---|
| 146 | this._validateName(definition.name);
|
---|
| 147 | const target = {
|
---|
| 148 | builder: definition.builder,
|
---|
| 149 | options: definition.options,
|
---|
| 150 | configurations: definition.configurations,
|
---|
| 151 | defaultConfiguration: definition.defaultConfiguration,
|
---|
| 152 | };
|
---|
| 153 | super.set(definition.name, target);
|
---|
| 154 | return target;
|
---|
| 155 | }
|
---|
| 156 | set(name, value) {
|
---|
| 157 | this._validateName(name);
|
---|
| 158 | super.set(name, value);
|
---|
| 159 | return this;
|
---|
| 160 | }
|
---|
| 161 | _validateName(name) {
|
---|
| 162 | if (typeof name !== 'string') {
|
---|
| 163 | throw new TypeError('Target name must be a string.');
|
---|
| 164 | }
|
---|
| 165 | }
|
---|
| 166 | }
|
---|
| 167 | exports.TargetDefinitionCollection = TargetDefinitionCollection;
|
---|