UNPKG

2.03 kBJavaScriptView Raw
1#!/usr/bin/env node
2'use strict'
3
4const { EventEmitter } = require('events')
5EventEmitter.defaultMaxListeners = 50
6
7/**
8 * Project dependencies
9 */
10const semver = require('semver')
11const chalk = require('chalk')
12const nodeVersion = require('./lib/node.version')
13const Screen = require('./src/screen')
14const DockerUtil = require('./src/dockerUtil')
15const cli = require('./src/cli')
16
17const cliOptions = cli.cliParse()
18if (cliOptions.help) {
19 cli.showUsage()
20 process.exit(0)
21}
22
23if (cliOptions.version) {
24 cli.showVersion()
25 process.exit(0)
26}
27
28if (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
34initDockerConnection()
35 .then(initScreens)
36 .then(function (screen) {
37 process.on('uncaughtException', (err) => {
38 // Make sure the screen is cleared
39 screen.teardown()
40 exitError(err)
41 })
42 })
43 .catch((err) => {
44 return exitError(err)
45 })
46
47function 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
72function 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
86function 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}