generated from salesforcecli/plugin-template-sf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunset.ts
79 lines (68 loc) · 2.66 KB
/
unset.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
* 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 { Flags, loglevel } 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.unset');
export default class AliasUnset 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:unset'];
public static readonly deprecateAliases = true;
public static flags = {
loglevel,
all: Flags.boolean({
summary: messages.getMessage('flags.all.summary'),
char: 'a',
}),
'no-prompt': Flags.boolean({
summary: messages.getMessage('flags.no-prompt.summary'),
char: 'p',
}),
};
public async run(): Promise<AliasResults> {
const { flags, argv } = await this.parse(AliasUnset);
const stateAggregator = await StateAggregator.getInstance();
const aliases = stateAggregator.aliases.getAll();
const toRemove = flags.all ? Object.keys(aliases) : (argv as string[]);
if (toRemove.length === 0) {
if (flags.all) {
this.warn(messages.getMessage('warning.NoAliasesSet'));
return [];
}
// No arg was passed, we don't know what to unset.
throw messages.createError('error.NameRequired');
}
// Confirm the users wants to remove all aliases. Supports --no-prompt.
if (
flags.all &&
!flags['no-prompt'] &&
!(await this.confirm({ message: messages.getMessage('prompt.RemoveAllAliases') }))
) {
return [];
}
const results = await Promise.all(
toRemove
// We will log the value in the output in case an alias was unset by mistake.
.map((alias) => ({ alias, value: aliases[alias] }))
.map(async ({ alias, value }) => {
try {
// safe to parallelize because of file locking
await stateAggregator.aliases.unsetAndSave(alias);
return { alias, value, success: true };
} catch (err) {
return aliasErrorHandler(err, alias, value);
}
})
);
this.output('Alias Unset', results);
return results;
}
}