2021-06-18 15:03:02 +00:00
|
|
|
var fs = require('fs')
|
|
|
|
const readline = require('readline')
|
|
|
|
const { once } = require('events')
|
|
|
|
var exec = require('child_process').exec
|
|
|
|
|
|
|
|
function bufferFile(path) {
|
2021-06-18 17:29:36 +00:00
|
|
|
try {
|
|
|
|
return fs.readFileSync(path, { encoding: 'utf8' })
|
2021-12-08 18:28:40 +00:00
|
|
|
} catch (_e) {
|
2021-06-18 17:29:36 +00:00
|
|
|
return ''
|
|
|
|
}
|
2021-06-18 15:03:02 +00:00
|
|
|
}
|
|
|
|
|
2021-06-18 15:43:51 +00:00
|
|
|
async function head (path, lines) {
|
2021-06-18 15:03:02 +00:00
|
|
|
let x = 0
|
2021-12-08 18:28:40 +00:00
|
|
|
const acc = []
|
2021-06-18 15:03:02 +00:00
|
|
|
const readInterface = readline.createInterface({
|
|
|
|
input: fs.createReadStream(path),
|
|
|
|
terminal: false,
|
|
|
|
crlfDelay: Infinity
|
|
|
|
})
|
|
|
|
readInterface.on('line', function(line) {
|
|
|
|
acc.push(line)
|
|
|
|
x++
|
2021-06-18 15:43:51 +00:00
|
|
|
if (lines && x >= lines) {
|
2021-06-18 15:03:02 +00:00
|
|
|
readInterface.close()
|
|
|
|
readInterface.removeAllListeners()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
await once(readInterface, 'close')
|
|
|
|
return acc
|
|
|
|
}
|
|
|
|
|
|
|
|
function wc (path) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
exec("sed -n '$=' " + path, function (error, results) {
|
|
|
|
if (error) {
|
|
|
|
reject(error)
|
|
|
|
} else {
|
|
|
|
resolve(results.toString().trim())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-10-14 18:29:03 +00:00
|
|
|
function escape (text) {
|
|
|
|
return text.replace('&', '&').replace('<', '<').replace('>', '>').replace('"', '"').replace("'", ''')
|
|
|
|
}
|
|
|
|
|
2021-06-18 15:03:02 +00:00
|
|
|
module.exports = {
|
|
|
|
bufferFile: bufferFile,
|
|
|
|
head: head,
|
2021-10-14 18:29:03 +00:00
|
|
|
wc: wc,
|
|
|
|
escape: escape
|
2021-06-18 15:03:02 +00:00
|
|
|
}
|