-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.js
121 lines (102 loc) · 3.48 KB
/
helpers.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
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
const { exec } = require('child_process')
const os = require('os')
const fs = require('fs')
const crypto = require('crypto')
const mkdirp = require('mkdirp')
const which = require('which')
const {dirname} = require('path')
const diff = require('diff')
const yaml = require('yaml')
const ora = require('ora')
global.APPNAME = 'rconf'
const joinPath = require('path').join
global.DATADIR = {
'win32': joinPath(process.env.APPDATA || '', APPNAME),
'darwin': joinPath(os.homedir(), 'Library', 'Application Support', APPNAME),
'linux': joinPath(os.homedir(), '.'+APPNAME)
}[os.platform()]
const defaultShell = () => {
if (os.platform() == 'win32') return process.env.ComSpec
return 'bash'
}
const getDiff = (f1, f2) => {
const differences = diff.diffLines(f1, f2);
let line = 1
const changes = []
differences.forEach(part => {
if (part.added) {
// const l = last(changes)
// if (l.remove && l.line == line) {
// changes.push({modify: [l.remove, part.value], line})
// delete changes[changes.length-2]
// } else
changes.push({add: part.value, line})
line += part.count
}
else if (part.removed) {
changes.push({remove: part.value, line})
}
else {
// console.log(`Unchanged: ${part.value}`);
line += part.count
}
});
return reject(isNil, changes)
}
const every = (ms, fn) => {
fn()
return setInterval(fn, ms)
}
const languages = reject(isNil, values(mapObjIndexed((v,name) => {
const m = concat(v.extensions || [], v.filenames || [])
if (isEmpty(m)) return
return [name.toLowerCase(), v.color, new RegExp(m.map(x => x.replace('.', '\\.').replace(/\+/gim, '\\+')).join('$|')+'$' || 'dont match anything', 'gim')]
}, require('./languages.json'))))
const detectLanguage = file => {
return (languages.find(x => x[2].test(file)) || ['yaml', 'orange'])
}
const run = async (commands, log = () => {}, verbose=true) => {
let last = Promise.resolve()
for (const command of coerceArray(commands)) {
const spinner = Spinner({text: 'run: '+command})
log({status: 'inprogress'})
if (verbose) spinner.start()
last = await new Promise((r,j) => exec(command, {shell: defaultShell()}, (error, stdout, stderr) => {
if (verbose) {
spinner[error ? 'fail' : 'succeed']()
console.log(stdout, stderr)
}
juxt([r, log])({stdout, stderr, status: error ? 'error' : 'ok'})
}))
}
return last
}
const calculateHash = (obj) => {
const hash = crypto.createHash('sha256')
hash.update(JSON.stringify(obj))
return hash.digest('hex')
}
const coerceArray = x => unless(is(Array), of, x)
const getIPV4Interfaces = (match = '.*') =>
reject(isNil, mapObjIndexed((interfaces, name)=> {
const interface = find(x => x.family == 'IPv4' && !x.internal && new RegExp(match).test(name), interfaces)
if (!interface) return null
interface.name = name
return interface
}, os.networkInterfaces()))
const pp = (x) => {
console.log(yaml.stringify(x))
return x
}
const Spinner = compose(ora, mergeRight({
text: 'connecting',
color: 'green',
spinner: {interval: 380, frames: [ "⣾", "⣽", "⣻", "⢿", "⡿", "⣟", "⣯", "⣷" ] }
}))
const notSoSafeEval = code =>
new Function('rconf', 'process', 'require', code)({
env: process.env,
interfaces: getIPV4Interfaces(),
// os: require('os'),
})
module.exports = {getDiff, every, detectLanguage, joinPath, run, calculateHash, coerceArray, which, mkdirp, dirname, fs, os, getIPV4Interfaces, pp, Spinner, notSoSafeEval}