Skip to content

Commit 3d30d85

Browse files
JohanObrinkraduachim
authored andcommitted
feat: 🎸 added subcommander to make help nicer (#1)
* feat: 🎸 added subcommander to make help nicer
1 parent 5aa64fe commit 3d30d85

6 files changed

Lines changed: 1166 additions & 516 deletions

File tree

lib/common.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function exitOnError (error) {
2+
if (error) {
3+
console.error(error)
4+
process.exit(1)
5+
}
6+
}
7+
8+
module.exports = {
9+
exitOnError
10+
}
Lines changed: 23 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
1-
#!/usr/bin/env node
21
const { spawn, exec } = require('child_process')
32
const { address } = require('ip')
43
const cc = require('camelcase')
5-
const argv = require('minimist')(process.argv.slice(2))
4+
const { exitOnError } = require('./common')
65

7-
const main = argv._[0]
8-
9-
switch (main) {
10-
case 'start':
11-
start()
12-
break
13-
case 'stop':
14-
stop()
15-
break
16-
default:
17-
help()
18-
break
6+
function parseDockerPs (table) {
7+
const rows = table
8+
.split('\n')
9+
.map((line) =>
10+
line.split(' ').map(w => w.trim()).filter(w => w))
11+
.filter((row) => row && row.length)
12+
.map((row) =>
13+
row.map(word => word.trim())
14+
)
15+
const names = rows.shift().map((word) => cc(word))
16+
return rows.map((values) =>
17+
values.reduce((res, val, ix) => ({
18+
...res,
19+
[names[ix]]: val
20+
}), {}))
1921
}
2022

21-
function start () {
23+
const rxCliContainer = /cli_/
24+
function start ({ attach }) {
2225
const command = `docker-compose`
2326
const args = ['up']
2427

25-
if (!argv.attach) { args.push('-d') }
28+
if (!attach) { args.push('-d') }
2629

2730
const HOST_IP = address()
2831
const options = {
@@ -40,8 +43,6 @@ function start () {
4043
pcs.on('error', exitOnError)
4144
}
4245

43-
const rxCliContainer = /cli_/
44-
4546
function stop () {
4647
exec('docker ps', (error, stdout, stderr) => {
4748
exitOnError(error)
@@ -55,7 +56,7 @@ function stop () {
5556
process.exit(0)
5657
}
5758

58-
59+
5960
console.log(cliContainers.map(c => 'Stopping ' + c.names).join('\n'))
6061
const args = ['stop'].concat(cliContainers.map(c => c.containerId))
6162
const options = {
@@ -73,30 +74,7 @@ function stop () {
7374
})
7475
}
7576

76-
function help () {
77-
console.log('help')
78-
}
79-
80-
function parseDockerPs (table) {
81-
const rows = table
82-
.split('\n')
83-
.map((line) =>
84-
line.split(' ').map(w => w.trim()).filter(w => w))
85-
.filter((row) => row && row.length)
86-
.map((row) =>
87-
row.map(word => word.trim())
88-
)
89-
const names = rows.shift().map((word) => cc(word))
90-
return rows.map((values) =>
91-
values.reduce((res, val, ix) => ({
92-
...res,
93-
[names[ix]]: val
94-
}), {}))
95-
}
96-
97-
function exitOnError (error) {
98-
if (error) {
99-
console.error(error)
100-
process.exit(1)
101-
}
77+
module.exports = {
78+
start,
79+
stop
10280
}

lib/main.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env node
2+
const sc = require('subcommander')
3+
const pkg = require(`${__dirname}/../package.json`)
4+
const tunnel = require('./tunnel')
5+
const infra = require('./infra')
6+
7+
sc
8+
.command('tunnel', {
9+
desc: 'expose local egendataserver to operator',
10+
callback: tunnel.start
11+
})
12+
.option('port', {
13+
abbr: 'p',
14+
description: 'local port',
15+
default: '4000'
16+
})
17+
.option('subdomain', {
18+
description: 'requested subdomain of localtunnel.me',
19+
default: pkg.name.replace(/[-_\@\/]/g, '')
20+
})
21+
sc
22+
.command('infra', { desc: 'manage local egendata infrastructure' })
23+
.command('start', {
24+
desc: 'start local infrastructure for egendata',
25+
callback: infra.start
26+
})
27+
.option('attach', {
28+
flag: true
29+
})
30+
.end()
31+
.command('stop', {
32+
desc: 'stop all running local egendata infrastructure',
33+
callback: infra.stop
34+
})
35+
.end()
36+
37+
sc.parse()

lib/tunnel.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const localtunnel = require('localtunnel')
2+
const { spawn } = require('child_process')
3+
const { exitOnError } = require('./common')
4+
5+
async function start ({ port, subdomain }) {
6+
console.log('tunnel start', port, subdomain)
7+
try {
8+
const tunnel = await localtunnel({ port, subdomain })
9+
10+
console.log(`Opening tunnel on ${tunnel.url}`)
11+
12+
// tunnel.on('request', (info) => console.log(info))
13+
tunnel.on('error', (error) => console.log(error))
14+
15+
const ls = spawn('npm', ['run', 'dev'], {
16+
env: {
17+
...process.env,
18+
NODE_ENV: 'development',
19+
CLIENT_ID: tunnel.url,
20+
OPERATOR_URL: 'https://operator-test.dev.services.jtech.se/api',
21+
PORT: port
22+
},
23+
stdio: 'pipe'
24+
})
25+
ls.stdout.on('data', (data) => process.stdout.write(data))
26+
ls.stderr.on('data', (data) => process.stderr.write(data))
27+
ls.on('error', exitOnError)
28+
ls.on('close', () => process.stdout.write('\n'))
29+
} catch (error) {
30+
exitOnError(error)
31+
}
32+
}
33+
34+
module.exports = {
35+
start
36+
}

0 commit comments

Comments
 (0)