-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
76 lines (66 loc) · 1.94 KB
/
util.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
#!/usr/bin/env node
let fs = require('fs')
// setup basic options `ioFV`
const program = require('commander')
.option('-i, --input [filename]', 'File to read input from')
.option('-o, --output [filename]', 'File to write output to')
.option('--force', 'Force file overwrite')
function exit_error(msg) {
console.error(msg);
process.exit(1);
}
function exit_success() {
process.exit(0);
}
function parse_json_or_fail (buf) {
try {
return JSON.parse(buf.toString('utf8'))
} catch (e) {
exit_error(`filed to parse JSON input: ${e.message}`)
}
}
// only load non-empty input
async function get_input () {
let rawInput = await get_raw_input()
if(rawInput.toString('utf8').trim() === "") {
exit_error('You must pass non empty input')
}
return rawInput
}
// reads the input from STDIN or file
async function get_raw_input () {
// read from input file
let fn = program.input
if (fn) {
if (!fs.existsSync(fn)) {
exit_error(`input file "${fn}" does not exist`)
}
// TODO check directory?
return fs.readFileSync(fn, {encoding: 'utf-8'})
} else {
// read from standard input and make UTF-8 string
return (await require("get-stdin").buffer()).toString()
}
}
// read the input as JSON
async function get_input_json () {
let input = await get_input()
let obj = parse_json_or_fail(input)
return obj
}
async function put_output (out) {
let fn = program.output
if (fn) {
if (fs.existsSync(fn) && !program.force) {
exit_error(`output file "${fn}" already exists`)
}
return fs.writeFileSync(fn, out)
} else {
// write to standard output
console.log(out)
}
}
async function put_output_json (out) {
return await put_output(JSON.stringify(out))
}
module.exports = { program, exit_error, exit_success, get_input, get_input_json, put_output, put_output_json }