Repository navigation
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