[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.ConfigCommand = void 0;
|
---|
| 11 | const core_1 = require("@angular-devkit/core");
|
---|
| 12 | const uuid_1 = require("uuid");
|
---|
| 13 | const command_1 = require("../models/command");
|
---|
| 14 | const interface_1 = require("../models/interface");
|
---|
| 15 | const config_1 = require("../utilities/config");
|
---|
| 16 | const json_file_1 = require("../utilities/json-file");
|
---|
| 17 | const validCliPaths = new Map([
|
---|
| 18 | ['cli.warnings.versionMismatch', undefined],
|
---|
| 19 | ['cli.defaultCollection', undefined],
|
---|
| 20 | ['cli.packageManager', undefined],
|
---|
| 21 | ['cli.analytics', undefined],
|
---|
| 22 | ['cli.analyticsSharing.tracking', undefined],
|
---|
[e29cc2e] | 23 | ['cli.analyticsSharing.uuid', (v) => (v === '' ? uuid_1.v4() : `${v}`)],
|
---|
[6a3a178] | 24 | ]);
|
---|
| 25 | /**
|
---|
| 26 | * Splits a JSON path string into fragments. Fragments can be used to get the value referenced
|
---|
| 27 | * by the path. For example, a path of "a[3].foo.bar[2]" would give you a fragment array of
|
---|
| 28 | * ["a", 3, "foo", "bar", 2].
|
---|
| 29 | * @param path The JSON string to parse.
|
---|
| 30 | * @returns {(string|number)[]} The fragments for the string.
|
---|
| 31 | * @private
|
---|
| 32 | */
|
---|
| 33 | function parseJsonPath(path) {
|
---|
| 34 | const fragments = (path || '').split(/\./g);
|
---|
| 35 | const result = [];
|
---|
| 36 | while (fragments.length > 0) {
|
---|
| 37 | const fragment = fragments.shift();
|
---|
| 38 | if (fragment == undefined) {
|
---|
| 39 | break;
|
---|
| 40 | }
|
---|
| 41 | const match = fragment.match(/([^\[]+)((\[.*\])*)/);
|
---|
| 42 | if (!match) {
|
---|
| 43 | throw new Error('Invalid JSON path.');
|
---|
| 44 | }
|
---|
| 45 | result.push(match[1]);
|
---|
| 46 | if (match[2]) {
|
---|
| 47 | const indices = match[2]
|
---|
| 48 | .slice(1, -1)
|
---|
| 49 | .split('][')
|
---|
| 50 | .map((x) => (/^\d$/.test(x) ? +x : x.replace(/\"|\'/g, '')));
|
---|
| 51 | result.push(...indices);
|
---|
| 52 | }
|
---|
| 53 | }
|
---|
| 54 | return result.filter((fragment) => fragment != null);
|
---|
| 55 | }
|
---|
| 56 | function normalizeValue(value) {
|
---|
| 57 | const valueString = `${value}`.trim();
|
---|
| 58 | switch (valueString) {
|
---|
| 59 | case 'true':
|
---|
| 60 | return true;
|
---|
| 61 | case 'false':
|
---|
| 62 | return false;
|
---|
| 63 | case 'null':
|
---|
| 64 | return null;
|
---|
| 65 | case 'undefined':
|
---|
| 66 | return undefined;
|
---|
| 67 | }
|
---|
| 68 | if (isFinite(+valueString)) {
|
---|
| 69 | return +valueString;
|
---|
| 70 | }
|
---|
[e29cc2e] | 71 | try {
|
---|
| 72 | // We use `JSON.parse` instead of `parseJson` because the latter will parse UUIDs
|
---|
| 73 | // and convert them into a numberic entities.
|
---|
| 74 | // Example: 73b61974-182c-48e4-b4c6-30ddf08c5c98 -> 73.
|
---|
| 75 | // These values should never contain comments, therefore using `JSON.parse` is safe.
|
---|
| 76 | return JSON.parse(valueString);
|
---|
| 77 | }
|
---|
| 78 | catch {
|
---|
| 79 | return value;
|
---|
| 80 | }
|
---|
[6a3a178] | 81 | }
|
---|
| 82 | class ConfigCommand extends command_1.Command {
|
---|
| 83 | async run(options) {
|
---|
| 84 | const level = options.global ? 'global' : 'local';
|
---|
| 85 | if (!options.global) {
|
---|
| 86 | await this.validateScope(interface_1.CommandScope.InProject);
|
---|
| 87 | }
|
---|
| 88 | let [config] = config_1.getWorkspaceRaw(level);
|
---|
| 89 | if (options.global && !config) {
|
---|
| 90 | try {
|
---|
| 91 | if (config_1.migrateLegacyGlobalConfig()) {
|
---|
| 92 | config = config_1.getWorkspaceRaw(level)[0];
|
---|
| 93 | this.logger.info(core_1.tags.oneLine `
|
---|
| 94 | We found a global configuration that was used in Angular CLI 1.
|
---|
| 95 | It has been automatically migrated.`);
|
---|
| 96 | }
|
---|
| 97 | }
|
---|
| 98 | catch { }
|
---|
| 99 | }
|
---|
| 100 | if (options.value == undefined) {
|
---|
| 101 | if (!config) {
|
---|
| 102 | this.logger.error('No config found.');
|
---|
| 103 | return 1;
|
---|
| 104 | }
|
---|
| 105 | return this.get(config, options);
|
---|
| 106 | }
|
---|
| 107 | else {
|
---|
| 108 | return this.set(options);
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
| 111 | get(jsonFile, options) {
|
---|
| 112 | let value;
|
---|
| 113 | if (options.jsonPath) {
|
---|
| 114 | value = jsonFile.get(parseJsonPath(options.jsonPath));
|
---|
| 115 | }
|
---|
| 116 | else {
|
---|
| 117 | value = jsonFile.content;
|
---|
| 118 | }
|
---|
| 119 | if (value === undefined) {
|
---|
| 120 | this.logger.error('Value cannot be found.');
|
---|
| 121 | return 1;
|
---|
| 122 | }
|
---|
| 123 | else if (typeof value === 'string') {
|
---|
| 124 | this.logger.info(value);
|
---|
| 125 | }
|
---|
| 126 | else {
|
---|
| 127 | this.logger.info(JSON.stringify(value, null, 2));
|
---|
| 128 | }
|
---|
| 129 | return 0;
|
---|
| 130 | }
|
---|
| 131 | async set(options) {
|
---|
| 132 | var _a, _b, _c;
|
---|
| 133 | if (!((_a = options.jsonPath) === null || _a === void 0 ? void 0 : _a.trim())) {
|
---|
| 134 | throw new Error('Invalid Path.');
|
---|
| 135 | }
|
---|
| 136 | if (options.global &&
|
---|
| 137 | !options.jsonPath.startsWith('schematics.') &&
|
---|
| 138 | !validCliPaths.has(options.jsonPath)) {
|
---|
| 139 | throw new Error('Invalid Path.');
|
---|
| 140 | }
|
---|
| 141 | const [config, configPath] = config_1.getWorkspaceRaw(options.global ? 'global' : 'local');
|
---|
| 142 | if (!config || !configPath) {
|
---|
| 143 | this.logger.error('Confguration file cannot be found.');
|
---|
| 144 | return 1;
|
---|
| 145 | }
|
---|
| 146 | const jsonPath = parseJsonPath(options.jsonPath);
|
---|
| 147 | const value = (_c = (_b = validCliPaths.get(options.jsonPath)) === null || _b === void 0 ? void 0 : _b(options.value)) !== null && _c !== void 0 ? _c : options.value;
|
---|
| 148 | const modified = config.modify(jsonPath, normalizeValue(value));
|
---|
| 149 | if (!modified) {
|
---|
| 150 | this.logger.error('Value cannot be found.');
|
---|
| 151 | return 1;
|
---|
| 152 | }
|
---|
| 153 | try {
|
---|
| 154 | await config_1.validateWorkspace(json_file_1.parseJson(config.content));
|
---|
| 155 | }
|
---|
| 156 | catch (error) {
|
---|
| 157 | this.logger.fatal(error.message);
|
---|
| 158 | return 1;
|
---|
| 159 | }
|
---|
| 160 | config.save();
|
---|
| 161 | return 0;
|
---|
| 162 | }
|
---|
| 163 | }
|
---|
| 164 | exports.ConfigCommand = ConfigCommand;
|
---|