@ossy/cli 0.0.2-alpha → 0.0.4-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.2-alpha",
3
+ "version": "0.0.4-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,56 +1,67 @@
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')
7
-
8
- const config = {
9
- apiUrl: 'https://api.ossy.se/api/v0',
10
- // apiUrl: 'http://localhost:3001/api/v0'
2
+ import { resolve } from 'path'
3
+ import { readFileSync } from 'fs'
4
+ import arg from 'arg'
5
+ import fetch from 'node-fetch'
6
+ import { logInfo, logError, logErrorAndReject, logDebug } from '../log.js'
7
+
8
+ const Api = {
9
+ uploadResourceTemplates: (apiUrl, token, workspaceId, resourceTemplates) => {
10
+ const endpoint = `${apiUrl}/workspaces/${workspaceId}/resource-templates`
11
+
12
+ const fetchOptions = {
13
+ method: 'POST',
14
+ headers: { 'Authorization': token, 'Content-Type': 'application/json' },
15
+ body: JSON.stringify(resourceTemplates)
16
+ }
17
+
18
+ return fetch(endpoint, fetchOptions)
19
+ }
11
20
  }
12
21
 
22
+ const resolveConfigImport = path => path.endsWith('json')
23
+ ? Promise.resolve(JSON.parse(readFileSync(path), 'utf8'))
24
+ : import(path)
25
+
13
26
  const importResourceTemplates = options => {
14
27
 
15
28
  const parsedArgs = arg({
16
29
  '--authentication': String,
17
30
  '--a': '--authentication',
18
31
 
19
- '--ossy-file': String,
20
- '-o': '--ossy-file',
32
+ '--config': String,
33
+ '-c': '--config',
21
34
  }, { argv: options })
22
35
 
23
36
  logInfo({ message: '[CMS] reading files' })
24
- const token = parsedArgs['--authentication']
25
- const filePath = resolve(parsedArgs['--ossy-file'])
37
+ const token = parsedArgs['--authentication'];
38
+ const filePath = resolve(parsedArgs['--config'])
26
39
 
27
- const ossyfile = filePath.endsWith('json')
28
- ? JSON.parse(readFileSync(filePath), 'utf8')
29
- : require(filePath)
40
+ if (!token) return logErrorAndReject({ message: '[CMS] No token provided with --authentication'})
30
41
 
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'})
42
+ return resolveConfigImport(filePath)
43
+ .then(module => {
44
+ const config = module?.default
45
+ const apiUrl = config?.apiUrl || 'https://api.ossy.se/api/v0'
46
+ const workspaceId = config?.workspaceId
47
+ const resourceTemplates = config?.resourceTemplates
34
48
 
35
- logInfo({ message: '[CMS] uploading resource templates' })
49
+ if (!workspaceId) return logErrorAndReject({ message: '[CMS] No workspaceId provided in ossy.json'})
50
+ if (!resourceTemplates) return logErrorAndReject({ message: '[CMS] No resource templates provided in ossy.json'})
36
51
 
37
- const endpoint = `${config.apiUrl}/workspaces/${ossyfile.workspaceId}/resource-templates`
52
+ logInfo({ message: '[CMS] uploading resource templates' })
38
53
 
39
- const fetchOptions = {
40
- method: 'POST',
41
- headers: { 'Authorization': token, 'Content-Type': 'application/json' },
42
- body: JSON.stringify(ossyfile.resourceTemplates)
43
- }
54
+ Api.uploadResourceTemplates(apiUrl, token, workspaceId, resourceTemplates)
55
+ .then(response => {
56
+ logInfo({ message: '[CMS] Done' })
57
+ })
58
+ .catch(error => logError({ message: '[CMS] Error', error }))
59
+ })
44
60
 
45
- fetch(endpoint, fetchOptions)
46
- .then(() => logInfo({ message: '[CMS] Done' }))
47
- .catch(error => logError({ message: '[CMS] Error', error }))
48
61
  }
49
62
 
50
- module.exports = {
51
- handler: ([command, ...options]) => {
52
- !!command
53
- ? { 'import-resource-templates': importResourceTemplates }[command](options)
54
- : logError({ message: '[CMS] No command provided' })
55
- }
63
+ export const handler = ([command, ...options]) => {
64
+ !!command
65
+ ? { 'import-resource-templates': importResourceTemplates }[command](options)
66
+ : logError({ message: '[CMS] No command provided' })
56
67
  }
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
  }