-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcbpro-cli.js
More file actions
executable file
·72 lines (70 loc) · 1.76 KB
/
cbpro-cli.js
File metadata and controls
executable file
·72 lines (70 loc) · 1.76 KB
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
#!/usr/bin/env node
const os = require('os');
const path = require('path');
const minimst = require('minimist');
const dotenv = require('dotenv');
const CoinbasePro = require('coinbase-pro');
const prodUri = 'https://api.pro.coinbase.com';
const argv = minimst(process.argv.slice(2));
const home = os.homedir();
const file = path.join(home, '.cbpro', 'config.env');
dotenv.config({
path: file
});
const apiUri = process.env.CBPRO_APIURI || prodUri;
const client = new CoinbasePro.AuthenticatedClient(
process.env.CBPRO_KEY,
process.env.CBPRO_SECRET,
process.env.CBPRO_PASSPHRASE,
apiUri
);
(async () => {
const args = argv['_'];
if (!Array.isArray(args) || !(args.length > 0)) {
console.error(JSON.stringify({
type: 'UsageError',
message: 'Invalid Usage.'
}, null, 2));
process.exit(1);
}
const cmd = args[0];
const meth = client[cmd];
if (!meth) {
console.error(JSON.stringify({
type: 'UsageError',
message: `${cmd} is not a valid command.`
}, null, 2));
process.exit(1);
}
const params = args.slice(1);
const parsed = [];
for (let i = 0; i < params.length; i++) {
try {
const json = JSON.parse(params[i]);
parsed.push(json);
} catch {
parsed.push(params[i]);
}
}
const resp = await meth.apply(client, parsed);
if (resp) {
console.log(JSON.stringify(resp, null, 2));
}
})().catch(err => {
const resp = err && err.response;
const status = resp && resp.statusCode;
const data = err && err.data;
if (status && data) {
console.error(JSON.stringify({
type: 'ApiError',
statusCode: status,
data: data
}, null, 2));
} else {
console.error(JSON.stringify({
type: 'Error',
message: err.message || ''
}));
}
process.exit(1);
});