-
Notifications
You must be signed in to change notification settings - Fork 1
/
bin.js
executable file
·140 lines (130 loc) · 3.09 KB
/
bin.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env node
'use strict'
const meow = require('meow')
const pm2 = require('pm2')
const cli = meow(
`
Usage
$ wol-server start|stop|startup|unstartup
Options
--port, -p Specify a port to run the server on
Examples
$ wol-server start
> WOL Server now running on default port 3078
$ wol-server start --port 9000
> WOL Server now running on port 9000
$ wol-server stop
> WOL Server stopped running
$ sudo wol-server startup
> WOL Server now runs on startup running
$ sudo wol-server unstartup
> WOL Server no longer runs on startup
`,
{
flags: {
port: {
type: 'number',
alias: 'p'
}
}
}
)
if (cli.input.length === 0) {
return cli.showHelp()
}
const [startOrStop] = cli.input
pm2.connect(function(err) {
if (err) {
console.error(err)
process.exit(2)
}
if (startOrStop === 'start') {
pm2.start(
__dirname + '/index.js',
{
scriptArgs: [cli.flags.port]
},
(err, app) => {
pm2.disconnect()
if (err) {
if (Array.isArray(app) && app.length) {
return console.log('Server is already running')
}
return console.error('Unable to start server')
}
console.log(`WOL Server running on ${cli.flags.port || 3078}`)
}
)
} else if (startOrStop == 'startup') {
if (process.getuid() != 0) {
pm2.disconnect()
return console.log(
'You have to run this as root to get startup to work properly:\n\n sudo wol-server startup'
)
}
pm2.startup(
undefined,
{
args: [
{},
{
name() {
return 'start'
}
}
]
},
(err, proc) => {
pm2.disconnect()
if (err) {
if (!proc) {
return console.log('Server was not running')
}
console.error('Unable to setup startup script for server')
}
console.log(`WOL Server running on startup now`)
}
)
} else if (startOrStop == 'unstartup') {
if (process.getuid() != 0) {
pm2.disconnect()
return console.log(
'You have to run this as root to get this to not run on startup:\n\n sudo wol-server unstartup'
)
}
pm2.uninstallStartup(
undefined,
{
args: [
{},
{
name() {
return 'start'
}
}
]
},
(err, proc) => {
pm2.disconnect()
if (err) {
if (!proc) {
return console.log('Server was not running')
}
console.error('Unable to stop startup script for server')
}
console.log(`WOL Server no longer running on startup`)
}
)
} else {
pm2.stop('index', (err, proc) => {
pm2.disconnect()
if (err) {
if (!proc) {
return console.log('Server was not running')
}
return console.error('Unable to stop server')
}
console.log(`WOL Server successfully stopped running`)
})
}
})