generated from salesforcecli/plugin-template-sf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
set.ts
58 lines (48 loc) · 2.17 KB
/
set.ts
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
/*
* Copyright (c) 2022, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { loglevel, parseVarArgs } from '@salesforce/sf-plugins-core';
import { StateAggregator, Messages } from '@salesforce/core';
import { AliasCommand, AliasResults, aliasErrorHandler } from '../../alias.js';
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-settings', 'alias.set');
export default class AliasSet extends AliasCommand<AliasResults> {
public static summary = messages.getMessage('summary');
public static description = messages.getMessage('description');
public static examples = messages.getMessages('examples');
public static readonly strict = false; // This allows varargs
public static readonly aliases = ['force:alias:set'];
public static readonly deprecateAliases = true;
public static readonly flags = { loglevel };
public async run(): Promise<AliasResults> {
await this.parse(AliasSet);
const stateAggregator = await StateAggregator.getInstance();
const { args, argv } = await this.parse(AliasSet);
if (!argv.length) throw messages.createError('error.ArgumentsRequired');
const parsed = parseVarArgs(args, argv as string[]);
const results = await Promise.all(
Object.entries(parsed).map(async ([alias, value]) => {
try {
if (alias.includes(' ')) {
this.warn(messages.getMessage('warning.spaceAlias', [alias, alias]));
}
// to support plugin-settings in sfdx, which allowed setting an alias to undefined, when that happens we'll unset the alias
// which is what the user wants
if (!value) {
await stateAggregator.aliases.unsetAndSave(alias);
} else {
await stateAggregator.aliases.setAndSave(alias, value);
}
return { alias, success: true, value };
} catch (err) {
return aliasErrorHandler(err, alias, value);
}
})
);
this.output('Alias Set', results);
return results;
}
}