@ossy/cli 0.0.1-alpha

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/README.md ADDED
@@ -0,0 +1,45 @@
1
+ # @ossy/cli
2
+
3
+ Command line tool that makes it easier to interact with our APIs
4
+
5
+ ## Cms
6
+
7
+ ### import-resource-templates
8
+ Imports resource templates to your workspace so that they can be used in the UI.
9
+
10
+ ```bash
11
+ npx @ossy/cli cms import-resource-templates --authentication <cms-api-token> --ossy-file ossy.json
12
+ ```
13
+
14
+ #### Workflow example
15
+
16
+ ```bash
17
+ name: "[CMS] Upload resource templates"
18
+
19
+ on:
20
+ workflow_dispatch:
21
+
22
+ jobs:
23
+
24
+ upload-resource-templates:
25
+ name: Upload resource templates
26
+ runs-on: ubuntu-latest
27
+ steps:
28
+ - uses: actions/checkout@v2
29
+
30
+ - uses: actions/setup-node@v2
31
+ with:
32
+ node-version: "16"
33
+
34
+ - name: Upload
35
+ run: |
36
+ npx --yes @ossy/cli cms import-resource-templates \
37
+ --authentication ${{ secrets.CMS_API_TOKEN }} \
38
+ --ossy-file ossy.json \
39
+ ```
40
+
41
+ #### Arguments
42
+ | Argument | Description | Required |
43
+ |-|-|-|
44
+ | --authentication | Your personal CMS API token | required |
45
+ | --ossy-file | Path to the file containing the workspaceId and resource templates | required |
package/package.json ADDED
@@ -0,0 +1,18 @@
1
+ {
2
+ "name": "@ossy/cli",
3
+ "version": "0.0.1-alpha",
4
+ "description": "Command line tool that makes it easier to interact with our APIs",
5
+ "source": "./src/index.js",
6
+ "main": "./src/index.js",
7
+ "scripts": {
8
+ "test": "echo \"Error: no test specified\" && exit 1",
9
+ "build": "echo \"The build step is not required when using JavaScript!\" && exit 0"
10
+ },
11
+ "author": "Ossy",
12
+ "license": "ISC",
13
+ "bin": "./src/index.js",
14
+ "dependencies": {
15
+ "arg": "^5.0.2",
16
+ "node-fetch": "^2.6.7"
17
+ }
18
+ }
package/src/cms/cli.js ADDED
@@ -0,0 +1,56 @@
1
+ #!/usr/bin/env node
2
+ const { resolve } = require('path')
3
+ const { readFileSync } = require('fs')
4
+ const arg = require('arg')
5
+ const fetch = require('node-fetch')
6
+ const { logInfo, logError } = require('../log')
7
+
8
+ const config = {
9
+ apiUrl: 'https://api.ossy.se/api/v0',
10
+ // apiUrl: 'http://localhost:3001/api/v0'
11
+ }
12
+
13
+ const getTokenPayload = token =>
14
+ JSON.parse(Buffer.from(token.split('.')[1], 'base64').toString())
15
+
16
+ const importResourceTemplates = options => {
17
+
18
+ const parsedArgs = arg({
19
+ '--authentication': String,
20
+ '--a': '--authentication',
21
+
22
+ '--ossy-file': String,
23
+ '-o': '--ossy-file',
24
+ }, { argv: options })
25
+
26
+ logInfo({ message: '[CMS] reading files' })
27
+ const token = parsedArgs['--authentication']
28
+ const tokenPayload = getTokenPayload(token)
29
+ const ossyfile = JSON.parse(readFileSync(resolve(parsedArgs['--ossy-file']), 'utf8'))
30
+
31
+ if (!token) return logError({ message: '[CMS] No token provided with --authentication'})
32
+ if (!ossyfile?.workspaceId) return logError({ message: '[CMS] No workspaceId provided in ossy.json'})
33
+ if (!ossyfile?.resourceTemplates) return logError({ message: '[CMS] No resource templates provided in ossy.json'})
34
+
35
+ logInfo({ message: '[CMS] uploading resource templates' })
36
+
37
+ const endpoint = `${config.apiUrl}/workspaces/${ossyfile.workspaceId}/resource-templates`
38
+
39
+ const fetchOptions = {
40
+ method: 'POST',
41
+ headers: { 'Authorization': token, 'Content-Type': 'application/json' },
42
+ body: JSON.stringify(ossyfile.resourceTemplates)
43
+ }
44
+
45
+ fetch(endpoint, fetchOptions)
46
+ .then(() => logInfo({ message: '[CMS] Done' }))
47
+ .catch(error => logError({ message: '[CMS] Error', error }))
48
+ }
49
+
50
+ module.exports = {
51
+ handler: ([command, ...options]) => {
52
+ !!command
53
+ ? { 'import-resource-templates': importResourceTemplates }[command](options)
54
+ : logError({ message: '[CMS] No command provided' })
55
+ }
56
+ }
package/src/index.js ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env node
2
+ /* eslint-disable global-require, no-unused-vars */
3
+
4
+ const [_, __, handlerName, ...restArgs] = process.argv
5
+
6
+ const loadHandler = {
7
+ cms: () => require('./cms/cli.js')
8
+ }[handlerName]
9
+
10
+ !!loadHandler && loadHandler().handler(restArgs)
package/src/log.js ADDED
@@ -0,0 +1,50 @@
1
+ // interface LogInput {
2
+ // type: TypeOfMessage;
3
+ // message: string;
4
+ // data?: any;
5
+ // dataPrefix: string;
6
+ // }
7
+
8
+ const TypeOfMessage = {
9
+ Info: 'INFO',
10
+ Error: 'ERROR',
11
+ Debug: 'DEBUG'
12
+ }
13
+
14
+ const log = (...params) => { console.log(...params) }
15
+ const prefixTo = (prefix, message) => `[${prefix}]${message.startsWith('[') ? '' : ': '}`
16
+
17
+ const logInfo = logInput => {
18
+ const messagePrefix = prefixTo(TypeOfMessage.Info, logInput.message)
19
+ log(`${messagePrefix}${logInput.message}`)
20
+ }
21
+
22
+ const logError = logInput => {
23
+ const messagePrefix = prefixTo(TypeOfMessage.Error, logInput.message)
24
+ log('\n')
25
+ log(`${messagePrefix}${logInput.message}`)
26
+ logInput.error && log('[Reason]:', logInput.error)
27
+ log('\n')
28
+ }
29
+
30
+ const logDebug = logInput => {
31
+ const isDebugOn = process.env.DEBUG || true
32
+ if (!isDebugOn) return
33
+ const messagePrefix = prefixTo(TypeOfMessage.Debug, logInput.message)
34
+ log('\n')
35
+ log(`${messagePrefix}${logInput.message}`)
36
+ logInput.data && log('[DEBUG DATA]:', logInput.data)
37
+ log('\n')
38
+ }
39
+
40
+ const logErrorAndReject = message => error => {
41
+ logError({ message, error })
42
+ return Promise.reject(error)
43
+ }
44
+
45
+ module.exports = {
46
+ logInfo,
47
+ logError,
48
+ logErrorAndReject,
49
+ logDebug
50
+ }