@ossy/cli 0.7.12 → 0.7.13
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 +3 -3
- package/src/index.js +4 -3
- package/src/log.js +44 -0
- package/src/resource-templates/cli.js +65 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ossy/cli",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.13",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/ossy-se/packages.git"
|
|
@@ -24,8 +24,8 @@
|
|
|
24
24
|
"registry": "https://registry.npmjs.org"
|
|
25
25
|
},
|
|
26
26
|
"files": [
|
|
27
|
-
"/
|
|
27
|
+
"/src",
|
|
28
28
|
"README.md"
|
|
29
29
|
],
|
|
30
|
-
"gitHead": "
|
|
30
|
+
"gitHead": "9a4b45a86995c0b7d512f78fbe5d07ad0603f8c2"
|
|
31
31
|
}
|
package/src/index.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
/* eslint-disable global-require, no-unused-vars */
|
|
3
|
+
import { handler } from './resource-templates/cli.js'
|
|
3
4
|
|
|
4
5
|
const [_, __, handlerName, ...restArgs] = process.argv
|
|
5
6
|
|
|
6
|
-
const
|
|
7
|
-
|
|
7
|
+
const handler = {
|
|
8
|
+
'resource-templates': () => handler
|
|
8
9
|
}[handlerName]
|
|
9
10
|
|
|
10
|
-
|
|
11
|
+
handler(restArgs)
|
package/src/log.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
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
|
+
|
|
15
|
+
export const log = (...params) => { console.log(...params) }
|
|
16
|
+
const prefixTo = (prefix, message) => `[${prefix}]${message.startsWith('[') ? '' : ': '}`
|
|
17
|
+
|
|
18
|
+
export const logInfo = logInput => {
|
|
19
|
+
const messagePrefix = prefixTo(TypeOfMessage.Info, logInput.message)
|
|
20
|
+
log(`${messagePrefix}${logInput.message}`)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const logError = logInput => {
|
|
24
|
+
const messagePrefix = prefixTo(TypeOfMessage.Error, logInput.message)
|
|
25
|
+
log('\n')
|
|
26
|
+
log(`${messagePrefix}${logInput.message}`)
|
|
27
|
+
logInput.error && log('[Reason]:', logInput.error)
|
|
28
|
+
log('\n')
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export const logDebug = logInput => {
|
|
32
|
+
const isDebugOn = process.env.DEBUG || true
|
|
33
|
+
if (!isDebugOn) return
|
|
34
|
+
const messagePrefix = prefixTo(TypeOfMessage.Debug, logInput.message)
|
|
35
|
+
log('\n')
|
|
36
|
+
log(`${messagePrefix}${logInput.message}`)
|
|
37
|
+
logInput.data && log('[DEBUG DATA]:', logInput.data)
|
|
38
|
+
log('\n')
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export const logErrorAndReject = logInput => {
|
|
42
|
+
logError(logInput)
|
|
43
|
+
return Promise.reject(logInput.error)
|
|
44
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { resolve } from 'path'
|
|
2
|
+
import { readFileSync } from 'fs'
|
|
3
|
+
import arg from 'arg'
|
|
4
|
+
import { logInfo, logError, logErrorAndReject } from '../log.js'
|
|
5
|
+
|
|
6
|
+
const Api = {
|
|
7
|
+
uploadResourceTemplates: (apiUrl, token, workspaceId, resourceTemplates) => {
|
|
8
|
+
const endpoint = `${apiUrl}/workspaces/${workspaceId}/resource-templates`
|
|
9
|
+
|
|
10
|
+
const fetchOptions = {
|
|
11
|
+
method: 'POST',
|
|
12
|
+
headers: { 'Authorization': token, 'Content-Type': 'application/json' },
|
|
13
|
+
body: JSON.stringify(resourceTemplates)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return fetch(endpoint, fetchOptions)
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const resolveConfigImport = path => path.endsWith('json')
|
|
21
|
+
? Promise.resolve(JSON.parse(readFileSync(path), 'utf8'))
|
|
22
|
+
: import(path)
|
|
23
|
+
|
|
24
|
+
const importResourceTemplates = options => {
|
|
25
|
+
|
|
26
|
+
const parsedArgs = arg({
|
|
27
|
+
'--authentication': String,
|
|
28
|
+
'--a': '--authentication',
|
|
29
|
+
|
|
30
|
+
'--config': String,
|
|
31
|
+
'-c': '--config',
|
|
32
|
+
}, { argv: options })
|
|
33
|
+
|
|
34
|
+
logInfo({ message: '[CMS] reading files' })
|
|
35
|
+
const token = parsedArgs['--authentication'];
|
|
36
|
+
const filePath = resolve(parsedArgs['--config'])
|
|
37
|
+
|
|
38
|
+
if (!token) return logErrorAndReject({ message: '[CMS] No token provided with --authentication'})
|
|
39
|
+
|
|
40
|
+
return resolveConfigImport(filePath)
|
|
41
|
+
.then(module => {
|
|
42
|
+
const config = module?.default
|
|
43
|
+
const apiUrl = config?.apiUrl || 'https://api.ossy.se/api/v0'
|
|
44
|
+
const workspaceId = config?.workspaceId
|
|
45
|
+
const resourceTemplates = config?.resourceTemplates
|
|
46
|
+
|
|
47
|
+
if (!workspaceId) return logErrorAndReject({ message: '[@ossy/cli] No workspaceId provided in config'})
|
|
48
|
+
if (!resourceTemplates) return logErrorAndReject({ message: '[@ossy/cli] No resource templates provided in config'})
|
|
49
|
+
|
|
50
|
+
logInfo({ message: '[@ossy/cli] uploading resource templates' })
|
|
51
|
+
|
|
52
|
+
Api.uploadResourceTemplates(apiUrl, token, workspaceId, resourceTemplates)
|
|
53
|
+
.then(response => {
|
|
54
|
+
logInfo({ message: '[@ossy/cli] Done' })
|
|
55
|
+
})
|
|
56
|
+
.catch(error => logError({ message: '[@ossy/cli] Error', error }))
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export const handler = ([command, ...options]) => {
|
|
62
|
+
!!command
|
|
63
|
+
? { 'upload': importResourceTemplates }[command](options)
|
|
64
|
+
: logError({ message: '[@ossy/cli] No command provided' })
|
|
65
|
+
}
|