This repository has been archived by the owner on Jun 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
executable file
·169 lines (140 loc) · 4.66 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env node
var program = require('commander')
var commands = require('./commands')
var auth = require('./lib/cosmic_auth')
var bucketConfig = require('./lib/bucket_config')
var print = require('./lib/output')
var colors = require('colors')
var customScripts = require('./non_standard_commands')
var packageInfo = require('./package.json')
var token = auth.getCosmicToken()
var Cosmic = require('cosmicjs')({
token: token
})
var bucketOpts = bucketConfig.getCosmicBucketOptions()
var bucket = Cosmic.bucket(bucketOpts)
program
.version(packageInfo.version)
commands.forEach(function(command) {
var buildUp = program
.command(command.cmd)
.description(command.description)
if (command.options) {
command.options.forEach(function(option) {
buildUp.option(option.flags, option.description)
})
}
var regExp = /\[([^\]]+)\]/
var argumentParamName = regExp.exec(command.cmd)
argumentParamName = argumentParamName ? argumentParamName[1] : null
if (argumentParamName) {
buildUp
.action(handleArg)
} else {
buildUp
.action(handleNoArg)
}
function handleNoArg(invokedCmd) {
handleAction(null, invokedCmd)
}
function handleArg(arg, invokedCmd) {
var argObj = {
argumentParamName: argumentParamName,
arg: arg
}
handleAction(argObj, invokedCmd)
}
function handleAction(argObj, invokedCmd){
if (command.requiresToken && !token) {
print.error('Authentication required! Please type `cosmic login` to authenticate.')
process.exit(1)
}
if (command.custom) {
runCustomScript(command, invokedCmd, argObj)
} else {
runCosmicCommand(command, invokedCmd, argObj)
}
}
})
program.parse(process.argv)
if (!process.argv.slice(2).length) {
program.outputHelp()
} else {
var receivedCmd = program.args[program.args.length - 1] || {}
if (!receivedCmd._name) {
print.error('Unrecognized command')
}
}
function runCustomScript(command, invokedCmd, argObj) {
if (!customScripts[command.customScript]) {
print.error('Error with custom command ' + command.cmd + '. Please report this at ' + colors.blue('https://github.com/cosmicjs/cosmic-cli/issues/'))
process.exit(1)
}
customScripts[command.customScript]({
invokedCmd: invokedCmd,
bucket: bucket,
token: token,
argObj: argObj,
Cosmic: Cosmic,
})
}
function runCosmicCommand(command, invokedCmd, argObj) {
var params = parseCosmicParameters(command, invokedCmd, argObj)
// some commands have flags that, if present, change the cosmic method we should use
var overrideCommand
command.options.forEach(function(option) {
var cosmicParamName = option.cosmicParamName || option.param
if (option.switchToCommandIfPresent && params[cosmicParamName]) {
overrideCommand = option.switchToCommandIfPresent
}
})
var cosmicMethod = overrideCommand || command.cosmicMethod || {}
var scope = cosmicMethod.useBucket ? bucket : Cosmic
if (!scope[cosmicMethod.method]) {
var scopeStr = cosmicMethod.useBucket ? 'Cosmic.bucket' : 'Cosmic'
print.error('Method ' + cosmicMethod.method + ' does not exist on ' + scopeStr + '. Please report this at ' + colors.blue('https://github.com/cosmicjs/cosmic-cli/issues/'))
process.exit(1)
}
scope[cosmicMethod.method](params).then(function(res){
if (res.status && res.status !== 200) {
print.error('Error:')
console.log(res)
process.exit(1)
}
print.success('Success')
console.log(res)
process.exit(0)
}).catch(function(err) {
print.error('Error:')
console.log(err)
process.exit(1)
})
}
function parseCosmicParameters(command, invokedCmd, argObj) {
var params = {}
command.options.forEach(function(option) {
var paramValue = invokedCmd[option.param]
var addToParams = true
if (paramValue && option.isJsonString) {
try {
var jsonObj = JSON.parse(paramValue)
paramValue = jsonObj
if (option.isWrapperParameter) {
params = Object.assign(params, paramValue) // if this a wrapper for real parameters (like --json) assign those
addToParams = false
}
} catch (e) {
print.error('Failed to parse json string for argument ' + option.param + '. Please verify the json is correctly structured and has quotes around both keys and values.')
process.exit(1)
}
}
var cosmicParamName = option.cosmicParamName || option.param
if (addToParams) { // add the option directly to params unless a special case occurred that flipped addToParams to false
params[cosmicParamName] = paramValue
}
})
if (argObj && argObj.arg) {
params[argObj.argumentParamName] = argObj.arg
}
return params
}