@contentstack/cli-utilities 1.0.3 → 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,429 +0,0 @@
1
- import ora from 'ora'
2
- import { default as CLIError } from '../cli-error';
3
- import { default as config } from '../config-handler';
4
- import {
5
- Token,
6
- Organization,
7
- selectedOrganization,
8
- Stack,
9
- ContentType,
10
- Environment,
11
- Entry,
12
- Locale
13
- } from './interfaces'
14
- import { shouldNotBeEmpty } from './validations';
15
- import ContentstackManagementSDK from '@contentstack/management';
16
-
17
- const inquirer = require('inquirer')
18
- inquirer.registerPrompt('search-list', require('inquirer-search-list'))
19
- inquirer.registerPrompt('search-checkbox', require('inquirer-search-checkbox'))
20
-
21
- interface Region {
22
- name: string;
23
- cma: string;
24
- cda: string;
25
- }
26
-
27
- let _region: Region;
28
- let _authToken: string;
29
- let _managementAPIClient: ContentstackManagementSDK.ContentstackClient;
30
-
31
- const region = (): Region => {
32
- if (!_region) {
33
- _region = config.get('region');
34
- }
35
-
36
- return _region;
37
- }
38
-
39
- const cmaHost = () => {
40
- let cma = region().cma;
41
- if (cma.startsWith('http')) {
42
- const u = new URL(cma);
43
- if (u.host) return u.host;
44
- }
45
- return cma;
46
- }
47
-
48
- const managementAPIClient = (params) => {
49
- if (params) {
50
- _managementAPIClient = ContentstackManagementSDK.client(params)
51
- } else if (!_managementAPIClient) {
52
- _managementAPIClient = ContentstackManagementSDK.client({ host: cmaHost() });
53
- }
54
-
55
- return _managementAPIClient;
56
- }
57
-
58
- const authToken = () => {
59
- if (!_authToken) {
60
- _authToken = config.get('authtoken');
61
- }
62
- if (!_authToken) {
63
- throw new CLIError('You are not logged in. Please login with command $ csdx auth:login');
64
- }
65
-
66
- return _authToken;
67
- }
68
-
69
- export function chooseOrganization(client: any, displayMessage?: string, region?: string, orgUid?: string): Promise<selectedOrganization> {
70
- return new Promise(async (resolve, reject) => {
71
- try {
72
- const spinner = ora('Loading Organizations').start()
73
- let { items: organizations } = await client.organization().fetchAll()
74
- spinner.stop()
75
- let orgMap: any = {}
76
- if (orgUid) {
77
- organizations.forEach((org: Organization) => {
78
- orgMap[org.uid] = org.name
79
- })
80
- if (orgMap[orgUid]) {
81
- resolve({ orgUid: orgUid, orgName: orgMap[orgUid] })
82
- } else {
83
- return reject(new Error('The given orgUid doesn\'t exist or you might not have access to it.'))
84
- }
85
- } else {
86
- organizations.forEach((org: Organization) => {
87
- orgMap[org.name] = org.uid
88
- })
89
- const orgList = Object.keys(orgMap)
90
- let inquirerConfig = {
91
- type: 'search-list',
92
- name: 'chosenOrganization',
93
- message: displayMessage || 'Choose an organization',
94
- choices: orgList,
95
- loop: false,
96
- validate: shouldNotBeEmpty
97
- }
98
- inquirer.prompt(inquirerConfig).then(({ chosenOrganization }: { chosenOrganization: string }) => {
99
- resolve({ orgUid: orgMap[chosenOrganization], orgName: chosenOrganization })
100
- })
101
- }
102
- } catch (error) {
103
- reject(error)
104
- }
105
- })
106
- }
107
-
108
- export function chooseStack(client: any, organizationId: string, displayMessage?: string, region?: string): Promise<Stack> {
109
- return new Promise(async (resolve, reject) => {
110
- try {
111
- const spinner = ora('Loading Stacks').start()
112
- let { items: stacks } = await client.stack({ organization_uid: organizationId }).query({ query: {} }).find()
113
- spinner.stop()
114
- let stackMap: any = {}
115
- stacks.forEach((stack: Stack) => {
116
- stackMap[stack.name] = stack.api_key
117
- })
118
- const stackList = Object.keys(stackMap)
119
- let inquirerConfig = {
120
- type: 'search-list',
121
- name: 'chosenStack',
122
- choices: stackList,
123
- message: displayMessage || 'Choose a stack',
124
- loop: false,
125
- }
126
- inquirer.prompt(inquirerConfig).then(({ chosenStack }: { chosenStack: string }) => {
127
- resolve({ api_key: stackMap[chosenStack], name: chosenStack })
128
- })
129
- } catch (error) {
130
- console.error(error.message)
131
- }
132
- })
133
- }
134
-
135
- export function chooseContentType(stackApiKey: string, displayMessage?: string, region?: string): Promise<ContentType> {
136
- return new Promise(async (resolve, reject) => {
137
- const client = managementAPIClient({ host: cmaHost(), authtoken: authToken() })
138
-
139
- try {
140
- const spinner = ora('Loading Content Types').start()
141
- // let {items: contentTypes} = await client.stack({api_key: stackApiKey}).contentType().query({include_count: true}).find()
142
- let contentTypes = await getAll(client.stack({ api_key: stackApiKey }).contentType())
143
- spinner.stop()
144
- let contentTypeMap: any = {}
145
- contentTypes.forEach((contentType: ContentType) => {
146
- contentTypeMap[contentType.title] = contentType.uid
147
- })
148
- const contentTypeList = Object.keys(contentTypeMap)
149
- let inquirerConfig = {
150
- type: 'search-list',
151
- name: 'chosenContentType',
152
- choices: contentTypeList,
153
- message: displayMessage || 'Choose a content type',
154
- loop: false,
155
- }
156
- inquirer.prompt(inquirerConfig).then(({ chosenContentType }: { chosenContentType: string }) => {
157
- resolve({ uid: contentTypeMap[chosenContentType], title: chosenContentType })
158
- })
159
- } catch (error) {
160
- console.error(error.message)
161
- }
162
- })
163
- }
164
-
165
- export function chooseEntry(contentTypeUid: string, stackApiKey: string, displayMessage?: string, region?: string): Promise<Entry> {
166
- return new Promise(async (resolve, reject) => {
167
- const client = managementAPIClient({ host: cmaHost(), authtoken: authToken() })
168
-
169
- try {
170
- const spinner = ora('Loading Entries').start()
171
- let entries = await getAll(client.stack({ api_key: stackApiKey }).contentType(contentTypeUid).entry())
172
- spinner.stop()
173
- let entryMap: any = {}
174
- entries.forEach((entry: Entry) => {
175
- entryMap[entry.title] = entry.uid
176
- })
177
- const entryList = Object.keys(entryMap)
178
- let inquirerConfig = {
179
- type: 'search-list',
180
- name: 'chosenEntry',
181
- choices: entryList,
182
- message: displayMessage || 'Choose an entry',
183
- loop: false
184
- }
185
- inquirer.prompt(inquirerConfig).then(({ chosenEntry }: { chosenEntry: string }) => {
186
- resolve({ uid: entryMap[chosenEntry], title: chosenEntry })
187
- })
188
- } catch (error) {
189
- console.error(error.message)
190
- }
191
- })
192
- }
193
-
194
- export function chooseContentTypes(stack: any, displayMessage?: string): Promise<ContentType[]> {
195
- return new Promise(async (resolve, reject) => {
196
- try {
197
- const spinner = ora('Loading Content Types').start()
198
- // let {items: contentTypes} = await client.stack({api_key: stackApiKey}).contentType().query({include_count: true}).find()
199
- let contentTypes = await getAll(stack.contentType())
200
- spinner.stop()
201
- let contentTypeMap: any = {}
202
- contentTypes.forEach((contentType: ContentType) => {
203
- contentTypeMap[contentType.title] = contentType.uid
204
- })
205
- const contentTypeList = Object.keys(contentTypeMap)
206
- let inquirerConfig = {
207
- type: 'search-checkbox',
208
- name: 'chosenContentTypes',
209
- choices: contentTypeList,
210
- message: displayMessage || 'Choose a content type',
211
- loop: false,
212
- }
213
- inquirer.prompt(inquirerConfig).then(({ chosenContentTypes }: { chosenContentTypes: string[] }) => {
214
- let result: ContentType[] = chosenContentTypes.map(ct => {
215
- let foo: ContentType = { uid: contentTypeMap[ct], title: ct }
216
- return foo
217
- })
218
- resolve(result)
219
- })
220
- } catch (error) {
221
- console.error(error.message)
222
- }
223
- })
224
- }
225
-
226
- export function chooseEnvironments(stack: any, displayMessage?: string): Promise<Environment[]> {
227
- return new Promise(async (resolve, reject) => {
228
- try {
229
- const spinner = ora('Loading Environments').start()
230
- // let {items: contentTypes} = await client.stack({api_key: stackApiKey}).contentType().query({include_count: true}).find()
231
- let environments = await getAll(stack.environment())
232
- spinner.stop()
233
- let environmentMap: any = {}
234
- environments.forEach((environment: Environment) => {
235
- environmentMap[environment.name] = environment.uid
236
- })
237
- const environmentList = Object.keys(environmentMap)
238
- let inquirerConfig = {
239
- type: 'search-checkbox',
240
- name: 'chosenEnvironments',
241
- choices: environmentList,
242
- message: displayMessage || 'Choose an environment',
243
- loop: false,
244
- validate: shouldNotBeEmpty
245
- }
246
- inquirer.prompt(inquirerConfig).then(({ chosenEnvironments }: { chosenEnvironments: string[] }) => {
247
- let result: Environment[] = chosenEnvironments.map(env => {
248
- let foo: Environment = { uid: environmentMap[env], name: env }
249
- return foo
250
- })
251
- resolve(result)
252
- })
253
- } catch (error) {
254
- console.error(error.message)
255
- }
256
- })
257
- }
258
-
259
- export function chooseEnvironment(stack: any, displayMessage?: string): Promise<Environment> {
260
- return new Promise(async (resolve, reject) => {
261
- try {
262
- const spinner = ora('Loading Environments').start()
263
- // let {items: contentTypes} = await client.stack({api_key: stackApiKey}).contentType().query({include_count: true}).find()
264
- let environments = await getAll(stack.environment())
265
- spinner.stop()
266
- let environmentMap: any = {}
267
- environments.forEach((environment: Environment) => {
268
- environmentMap[environment.name] = environment.uid
269
- })
270
- const environmentList = Object.keys(environmentMap)
271
- let inquirerConfig = {
272
- type: 'search-list',
273
- name: 'chosenEnvironment',
274
- choices: environmentList,
275
- message: displayMessage || 'Choose an environment',
276
- loop: false,
277
- validate: shouldNotBeEmpty
278
- }
279
- inquirer.prompt(inquirerConfig).then(({ chosenEnvironment }: { chosenEnvironment: string }) => {
280
- let result: Environment = { uid: environmentMap[chosenEnvironment], name: chosenEnvironment }
281
- resolve(result)
282
- })
283
- } catch (error) {
284
- console.error(error.message)
285
- }
286
- })
287
- }
288
-
289
- export function chooseLocales(stack: any, displayMessage?: string): Promise<Locale[]> {
290
- return new Promise(async (resolve, reject) => {
291
- try {
292
- const spinner = ora('Loading Locales').start()
293
- // let {items: contentTypes} = await client.stack({api_key: stackApiKey}).contentType().query({include_count: true}).find()
294
- let locales = await getAll(stack.locale())
295
- spinner.stop()
296
- let localeMap: any = {}
297
- locales.forEach((locale: Locale) => {
298
- localeMap[locale.name] = locale.code
299
- })
300
- const localeList = Object.keys(localeMap)
301
- let inquirerConfig = {
302
- type: 'search-checkbox',
303
- name: 'chosenLocales',
304
- choices: localeList,
305
- message: displayMessage || 'Choose locales',
306
- loop: false,
307
- validate: shouldNotBeEmpty,
308
- }
309
- inquirer.prompt(inquirerConfig).then(({ chosenLocales }: { chosenLocales: string[] }) => {
310
- let result: Locale[] = chosenLocales.map(locale => {
311
- let foo: Locale = { code: localeMap[locale], name: locale }
312
- return foo
313
- })
314
- resolve(result)
315
- })
316
- } catch (error) {
317
- console.error(error.message)
318
- }
319
- })
320
- }
321
-
322
- export function chooseLocale(stack: any, displayMessage?: string, defaultLocale?: Locale): Promise<Locale> {
323
- return new Promise(async (resolve, reject) => {
324
- try {
325
- const spinner = ora('Loading Locales').start()
326
- // let {items: contentTypes} = await client.stack({api_key: stackApiKey}).contentType().query({include_count: true}).find()
327
- let locales = await getAll(stack.locale())
328
- spinner.stop()
329
- let localeMap: any = {}
330
- locales.forEach((locale: Locale) => {
331
- localeMap[locale.name] = locale.code
332
- })
333
- const localeList = Object.keys(localeMap)
334
- let inquirerConfig = {
335
- type: 'search-list',
336
- name: 'chosenLocale',
337
- choices: localeList,
338
- default: defaultLocale,
339
- message: displayMessage || 'Choose locale',
340
- loop: false,
341
- validate: shouldNotBeEmpty
342
- }
343
- inquirer.prompt(inquirerConfig).then(({ chosenLocale }: { chosenLocale: string }) => {
344
- let result: Locale = { code: localeMap[chosenLocale], name: chosenLocale }
345
- resolve(result)
346
- })
347
- } catch (error) {
348
- console.error(error.message)
349
- }
350
- })
351
- }
352
-
353
- export function chooseTokenAlias(): Promise<Token> {
354
- return new Promise(async (resolve, reject) => {
355
- const tokens = config.get('tokens')
356
- const tokenList = Object.keys(tokens).filter((token: string) => tokens[token].type === 'management')
357
- let inquirerConfig = {
358
- type: 'search-list',
359
- name: 'chosenToken',
360
- choices: tokenList,
361
- message: 'Choose an alias to use',
362
- loop: false,
363
- validate: shouldNotBeEmpty,
364
- }
365
- inquirer.prompt(inquirerConfig).then(({ chosenToken }: { chosenToken: string }) => {
366
- resolve({ apiKey: tokens[chosenToken].apiKey, token: tokens[chosenToken].token })
367
- })
368
- })
369
- }
370
-
371
- export function chooseDeliveryTokenAlias(): Promise<Token> {
372
- return new Promise(async (resolve, reject) => {
373
- const tokens = config.get('tokens')
374
- const tokenList = Object.keys(tokens).filter((token: string) => tokens[token].type === 'delivery')
375
- let inquirerConfig = {
376
- type: 'search-list',
377
- name: 'chosenToken',
378
- choices: tokenList,
379
- message: 'Choose an alias to use',
380
- loop: false,
381
- validate: shouldNotBeEmpty,
382
- }
383
- inquirer.prompt(inquirerConfig).then(({ chosenToken }: { chosenToken: string }) => {
384
- resolve({ apiKey: tokens[chosenToken].apiKey, token: tokens[chosenToken].token })
385
- })
386
- })
387
- }
388
-
389
- async function getAll(element: any, skip: number = 0): Promise<any> {
390
- return new Promise(async resolve => {
391
- let result: any[] = []
392
- result = await fetch(element, skip, result)
393
- resolve(result)
394
- })
395
- }
396
-
397
- async function fetch(element: any, skip: number, accumulator: any[]): Promise<any[]> {
398
- return new Promise(async resolve => {
399
- let queryParams = { include_count: true, skip: skip }
400
- let { items: result, count: count } = await element.query(queryParams).find()
401
- accumulator = accumulator.concat(result)
402
- skip += result.length
403
- if (skip < count)
404
- return resolve(await fetch(element, skip, accumulator))
405
- return resolve(accumulator)
406
- })
407
- }
408
-
409
- // async function callMe() {
410
- // // let organization = await chooseOrganization()
411
- // // console.log(organization)
412
-
413
- // // let stack = await chooseStack(organization.orgUid)
414
- // // console.log(stack)
415
-
416
- // // let contentType = await chooseContentType(stack.api_key)
417
- // // console.log(contentType)
418
-
419
- // // let entry = await chooseEntry(contentType.uid, stack.api_key)
420
- // // console.log(entry)
421
-
422
- // // let entry = await chooseEntry(contentType.uid, stack.api_key)
423
- // // console.log(entry)
424
-
425
- // let token = await chooseTokenAlias()
426
- // console.log(token)
427
- // }
428
-
429
- // callMe()
@@ -1,39 +0,0 @@
1
- export interface Token {
2
- token: string;
3
- apiKey: string;
4
- }
5
-
6
- export interface Organization {
7
- uid: string,
8
- name: string,
9
- }
10
-
11
- export interface selectedOrganization {
12
- orgUid: string,
13
- orgName: string,
14
- }
15
-
16
- export interface Stack {
17
- name: string,
18
- api_key: string,
19
- }
20
-
21
- export interface ContentType {
22
- uid: string,
23
- title: string,
24
- }
25
-
26
- export interface Environment {
27
- name: string,
28
- uid: string,
29
- }
30
-
31
- export interface Entry {
32
- uid: string,
33
- title: string,
34
- }
35
-
36
- export interface Locale {
37
- name: string;
38
- code: string;
39
- }
@@ -1,5 +0,0 @@
1
- export function shouldNotBeEmpty(input) {
2
- if (input.length === 0)
3
- throw new Error('Please enter a valid value')
4
- return true
5
- }
@@ -1,4 +0,0 @@
1
- const path = require('path');
2
-
3
- process.env.TS_NODE_PROJECT = path.resolve('test/tsconfig.json');
4
- process.env.CLI_ENV = 'TEST';
@@ -1,6 +0,0 @@
1
- {
2
- "extends": "../tsconfig",
3
- "compilerOptions": {
4
- "noEmit": true
5
- }
6
- }
package/tsconfig.json DELETED
@@ -1,20 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "declaration": false,
4
- "importHelpers": true,
5
- "module": "commonjs",
6
- "outDir": "lib",
7
- "rootDir": "src",
8
- "strict": false,
9
- "target": "es2017",
10
- "allowJs": true,
11
- "skipLibCheck": true,
12
- "esModuleInterop": true
13
- },
14
- "include": [
15
- "src/**/*"
16
- ],
17
- "exclude": [
18
- "src/**/*.d.ts"
19
- ]
20
- }