-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.ts
More file actions
115 lines (107 loc) · 2.9 KB
/
index.ts
File metadata and controls
115 lines (107 loc) · 2.9 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
import { cliux } from '@contentstack/cli-utilities'
import config from '../config'
import { Stack } from '../types'
export async function getStack(managementSdk: any, apiKey: string, spinner: any) {
const stackDetails = await managementSdk
.stack({ api_key: apiKey })
.fetch()
.then((data: any) => data)
.catch((error: any) => {
handleErrorMsg(error, spinner)
})
return {
api_key: apiKey,
master_locale: stackDetails.master_locale,
name: stackDetails.name,
org_uid: stackDetails.org_uid,
uid: stackDetails.uid
} as Stack
}
export async function getUsers(managementSdk: any, apiKey: string, spinner: any) {
const users = await managementSdk
.stack({ api_key: apiKey })
.users()
.then((data: any) => data)
.catch((error: any) => {
handleErrorMsg(error, spinner)
})
return users
}
export async function getContentTypes(
managementSdk: any,
apiKey: string,
spinner: any,
skip = config.skip,
contentTypes: any[] = []
): Promise<any[]> {
const ct = await managementSdk
.stack({ api_key: apiKey })
.contentType()
.query()
.find()
.then((data: any) => data)
.catch((error: any) => {
handleErrorMsg(error, spinner)
})
if (ct?.items?.length > 0) {
contentTypes = [...contentTypes, ...ct.items]
skip += config.limit
if (skip < ct.count) return getContentTypes(managementSdk, apiKey, spinner, skip, contentTypes)
}
return contentTypes
}
export async function getGlobalFields(
managementSdk: any,
apiKey: string,
spinner: any,
skip = config.skip,
globalFields: any[] = []
): Promise<any[]> {
const gf = await managementSdk
.stack({ api_key: apiKey })
.globalField()
.query()
.find()
.then((data: any) => data)
.catch((error: any) => {
handleErrorMsg(error, spinner)
})
if (gf?.items?.length > 0) {
globalFields = [...globalFields, ...gf.items]
skip += config.limit
if (skip < gf.count) return getGlobalFields(managementSdk, apiKey, spinner, skip, globalFields)
}
return globalFields
}
export async function getContentType(params: {
managementSdk: any;
apiKey: string;
uid: string;
ctVersion?: string;
spinner: any;
}) {
const { managementSdk, apiKey, ctVersion, spinner, uid } = params
const param = ctVersion ? { version: ctVersion } : {}
const ct = await managementSdk
.stack({ api_key: apiKey })
.contentType(uid)
.fetch(param)
.then((data: any) => data)
.catch((error: any) => {
handleErrorMsg(error, spinner)
})
return ct
}
function handleErrorMsg(err: any, spinner: any) {
cliux.loaderV2('', spinner)
if (err?.errorMessage) {
cliux.print(`Error: ${err.errorMessage}`, { color: 'red' })
} else if (err?.message) {
cliux.print(`Error: ${err.message}`, { color: 'red' })
} else {
cliux.print('Error: Something went wrong.Please try again!', {
color: 'red'
})
}
process.exit(1)
}