attempt fix for nohup problem https://github.com/rejetto/hfs/issues/74

Massimo Melina committed Dec 24, 2022 at 19:21 UTC 9357a5c6f8589840caf97b3c44c573f0cab21086
1 file changed +16 -3
src/commands.ts
+16 -3
@@ -5,9 +5,22 @@ import { getUpdate, update } from './update'
5 import { openAdmin } from './listen'
6 import yaml from 'yaml'
7 import { BUILD_TIMESTAMP, VERSION } from './const'
8 +import { createInterface } from 'readline'
9
9 -console.log(`HINT: type "help" for help`)
10 -require('readline').createInterface({ input: process.stdin }).on('line', (line: string) => {
10 +try {
11 + /*
12 + is this try-block useful in case the stdin is unavailable?
13 + Not sure, but someone reported a problem using nohup https://github.com/rejetto/hfs/issues/74
14 + and I've found this example try-catching https://github.com/DefinitelyTyped/DefinitelyTyped/blob/dda83a906914489e09ca28afea12948529015d4a/types/node/readline.d.ts#L489
15 + */
16 + createInterface({ input: process.stdin }).on('line', parseCommandLine)
17 + console.log(`HINT: type "help" for help`)
18 +}
19 +catch {
20 + console.log("console commands not available")
21 +}
22 +
23 +function parseCommandLine(line: string) {
24 if (!line) return
25 const [name, ...params] = line.trim().split(/ +/)
26 const cmd = (commands as any)[name]
@@ -22,7 +35,7 @@ require('readline').createInterface({ input: process.stdin }).on('line', (line:
35 else
36 throw err
37 })
25 -})
38 +}
39
40 const commands = {
41 help: {