1 | #!/usr/bin/env node
|
2 | 'use strict'
|
3 |
|
4 | const { EventEmitter } = require('events')
|
5 | EventEmitter.defaultMaxListeners = 50
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | const semver = require('semver')
|
11 | const chalk = require('chalk')
|
12 | const nodeVersion = require('./lib/node.version')
|
13 | const Screen = require('./src/screen')
|
14 | const DockerUtil = require('./src/dockerUtil')
|
15 | const cli = require('./src/cli')
|
16 |
|
17 | const cliOptions = cli.cliParse()
|
18 | if (cliOptions.help) {
|
19 | cli.showUsage()
|
20 | process.exit(0)
|
21 | }
|
22 |
|
23 | if (cliOptions.version) {
|
24 | cli.showVersion()
|
25 | process.exit(0)
|
26 | }
|
27 |
|
28 | if (semver.lt(process.version, nodeVersion.minimum)) {
|
29 | cli.showUsage()
|
30 | console.log(chalk.red('unsupported node version, please use a version equal or greater than ' + nodeVersion.minimum))
|
31 | process.exit(0)
|
32 | }
|
33 |
|
34 | initDockerConnection()
|
35 | .then(initScreens)
|
36 | .then(function (screen) {
|
37 | process.on('uncaughtException', (err) => {
|
38 |
|
39 | screen.teardown()
|
40 | exitError(err)
|
41 | })
|
42 | })
|
43 | .catch((err) => {
|
44 | return exitError(err)
|
45 | })
|
46 |
|
47 | function initDockerConnection () {
|
48 | return new Promise((resolve, reject) => {
|
49 | let utils
|
50 | let docker
|
51 |
|
52 | try {
|
53 | docker = new DockerUtil(cli.cliParse())
|
54 | } catch (err) {
|
55 | return exitError(err)
|
56 | }
|
57 |
|
58 | docker.ping((err) => {
|
59 | if (err) {
|
60 | return reject(err)
|
61 | }
|
62 |
|
63 | utils = new Map([
|
64 | ['docker', docker]
|
65 | ])
|
66 |
|
67 | return resolve(utils)
|
68 | })
|
69 | })
|
70 | }
|
71 |
|
72 | function initScreens (utils) {
|
73 | return new Promise((resolve, reject) => {
|
74 | let screen
|
75 | try {
|
76 | screen = new Screen(utils)
|
77 | screen.init()
|
78 | } catch (err) {
|
79 | return reject(err)
|
80 | }
|
81 |
|
82 | return resolve(screen)
|
83 | })
|
84 | }
|
85 |
|
86 | function exitError (err) {
|
87 | cli.showUsage()
|
88 |
|
89 | if (err && err.message) {
|
90 | console.log('\x1b[31m')
|
91 | console.trace(err)
|
92 |
|
93 | if (err.message === 'Unable to determine the domain name') {
|
94 | console.log('-> check your connection options to the docker daemon and confirm containers exist')
|
95 | }
|
96 | console.log('\x1b[0m')
|
97 | }
|
98 |
|
99 | process.exit(-1)
|
100 | }
|