Repository navigation
Expand file tree
/
Copy pathcli.ts
More file actions
191 lines (159 loc) · 5.37 KB
/
Copy pathcli.ts
File metadata and controls
191 lines (159 loc) · 5.37 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/**
* @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.dev/license
*/
import { JsonValue } from '@angular-devkit/core';
import { randomUUID } from 'node:crypto';
import { Argv } from 'yargs';
import {
CommandModule,
CommandModuleError,
CommandModuleImplementation,
Options,
} from '../../command-builder/command-module';
import { getWorkspaceRaw, validateWorkspace } from '../../utilities/config';
import { JSONFile, parseJson } from '../../utilities/json-file';
import longDescription from './long-description.md';
interface ConfigCommandArgs {
'json-path'?: string;
value?: string;
global?: boolean;
}
export default class ConfigCommandModule
extends CommandModule<ConfigCommandArgs>
implements CommandModuleImplementation<ConfigCommandArgs>
{
command = 'config [json-path] [value]';
describe =
'Retrieves or sets Angular configuration values in the angular.json file for the workspace.';
override longDescription = longDescription;
builder(localYargs: Argv): Argv<ConfigCommandArgs> {
return localYargs
.positional('json-path', {
description:
`The configuration key to set or query, in JSON path format. ` +
`For example: "a[3].foo.bar[2]". If no new value is provided, returns the current value of this key.`,
type: 'string',
})
.positional('value', {
description: 'If provided, a new value for the given configuration key.',
type: 'string',
})
.option('global', {
description: `Access the global configuration in the caller's home directory.`,
alias: ['g'],
type: 'boolean',
default: false,
})
.strict();
}
async run(options: Options<ConfigCommandArgs>): Promise<number | void> {
const level = options.global ? 'global' : 'local';
const [config] = await getWorkspaceRaw(level);
if (options.value == undefined) {
if (!config) {
this.context.logger.error('No config found.');
return 1;
}
return this.get(config, options);
} else {
return this.set(options);
}
}
private get(jsonFile: JSONFile, options: Options<ConfigCommandArgs>): number {
const { logger } = this.context;
const value = options.jsonPath
? jsonFile.get(parseJsonPath(options.jsonPath))
: jsonFile.content;
if (value === undefined) {
logger.error('Value cannot be found.');
return 1;
} else if (typeof value === 'string') {
logger.info(value);
} else {
logger.info(JSON.stringify(value, null, 2));
}
return 0;
}
private async set(options: Options<ConfigCommandArgs>): Promise<number | void> {
if (!options.jsonPath?.trim()) {
throw new CommandModuleError('Invalid Path.');
}
const [config, configPath] = await getWorkspaceRaw(options.global ? 'global' : 'local');
const { logger } = this.context;
if (!config || !configPath) {
throw new CommandModuleError('Configuration file cannot be found.');
}
const normalizeUUIDValue = (v: string | undefined) => (v === '' ? randomUUID() : `${v}`);
const value =
options.jsonPath === 'cli.analyticsSharing.uuid'
? normalizeUUIDValue(options.value)
: options.value;
const modified = config.modify(parseJsonPath(options.jsonPath), normalizeValue(value));
if (!modified) {
logger.error('Value cannot be found.');
return 1;
}
await validateWorkspace(parseJson(config.content), options.global ?? false);
config.save();
return 0;
}
}
/**
* 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.
*/
export function parseJsonPath(path: string): (string | number)[] {
const fragments = (path || '').split(/\./g);
const result: (string | number)[] = [];
while (fragments.length > 0) {
const fragment = fragments.shift();
if (fragment == undefined) {
break;
}
const match = fragment.match(/([^[]+)((\[.*\])*)/);
if (!match) {
throw new CommandModuleError('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: string | undefined | boolean | number): JsonValue | undefined {
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) as JsonValue;
} catch {
return value;
}
}