@ossy/cli 0.0.1-alpha → 0.0.3-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/package.json CHANGED
@@ -1,9 +1,10 @@
1
1
  {
2
2
  "name": "@ossy/cli",
3
- "version": "0.0.1-alpha",
3
+ "version": "0.0.3-alpha",
4
4
  "description": "Command line tool that makes it easier to interact with our APIs",
5
5
  "source": "./src/index.js",
6
6
  "main": "./src/index.js",
7
+ "type": "module",
7
8
  "scripts": {
8
9
  "test": "echo \"Error: no test specified\" && exit 1",
9
10
  "build": "echo \"The build step is not required when using JavaScript!\" && exit 0"
package/src/cms/cli.js CHANGED
@@ -1,17 +1,32 @@
1
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')
2
+ import { resolve } from 'path'
3
+ import { readFileSync } from 'fs'
4
+ import arg from 'arg'
5
+ import fetch from 'node-fetch'
6
+ import { logInfo, logErrorAndReject, logDebug } from '../log.js'
7
7
 
8
8
  const config = {
9
9
  apiUrl: 'https://api.ossy.se/api/v0',
10
10
  // apiUrl: 'http://localhost:3001/api/v0'
11
11
  }
12
12
 
13
- const getTokenPayload = token =>
14
- JSON.parse(Buffer.from(token.split('.')[1], 'base64').toString())
13
+ const Api = {
14
+ uploadResourceTemplates: (token, workspaceId, resourceTemplates) => {
15
+ const endpoint = `${config.apiUrl}/workspaces/${workspaceId}/resource-templates`
16
+
17
+ const fetchOptions = {
18
+ method: 'POST',
19
+ headers: { 'Authorization': token, 'Content-Type': 'application/json' },
20
+ body: JSON.stringify(resourceTemplates)
21
+ }
22
+
23
+ return fetch(endpoint, fetchOptions)
24
+ }
25
+ }
26
+
27
+ const resolveConfigImport = path => path.endsWith('json')
28
+ ? Promise.resolve(JSON.parse(readFileSync(path), 'utf8'))
29
+ : import(path)
15
30
 
16
31
  const importResourceTemplates = options => {
17
32
 
@@ -25,32 +40,29 @@ const importResourceTemplates = options => {
25
40
 
26
41
  logInfo({ message: '[CMS] reading files' })
27
42
  const token = parsedArgs['--authentication']
28
- const tokenPayload = getTokenPayload(token)
29
- const ossyfile = JSON.parse(readFileSync(resolve(parsedArgs['--ossy-file']), 'utf8'))
43
+ const filePath = resolve(parsedArgs['--ossy-file'])
30
44
 
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'})
45
+ if (!token) return logErrorAndReject({ message: '[CMS] No token provided with --authentication'})
34
46
 
35
- logInfo({ message: '[CMS] uploading resource templates' })
47
+ return resolveConfigImport(filePath)
48
+ .then(module => {
49
+ const config = module?.default
50
+ const workspaceId = config?.workspaceId
51
+ const resourceTemplates = config?.resourceTemplates
36
52
 
37
- const endpoint = `${config.apiUrl}/workspaces/${ossyfile.workspaceId}/resource-templates`
53
+ if (!workspaceId) return logErrorAndReject({ message: '[CMS] No workspaceId provided in ossy.json'})
54
+ if (!resourceTemplates) return logErrorAndReject({ message: '[CMS] No resource templates provided in ossy.json'})
38
55
 
39
- const fetchOptions = {
40
- method: 'POST',
41
- headers: { 'Authorization': token, 'Content-Type': 'application/json' },
42
- body: JSON.stringify(ossyfile.resourceTemplates)
43
- }
56
+ logInfo({ message: '[CMS] uploading resource templates' })
57
+ return Api.uploadResourceTemplates(token, workspaceId, resourceTemplates)
58
+ .then(() => logInfo({ message: '[CMS] Done' }))
59
+ .catch(error => logErrorAndReject({ message: '[CMS] Error', error }))
60
+ })
44
61
 
45
- fetch(endpoint, fetchOptions)
46
- .then(() => logInfo({ message: '[CMS] Done' }))
47
- .catch(error => logError({ message: '[CMS] Error', error }))
48
62
  }
49
63
 
50
- module.exports = {
51
- handler: ([command, ...options]) => {
52
- !!command
53
- ? { 'import-resource-templates': importResourceTemplates }[command](options)
54
- : logError({ message: '[CMS] No command provided' })
55
- }
64
+ export const handler = ([command, ...options]) => {
65
+ !!command
66
+ ? { 'import-resource-templates': importResourceTemplates }[command](options)
67
+ : logError({ message: '[CMS] No command provided' })
56
68
  }
package/src/index.js CHANGED
@@ -4,7 +4,7 @@
4
4
  const [_, __, handlerName, ...restArgs] = process.argv
5
5
 
6
6
  const loadHandler = {
7
- cms: () => require('./cms/cli.js')
7
+ cms: () => import('./cms/cli.js')
8
8
  }[handlerName]
9
9
 
10
- !!loadHandler && loadHandler().handler(restArgs)
10
+ !!loadHandler && loadHandler().then(({ handler })=> handler(restArgs))
package/src/log.js CHANGED
@@ -11,15 +11,15 @@ const TypeOfMessage = {
11
11
  Debug: 'DEBUG'
12
12
  }
13
13
 
14
- const log = (...params) => { console.log(...params) }
14
+ export const log = (...params) => { console.log(...params) }
15
15
  const prefixTo = (prefix, message) => `[${prefix}]${message.startsWith('[') ? '' : ': '}`
16
16
 
17
- const logInfo = logInput => {
17
+ export const logInfo = logInput => {
18
18
  const messagePrefix = prefixTo(TypeOfMessage.Info, logInput.message)
19
19
  log(`${messagePrefix}${logInput.message}`)
20
20
  }
21
21
 
22
- const logError = logInput => {
22
+ export const logError = logInput => {
23
23
  const messagePrefix = prefixTo(TypeOfMessage.Error, logInput.message)
24
24
  log('\n')
25
25
  log(`${messagePrefix}${logInput.message}`)
@@ -27,7 +27,7 @@ const logError = logInput => {
27
27
  log('\n')
28
28
  }
29
29
 
30
- const logDebug = logInput => {
30
+ export const logDebug = logInput => {
31
31
  const isDebugOn = process.env.DEBUG || true
32
32
  if (!isDebugOn) return
33
33
  const messagePrefix = prefixTo(TypeOfMessage.Debug, logInput.message)
@@ -37,14 +37,7 @@ const logDebug = logInput => {
37
37
  log('\n')
38
38
  }
39
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
40
+ export const logErrorAndReject = logInput => {
41
+ logError(logInput)
42
+ return Promise.reject(logInput.error)
50
43
  }