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