-
Notifications
You must be signed in to change notification settings - Fork 4
/
orgFlags.ts
227 lines (216 loc) · 6.84 KB
/
orgFlags.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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
* Copyright (c) 2020, 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 } from '@oclif/core';
import { ConfigAggregator, Messages, Org, OrgConfigProperties } from '@salesforce/core';
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/sf-plugins-core', 'messages');
export async function maybeGetOrg(input: string): Promise<Org>;
export async function maybeGetOrg(input: undefined): Promise<undefined>;
export async function maybeGetOrg(input?: string | undefined): Promise<Org | undefined>;
export async function maybeGetOrg(input?: string | undefined): Promise<Org | undefined> {
try {
return await Org.create({ aliasOrUsername: input });
} catch (e) {
if (!input) {
return undefined;
} else {
throw e;
}
}
}
export const maybeGetHub = async (input?: string): Promise<Org | undefined> => {
let org: Org | undefined;
// user provided input, verify the org exits
if (input) {
org = await getOrgOrThrow(input);
} else {
// no input, check config for a default
const aliasOrUsername = await getDefaultHub(false);
// if there is a default, verify the org exists
if (aliasOrUsername) {
org = await getOrgOrThrow(aliasOrUsername);
}
}
if (org) {
return ensureDevHub(org, org.getUsername());
} else {
return undefined;
}
};
export const getOrgOrThrow = async (input?: string): Promise<Org> => {
const org = await maybeGetOrg(input);
if (!org) {
throw messages.createError('errors.NoDefaultEnv');
}
return org;
};
const ensureDevHub = async (org: Org, aliasOrUsername?: string): Promise<Org> => {
if (await org.determineIfDevHubOrg()) {
return org;
}
throw messages.createError('errors.NotADevHub', [aliasOrUsername ?? org.getUsername()]);
};
async function getDefaultHub(throwIfNotFound: false): Promise<string | undefined>;
async function getDefaultHub(throwIfNotFound: true): Promise<string>;
async function getDefaultHub(throwIfNotFound: boolean): Promise<string | undefined> {
// check config for a default
const config = await ConfigAggregator.create();
const aliasOrUsername = config.getInfo(OrgConfigProperties.TARGET_DEV_HUB)?.value as string;
if (throwIfNotFound && !aliasOrUsername) {
throw messages.createError('errors.NoDefaultDevHub');
}
return aliasOrUsername;
}
export const getHubOrThrow = async (aliasOrUsername?: string): Promise<Org> => {
const resolved = aliasOrUsername ?? (await getDefaultHub(true));
const org = await Org.create({ aliasOrUsername: resolved, isDevHub: true });
return ensureDevHub(org, resolved);
};
/**
* An optional org specified by username or alias
* Will default to the default org if one is not specified.
* Will not throw if the specified org and default do not exist
*
* @example
*
* ```
* import { Flags } from '@salesforce/sf-plugins-core';
* public static flags = {
* // setting length or prefix
* 'target-org': Flags.optionalOrg(),
* // adding properties
* 'flag2': Flags.optionalOrg({
* required: true,
* description: 'flag2 description',
* }),
* }
* ```
*/
export const optionalOrgFlag = Flags.custom({
char: 'o',
noCacheDefault: true,
parse: async (input: string | undefined) => maybeGetOrg(input),
summary: messages.getMessage('flags.optionalTargetOrg.summary'),
default: async () => maybeGetOrg(),
defaultHelp: async (context) => {
if (context.options instanceof Org) {
const org = context.options as Org;
return org.getUsername();
}
return (await maybeGetOrg())?.getUsername();
},
});
/**
* A required org, specified by username or alias
* Will throw if the specified org default do not exist
* Will default to the default org if one is not specified.
* Will throw if no default org exists and none is specified
*
* @example
*
* ```
* import { Flags } from '@salesforce/sf-plugins-core';
* public static flags = {
* // setting length or prefix
* 'target-org': Flags.requiredOrg(),
* // adding properties
* 'flag2': Flags.requiredOrg({
* required: true,
* description: 'flag2 description',
* char: 'o'
* }),
* }
* ```
*/
export const requiredOrgFlag = Flags.custom({
char: 'o',
summary: messages.getMessage('flags.targetOrg.summary'),
noCacheDefault: true,
parse: async (input: string | undefined) => getOrgOrThrow(input),
default: async () => getOrgOrThrow(),
defaultHelp: async (context) => {
if (context.options instanceof Org) {
const org = context.options as Org;
return org.getUsername();
}
return (await maybeGetOrg())?.getUsername();
},
required: true,
});
/**
* A required org that is a devHub
* Will throw if the specified org does not exist
* Will default to the default dev hub if one is not specified
* Will throw if no default deb hub exists and none is specified
*
* @example
*
* ```
* import { Flags } from '@salesforce/sf-plugins-core';
* public static flags = {
* // setting length or prefix
* 'target-org': requiredHub(),
* // adding properties
* 'flag2': requiredHub({
* required: true,
* description: 'flag2 description',
* char: 'h'
* }),
* }
* ```
*/
export const requiredHubFlag = Flags.custom({
char: 'v',
summary: messages.getMessage('flags.targetDevHubOrg.summary'),
noCacheDefault: true,
parse: async (input: string | undefined) => getHubOrThrow(input),
default: async () => getHubOrThrow(),
defaultHelp: async (context) => {
if (context.options instanceof Org) {
const org = context.options as Org;
return org.getUsername();
}
return (await maybeGetHub())?.getUsername();
},
required: true,
});
/**
* An optional org that, if present, must be a devHub
* Will throw if the specified org does not exist
* Will default to the default dev hub if one is not specified
* Will NOT throw if no default deb hub exists and none is specified
*
* @example
*
* ```
* import { Flags } from '@salesforce/sf-plugins-core';
* public static flags = {
* // setting length or prefix
* 'target-org': optionalHubFlag(),
* // adding properties
* 'flag2': optionalHubFlag({
* description: 'flag2 description',
* char: 'h'
* }),
* }
* ```
*/
export const optionalHubFlag = Flags.custom({
char: 'v',
summary: messages.getMessage('flags.optionalTargetDevHubOrg.summary'),
noCacheDefault: true,
parse: async (input: string | undefined) => maybeGetHub(input),
default: async () => maybeGetHub(),
defaultHelp: async (context) => {
if (context.options instanceof Org) {
const org = context.options as Org;
return org.getUsername();
}
return (await maybeGetHub())?.getUsername();
},
required: false,
});