@contentstack/cli-command 0.1.1-beta.3 → 0.1.1-beta.6

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contentstack/cli-command",
3
- "version": "0.1.1-beta.3",
3
+ "version": "0.1.1-beta.6",
4
4
  "description": "Base class for Contentstack command",
5
5
  "main": "./src/index.js",
6
6
  "author": "Contentstack",
@@ -12,7 +12,7 @@
12
12
  "url": "git+https://github.com/contentstack/cli.git"
13
13
  },
14
14
  "files": [
15
- "./src"
15
+ "/src"
16
16
  ],
17
17
  "license": "MIT",
18
18
  "bugs": "https://github.com/contentstack/cli/issues",
@@ -0,0 +1,23 @@
1
+ class TokenNotFound extends Error {
2
+ constructor(value) {
3
+ super()
4
+ this.value = value
5
+ this.message = 'Token not found'
6
+ this.toString = function () {
7
+ return this.value + this.message
8
+ }
9
+ }
10
+ }
11
+
12
+ class NotLoggedIn extends Error {
13
+ constructor() {
14
+ super()
15
+ this.message = 'You are not logged in. Please login with command $ csdx auth:login'
16
+ this.toString = function () {
17
+ return this.message
18
+ }
19
+ }
20
+ }
21
+
22
+ module.exports.TokenNotFound = TokenNotFound
23
+ module.exports.NotLoggedIn = NotLoggedIn
package/src/index.js ADDED
@@ -0,0 +1,101 @@
1
+ const {Command, flags} = require('@oclif/command')
2
+ const ContentstackManagementSDK = require('@contentstack/management')
3
+ const ContentstackDeliverySDK = require('contentstack')
4
+ const {TokenNotFound, NotLoggedIn} = require('./custom-errors')
5
+ const Configstore = require('configstore')
6
+ const config = new Configstore('contentstack_cli')
7
+ const url = require('url')
8
+ const defaultRegion = {cma: 'https://api.contentstack.io', cda: 'https://cdn.contentstack.io', name: 'NA'}
9
+ const defaultRateLimit = 5
10
+
11
+
12
+ class ContentstackCommand extends Command {
13
+
14
+ get managementAPIClient() {
15
+ if (this._managementAPIClient) return this._managementAPIClient
16
+ this._managementAPIClient = ContentstackManagementSDK.client({host:this.cmaHost})
17
+ return this._managementAPIClient
18
+ }
19
+
20
+ set managementAPIClient(params) {
21
+ if(params && params.host) {
22
+ //can not set host explicitly as CLI runs under constant host coming from config
23
+ params.host = this.cmaHost
24
+ }
25
+ this._managementAPIClient = ContentstackManagementSDK.client(params)
26
+ }
27
+
28
+ get email() {
29
+ if (this._email) return this._email
30
+ this._email = config.get('email')
31
+ if(this._email) return this._email
32
+ throw new NotLoggedIn()
33
+ }
34
+
35
+ get deliveryAPIClient() {
36
+ if (this._deliveryAPIClient) return this._deliveryAPIClient
37
+ this._deliveryAPIClient = ContentstackDeliverySDK
38
+ return this._deliveryAPIClient
39
+ }
40
+
41
+ get region() {
42
+ if (this._region) return this._region
43
+ this._region = config.get('region')
44
+ if (this._region) return this._region
45
+ // return defaultRegion
46
+ }
47
+
48
+ get rateLimit() {
49
+ this._rateLimit = config.get('rate-limit')
50
+ if (this._rateLimit) return this._rateLimit
51
+ return defaultRateLimit
52
+ }
53
+
54
+ get cmaHost() {
55
+ let cma = this.region.cma
56
+ if (cma.startsWith('http')) {
57
+ const u = url.parse(cma)
58
+ if (u.host) return u.host
59
+ }
60
+ return cma
61
+ }
62
+
63
+ get cdaHost() {
64
+ let cda = this.region.cda
65
+ if (cda.startsWith('http')) {
66
+ const u = url.parse(cda)
67
+ if (u.host) return u.host
68
+ }
69
+ return cda
70
+ }
71
+
72
+ get cdaAPIUrl() {
73
+ let cda = this.region.cda
74
+ return cda.startsWith('http') ? cda : `https://${cda}`
75
+ }
76
+
77
+ get cmaAPIUrl() {
78
+ let cma = this.region.cma
79
+ return cma.startsWith('http') ? cma : `https://${cma}`
80
+ }
81
+
82
+ get authToken() {
83
+ if (this._authToken) return this._authToken
84
+ this._authToken = config.get('authtoken')
85
+ if (this._authToken) return this._authToken
86
+ throw new NotLoggedIn()
87
+ }
88
+
89
+ getToken(alias) {
90
+ if (alias) {
91
+ const token = config.get(`tokens.${alias}`)
92
+ if (token) return token
93
+ }
94
+ throw new TokenNotFound(alias)
95
+ }
96
+ }
97
+
98
+ module.exports = {
99
+ Command: ContentstackCommand,
100
+ flags
101
+ }