-
Notifications
You must be signed in to change notification settings - Fork 682
Expand file tree
/
Copy pathset.ts
More file actions
92 lines (80 loc) · 3.79 KB
/
set.ts
File metadata and controls
92 lines (80 loc) · 3.79 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
import { MetadataGroup, validators } from '@ionic/cli-framework';
import { prettyPath } from '@ionic/utils-terminal';
import * as path from 'path';
import { PROJECT_FILE } from '../../constants';
import { CommandLineInputs, CommandLineOptions, CommandMetadata } from '../../definitions';
import { input, strong, weak } from '../../lib/color';
import { DEFAULT_CONFIG_DIRECTORY } from '../../lib/config';
import { FatalException } from '../../lib/errors';
import { BaseConfigCommand, getConfigValue, setConfigValue } from './base';
export class ConfigSetCommand extends BaseConfigCommand {
async getMetadata(): Promise<CommandMetadata> {
const projectFile = this.project ? prettyPath(this.project.filePath) : PROJECT_FILE;
return {
name: 'set',
type: 'global',
summary: 'Set config values',
description: `
This command writes configuration values to the project's ${strong(prettyPath(projectFile))} file. It can also operate on the global CLI configuration (${strong(path.join(DEFAULT_CONFIG_DIRECTORY, 'config.json'))}) using the ${input('--global')} option.
For nested properties, separate nest levels with dots. For example, the property name ${input('integrations.cordova')} will look in the ${strong('integrations')} object for the ${strong('cordova')} property.
For multi-app projects, this command is scoped to the current project by default. To operate at the root of the project configuration file instead, use the ${input('--root')} option.
This command will attempt to coerce ${input('value')} into a suitable JSON type. If it is JSON-parsable, such as ${input('123')}, ${input('true')}, ${input('[]')}, etc., then it takes the parsed result. Otherwise, the value is interpreted as a string. For stricter input, use ${input('--json')}, which will error with non-JSON values.
By default, if ${input('property')} exists and is an object or an array, the value is not overwritten. To disable this check and always overwrite the property, use ${input('--force')}.
`,
inputs: [
{
name: 'property',
summary: 'The property name you wish to set',
validators: [validators.required],
},
{
name: 'value',
summary: 'The new value of the given property',
validators: [validators.required],
},
],
options: [
{
name: 'global',
summary: 'Use global CLI config',
type: Boolean,
aliases: ['g'],
},
{
name: 'json',
summary: `Always interpret ${input('value')} as JSON`,
type: Boolean,
groups: [MetadataGroup.ADVANCED],
},
{
name: 'force',
summary: 'Always overwrite existing values',
type: Boolean,
groups: [MetadataGroup.ADVANCED],
},
{
name: 'root',
summary: `Operate on root of ${strong(prettyPath(projectFile))}`,
type: Boolean,
hint: weak('[multi-app]'),
groups: [MetadataGroup.ADVANCED],
},
],
exampleCommands: ['name newAppName', 'name "\\"newAppName\\"" --json', '-g interactive false'],
};
}
async run(inputs: CommandLineInputs, options: CommandLineOptions): Promise<void> {
const ctx = this.generateContext(inputs, options);
const { property } = ctx;
if (typeof property === 'undefined') {
throw new FatalException(`Cannot set config to ${input(ctx.value)} without a property.`);
}
const originalValue = getConfigValue(ctx);
setConfigValue({ ...ctx, property, originalValue });
if (ctx.value !== originalValue) {
this.env.log.ok(`${input(property)} set to ${input(JSON.stringify(ctx.value))}!`);
} else {
this.env.log.info(`${input(property)} is already set to ${input(JSON.stringify(ctx.value))}.`);
}
}
}