-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
108 lines (102 loc) · 3.29 KB
/
cli.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
#!/usr/bin/env node
const yargs = require('yargs');
const path = require('path');
const fs = require('fs');
const isJSON = text => {
try {
JSON.parse(text);
return true;
} catch {
return false;
}
};
if (
!fs.existsSync(path.join(__dirname, 'tokens.json')) ||
!isJSON(fs.readFileSync(path.join(__dirname, 'tokens.json')))
)
fs.writeFileSync(path.join(__dirname, 'tokens.json'), '{}', 'utf-8');
const commands = {
add: require(path.join(__dirname, 'lib', 'add.command')),
list: require(path.join(__dirname, 'lib', 'list.command')),
listen: require(path.join(__dirname, 'lib', 'listen.command')),
remove: require(path.join(__dirname, 'lib', 'remove.command')),
edit: require(path.join(__dirname, 'lib', 'edit.command')),
};
global.getAllTokens = () => require(path.join(__dirname, 'tokens.json'));
global.syncTokens = obj => fs.writeFileSync(path.join(__dirname, 'tokens.json'), JSON.stringify(obj, null, 2), 'utf-8');
yargs
.scriptName('mungrok')
.usage('$0 [command] [options/arguments]')
.command(
['add <account_name> <auth_token>', 'a'],
'Add new account',
ys => {
ys.positional('account_name', {
describe: 'Ngrok username',
type: 'string',
}).positional('auth_token', {
describe: 'Ngrok auth token',
type: 'string',
});
},
commands.add
)
.command(
['remove <account_name>', 'rm'],
'Remove account',
ys => {
ys.positional('account_name', {
describe: 'Ngrok account to be removed',
type: 'string',
});
},
commands.remove
)
.command(['list', 'ls'], 'List existing accounts', {}, commands.list)
.command(
['listen [account_name]', 'l'],
'Create tunnel',
ys => {
ys.positional('account_name', {
describe: 'Select specified account to use, leave it to use randomized one',
type: 'string',
})
.option('protocol', {
alias: 'p',
type: 'string',
choices: ['http', 'tcp', 'tls'],
default: 'http',
describe: 'Tunnel protocol',
})
.option('address', {
alias: 'a',
type: 'string',
default: '80',
describe: 'Local address',
})
.option('region', {
alias: 'r',
type: 'string',
choices: ['us', 'eu', 'ap', 'au'],
default: 'ap',
describe: 'Ngrok server location',
});
},
commands.listen
)
.command(
['edit <account_name> <auth_token>', 'e'],
'Edit auth token of existing account',
ys => {
ys.positional('account_name', {
describe: "Saved account's name",
type: 'string',
}).positional('auth_token', {
describe: 'Ngrok auth token',
type: 'string',
});
},
commands.edit
)
.strict()
.parse(process.argv.slice(2));