-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstably.js
More file actions
173 lines (146 loc) · 5.37 KB
/
Copy pathstably.js
File metadata and controls
173 lines (146 loc) · 5.37 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
* @TODO
* 1. [v] -min 최소 작동시간 체크 후 유효값보다 작으면 프로그램 종료
* 2. [v] -refresh 소스코드 변경시 자동재시작 토글 기능 추가
* 3. [v] -delay 재시작시 기다리는 기능 추가
* 4. [v] 안전한 재부팅 시그널
* 5. [ ] -l --log
* 6. [ ] 이용자가 Ctrl+C 로 끌경우 감지(현재 맥만 작동)
* 7. [ ] 정기보고?
*/
const fs = require('fs')
const path = require('path')
const nodemon = require('nodemon')
const cli = require('nodemon/lib/cli')
const bus = require('nodemon/lib/utils/bus')
const config = require('nodemon/lib/config')
const strErrParser = require('string-error-parse')
const folderLogger = require('folder-logger')
// Main Logger
var logger = null
// Here is the most recent error string.
var lastError = null
// Obtain stdio data from the child process.
bus.on('stdout', (text)=>{
if(logger === null){
console.log(String(text))
}else{
logger.info(String(text), {noFormat: true, noWrite: true})
logger.info(String(text), {noPrint: true})
}
/**
* @TODO 일반 로그 이벤트발생
*/
})
bus.on('stderr', (text)=>{
lastError = String(text)
/**
* @TODO 크래시 로그 이벤트발생
*/
})
// Stably Main Function
const stably = (command, option, program)=>{
let initTimestamp = new Date().getTime()
let nodemonOption = cli.parse(`nodemon ${command}`)
// Additional Exec(ts-node) support
let parseExec = command.split(' ')
if(parseExec.length >= 2 && (nodemonOption.script == parseExec[1]))
nodemonOption.exec = parseExec[0]
if(option.ignore) nodemonOption.ignore = ['*']
nodemonOption.ext = option.external.split(',').join(' ')
nodemonOption.stdout = false
nodemon(nodemonOption)
if(option.consoleLog || option.errorLog){
if(logger === null)
logger = new folderLogger(path.join(process.cwd(), option.logPath))
}
let restartSigned = false
// Nodemon EventHandler
nodemon.on('start', () => {
if(option.terminalUse){
logger.warn('App has been started.')
console.log(' ')
}
}).on('quit', () => {
if(option.terminalUse)
logger.warn('Program terminated successfully (Quit).')
process.exit()
}).on('exit', ()=>{
if(!restartSigned){
if(option.terminalUse)
logger.warn('Program terminated successfully. (Exit)')
/**
* @TODO 정상종료 이벤트 발생
*/
process.exit()
}else{
restartSigned = false
}
}).on('restart', (files) => {
if(option.terminalUse)
logger.warn(`Program restarted due to: ${files}`)
restartSigned = true
}).on('crash', ()=>{
nodemon.removeAllListeners()
let throwCommand = lastError
let parsedError = null
try{
parsedError = strErrParser(lastError)
throwCommand = String(parsedError.main.text).toLowerCase()
}catch(e){
throwCommand = `${throwCommand.replace(/[\r\n\t\b]/gi, '')}`
}
let crashTimestamp = new Date().getTime()
let crashTimeCheck = crashTimestamp - initTimestamp
if(crashTimeCheck < option.min){
logger.critical(String(lastError), {noFormat: true})
if(option.terminalUse)
logger.critical(`Program has been terminated, It failed fill in the minimum Op-time (${crashTimeCheck}ms < ${option.min}ms)`)
setTimeout(()=>{
process.exit()
}, 3000)
}else{
// Processing App Commands.
switch(throwCommand){
case option.signalReboot:
if(option.terminalUse)
logger.warn('Program has requested a reboot...')
break
case option.signalShutdown:
if(option.terminalUse){
logger.warn('Program has requested a shutdown...')
logger.warn('Program terminated successfully.')
}
process.exit()
/**
* @TODO 정상종료 이벤트
*/
break
default:
if(option.terminalUse){
logger.error('An error occurred in the program.')
if(parsedError !== null){
logger.error(`Error: ${parsedError.main.text}`)
logger.error(`Line: ${parsedError.main.lineText}`)
logger.error(`Location: ${parsedError.main.fileName}:${parsedError.main.lineNumber}\n`)
}
}
logger.error(String(lastError), {noFormat: true, noPrint: true})
/**
* @TODO 크래시 이벤트
*/
break
}
if(option.delay != 0){
if(option.terminalUse)
logger.warn(`App will be restart in ${option.delay} ms...`)
}
setTimeout(()=>{
if(option.terminalUse)
logger.warn('Reboot..')
stably(command, option, program)
}, option.delay)
}
})
}
module.exports = stably