-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.js
67 lines (55 loc) · 1.53 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
#!/usr/bin/env node
'use strict'
const Table = require('cli-table2')
const childProcess = require('child_process')
const tmp = require('tmp')
const fs = require('fs')
const trim = require('trim-newlines')
const table = (cols = []) => new Table({
head: [],
chars: {
top: '', 'top-mid': '', 'top-left': '', 'top-right': '',
bottom: '', 'bottom-mid': '', 'bottom-left': '', 'bottom-right': '',
left: '', 'left-mid': '', mid: '', 'mid-mid': '',
right: '', 'right-mid': '', middle: ' '
},
style: {'padding-left': 0, 'padding-right': 0},
colWidths: cols, wordWrap: true
})
const newline = /(\n|\r\n|\r)/
const spawn = (cmd, args, stdin, stdout) => new Promise((yay, nay) => {
const proc = childProcess.spawn(cmd, args, {
stdio: [stdin, stdout, 'ignore']
})
proc.on('close', (code) => {
if (code === 0) yay()
else nay()
})
proc.on('error', () => yay())
})
const tmpFile = () => new Promise((yay, nay) => {
tmp.tmpName((err, path) => {
if (err) nay(err)
else yay(path)
})
})
const readFile = (path) => new Promise((yay, nay) => {
fs.stat(path, (err, stats) => {
if (err) {
if (err.code === 'ENOENT') return yay(null)
return nay(err)
}
if (!stats.isFile()) return yay(null)
fs.readFile(path, 'utf8', (err, data) => {
if (err) return nay(err)
yay(data)
})
})
})
const writeFile = (path, data) => new Promise((yay, nay) => {
fs.writeFile(path, data, (err) => {
if (err) return nay(err)
yay()
})
})
module.exports = {table, newline, spawn, tmpFile, readFile, writeFile}