forked from ionic-team/ionic-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathguards.ts
More file actions
358 lines (297 loc) · 10.4 KB
/
guards.ts
File metadata and controls
358 lines (297 loc) · 10.4 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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
import {
APIResponse,
APIResponseError,
APIResponseSuccess,
AndroidBuildOutput,
App,
AppAssociation,
AzureDevopsRepoAssociation,
BitbucketCloudRepoAssociation,
BitbucketServerRepoAssociation,
CommandPreRun,
CordovaPackageJson,
ExitCodeException,
GithubBranch,
GithubRepo,
GithubRepoAssociation,
GitlabEnterpriseRepoAssociation,
GitlabRepoAssociation,
ICommand,
IMultiProjectConfig,
IProjectConfig,
IntegrationName,
LegacyAndroidBuildOutputEntry,
Login,
OpenIdToken,
Org,
Response,
SSHKey,
SecurityProfile,
Snapshot,
StarterManifest,
SuperAgentError,
User
} from './definitions';
import { AuthConnection } from './lib/oauth/auth';
export const INTEGRATION_NAMES: IntegrationName[] = ['capacitor', 'cordova', 'enterprise'];
export function isCommand(cmd: any): cmd is ICommand {
return cmd && typeof cmd.run === 'function';
}
export function isCommandPreRun(cmd: any): cmd is CommandPreRun {
return cmd && typeof cmd.preRun === 'function';
}
export function isStarterManifest(obj: any): obj is StarterManifest {
return obj &&
typeof obj.name === 'string' &&
typeof obj.baseref === 'string';
}
export function isCordovaPackageJson(obj: any): obj is CordovaPackageJson {
return obj &&
typeof obj.name === 'string' &&
typeof obj.cordova === 'object' &&
Array.isArray(obj.cordova.platforms) &&
typeof obj.cordova.plugins === 'object';
}
export function isLegacyAndroidBuildOutputFile(obj: any): obj is LegacyAndroidBuildOutputEntry[] {
if (!Array.isArray(obj)) {
return false;
}
if (obj.length === 0) {
return true;
}
return obj[0]
&& typeof obj[0].path === 'string'
&& typeof obj[0].outputType === 'object'
&& typeof obj[0].outputType.type === 'string';
}
export function isAndroidBuildOutputFile(obj: any): obj is AndroidBuildOutput {
return obj &&
typeof obj.artifactType === 'object' &&
typeof obj.artifactType.type === 'string' &&
Array.isArray(obj.elements);
}
export function isExitCodeException(err: any): err is ExitCodeException {
return err && typeof err.exitCode === 'number' && err.exitCode >= 0 && err.exitCode <= 255;
}
export function isSuperAgentError(err: any): err is SuperAgentError {
return err && err.response && typeof err.response === 'object';
}
export function isAPIResponseSuccess(res: any): res is APIResponseSuccess {
return res && (typeof res.data === 'object' || typeof res.data === 'string');
}
export function isAPIResponseError(res: any): res is APIResponseError {
return res && typeof res.error === 'object';
}
export function isOrg(org: any): org is Org {
return org && typeof org.name === 'string';
}
export function isGithubRepo(repo: any): repo is GithubRepo {
return repo
&& typeof repo.full_name === 'string'
&& typeof repo.id === 'number';
}
export function isGithubBranch(branch: any): branch is GithubBranch {
return branch && typeof branch.name === 'string';
}
export function isGithubRepoListResponse(res: any): res is Response<GithubRepo[]> {
if (!isAPIResponseSuccess(res) || !Array.isArray(res.data)) {
return false;
}
if (res.data.length === 0) {
return true;
}
return isGithubRepo(res.data[0]);
}
export function isGithubBranchListResponse(res: any): res is Response<GithubBranch[]> {
if (!isAPIResponseSuccess(res) || !Array.isArray(res.data)) {
return false;
}
if (res.data.length === 0) {
return true;
}
return isGithubBranch(res.data[0]);
}
export function isAppAssociation(association: any): association is AppAssociation {
return (
association &&
typeof association.repository === 'object' &&
typeof association.repository.html_url === 'string' &&
(
isGithubRepoAssociation(association.repository) ||
isBitbucketCloudRepoAssociation(association.repository) ||
isBitbucketServerRepoAssociation(association.repository) ||
isGitlabRepoAssociation(association.repository) ||
isGitlabEnterpriseRepoAssociation(association.repository) ||
isAzureDevopsRepoAssociation(association.repository)
)
);
}
export function isAppAssociationResponse(res: APIResponse): res is Response<AppAssociation> {
return isAPIResponseSuccess(res)
&& typeof res.data === 'object'
&& isAppAssociation(res.data);
}
export function isGithubRepoAssociation(association: any): association is GithubRepoAssociation {
return association
&& association.type === 'github'
&& typeof association.id === 'number';
}
export function isGitlabRepoAssociation(association: any): association is GitlabRepoAssociation {
return association
&& association.type === 'gitlab'
&& typeof association.id === 'number';
}
export function isGitlabEnterpriseRepoAssociation(association: any): association is GitlabEnterpriseRepoAssociation {
return association
&& association.type === 'gitlab_enterprise'
&& typeof association.id === 'number';
}
export function isBitbucketCloudRepoAssociation(association: any): association is BitbucketCloudRepoAssociation {
return association
&& association.type === 'bitbucket_cloud'
&& typeof association.id === 'string';
}
export function isBitbucketServerRepoAssociation(association: any): association is BitbucketServerRepoAssociation {
return association
&& association.type === 'bitbucket_server'
&& typeof association.id === 'number';
}
export function isAzureDevopsRepoAssociation(association: any): association is AzureDevopsRepoAssociation {
return association
&& association.type === 'azure_devops'
&& typeof association.id === 'string';
}
export function isApp(app: any): app is App {
return app
&& typeof app === 'object'
&& typeof app.id === 'string'
&& typeof app.name === 'string'
&& typeof app.slug === 'string'
&& (!app.org || isOrg(app.org))
&& (!app.association || isAppAssociation(app.association));
}
export function isAppResponse(res: APIResponse): res is Response<App> {
return isAPIResponseSuccess(res)
&& typeof res.data === 'object'
&& isApp(res.data);
}
export function isAppsResponse(res: APIResponse): res is Response<App[]> {
if (!isAPIResponseSuccess(res) || !Array.isArray(res.data)) {
return false;
}
if (res.data.length === 0) {
return true;
}
return isApp(res.data[0]);
}
export interface OAuthLogin {
redirect_url: string;
}
export function isOAuthLogin(login: any): login is OAuthLogin {
return login && typeof login.redirect_url === 'string';
}
export function isOAuthLoginResponse(res: any): res is Response<OAuthLogin> {
return isAPIResponseSuccess(res) && isOAuthLogin(res.data);
}
export function isOpenIDToken(tokenObj: any): tokenObj is OpenIdToken {
return tokenObj
&& typeof tokenObj.access_token === 'string'
&& typeof tokenObj.expires_in === 'number'
&& (tokenObj.id_token ? typeof tokenObj.id_token === 'string' : true)
&& (tokenObj.refresh_token ? typeof tokenObj.refresh_token === 'string' : true)
&& tokenObj.scope === 'openid profile email offline_access'
&& tokenObj.token_type === 'Bearer';
}
export function isOpenIDTokenExchangeResponse(res: any): res is Response<OpenIdToken> {
return res && typeof res.body === 'object' && isOpenIDToken(res.body);
}
export function isSnapshot(snapshot: any): snapshot is Snapshot {
return snapshot
&& typeof snapshot.id === 'string'
&& typeof snapshot.sha === 'string'
&& typeof snapshot.ref === 'string'
&& typeof snapshot.state === 'string'
&& typeof snapshot.created === 'string'
&& typeof snapshot.note === 'string';
}
export function isSnapshotResponse(res: APIResponse): res is Response<Snapshot> {
return isAPIResponseSuccess(res) && isSnapshot(res.data);
}
export function isSnapshotListResponse(res: APIResponse): res is Response<Snapshot[]> {
if (!isAPIResponseSuccess(res) || !Array.isArray(res.data)) {
return false;
}
if (res.data.length === 0) {
return true;
}
return isSnapshot(res.data[0]);
}
export function isLogin(login: any): login is Login {
return login
&& isUser(login.user)
&& typeof login.token === 'string';
}
export function isLoginResponse(res: APIResponse): res is Response<Login> {
return isAPIResponseSuccess(res) && isLogin(res.data);
}
export function isAuthConnection(connection: any): connection is AuthConnection {
return connection && typeof connection.uuid === 'string';
}
export function isAuthConnectionResponse(res: APIResponse): res is Response<AuthConnection> {
return isAPIResponseSuccess(res) && isAuthConnection(res.data);
}
export function isUser(user: any): user is User {
return user
&& typeof user.id === 'number'
&& typeof user.email === 'string';
}
export function isUserResponse(res: APIResponse): res is Response<User> {
return isAPIResponseSuccess(res) && isUser(res.data);
}
export function isSSHKey(key: any): key is SSHKey {
return key
&& typeof key.id === 'string'
&& typeof key.pubkey === 'string'
&& typeof key.fingerprint === 'string'
&& typeof key.annotation === 'string'
&& typeof key.name === 'string'
&& typeof key.created === 'string'
&& typeof key.updated === 'string';
}
export function isSSHKeyListResponse(res: APIResponse): res is Response<SSHKey[]> {
if (!isAPIResponseSuccess(res) || !Array.isArray(res.data)) {
return false;
}
if (res.data.length === 0) {
return true;
}
return isSSHKey(res.data[0]);
}
export function isSSHKeyResponse(res: APIResponse): res is Response<SSHKey> {
return isAPIResponseSuccess(res) && isSSHKey(res.data);
}
export function isSecurityProfile(obj: any): obj is SecurityProfile {
return obj
&& typeof obj.name === 'string'
&& typeof obj.tag === 'string'
&& typeof obj.type === 'string'
&& typeof obj.created === 'string'
&& typeof obj.credentials === 'object';
}
export function isSecurityProfileResponse(r: APIResponse): r is Response<SecurityProfile> {
const res = r as Response<SecurityProfile>;
return isAPIResponseSuccess(res) && isSecurityProfile(res.data);
}
export function isIntegrationName(name: any): name is IntegrationName {
return INTEGRATION_NAMES.includes(name);
}
export function isProjectConfig(configFile: any): configFile is IProjectConfig {
return configFile
&& typeof configFile.name === 'string'
&& typeof configFile.projects === 'undefined';
}
export function isMultiProjectConfig(configFile: any): configFile is IMultiProjectConfig {
return configFile
&& typeof configFile.name === 'undefined'
&& typeof configFile.projects === 'object';
}