Skip to content

Instantly share code, notes, and snippets.

@xixilive
Last active March 9, 2019 14:31
Show Gist options
  • Save xixilive/18beb61f1bf3d06f7f6e9992633996ac to your computer and use it in GitHub Desktop.
Save xixilive/18beb61f1bf3d06f7f6e9992633996ac to your computer and use it in GitHub Desktop.
utils for node fs
// utitlies module
const path = require('path')
const fs = require('fs')
const noop = () => {}
const debug = process.env.DEBUG ? console.log : noop
// lang functions
const toString = Object.prototype.toString
const typeChecker = (expect) => (val) => toString.call(val) === `[object ${expect}]`
const isArr = typeChecker('Array')
const isObj = typeChecker('Object')
const isStr = typeChecker('String')
const isFn = typeChecker('Function')
const isNum = (val) => !isNaN(val) && typeChecker('Number')(val)
const isDate = typeChecker('Date')
const isUndef = typeChecker('Undefined')
const isNull = typeChecker('Null')
const isNil = val => (val === null || val === undefined)
function promisify(fn){
return function(...args){
return new Promise((resolve, reject) => {
args.push((err, result) => {
err ? reject(err) : resolve(result)
})
fn(...args)
})
}
}
// catch and supress errors for sync function calls
function supressify(fn, fallback = null){
return (...args) => {
try{
return fn(...args)
}catch(e){
debug(e)
return fallback
}
}
}
// json
const fromJson = json => {
try{
return JSON.parse(json)
}catch(e){
debug(e)
return undefined
}
}
const toJson = obj => {
try{
return JSON.stringify(obj)
}catch(e){
debug(e)
return null
}
}
// filesystem functions
const fileMode = (flag) => {
flag = 'string' === typeof flag ? flag.trim().toLowerCase() : ''
let mode = fs.constants.F_OK
if(flag.indexOf('r') > -1){
mode |= fs.constants.R_OK
}
if(flag.indexOf('w') > -1){
mode |= fs.constants.W_OK
}
return mode
}
const access = promisify(fs.access)
const accessSync = supressify(fs.accessSync)
const readFile = promisify(fs.readFile)
const readFileSync = supressify(fs.readFileSync)
const writeFile = promisify(fs.writeFile)
const writeFileSync = supressify(fs.writeFileSync)
const unlink = promisify(fs.unlink)
const unlinkSync = supressify(fs.unlinkSync)
const checkFile = (file, expect = '') => access(file, fileMode(expect)).then(() => true, () => false)
const checkFileSync = (file, expect = '') => isUndef(accessSync(file, fileMode(expect)))
const mkdirSync = (dir) => {
try{
if(isUndef(accessSync(dir))){
return -1
}
fs.mkdirSync(dir, {recursive: true, mode: 0o755})
return 1
}catch(e){
debug(e)
return 0
}
}
const mkdir = (dir) => Promise.resolve(mkdirSync(dir))
const readJson = (file) => readFile(file, 'utf-8').then(fromJson)
const readJsonSync = (file) => fromJson(readFileSync(file, 'utf-8'))
const writeJson = (file, data) => {
const json = toJson(data)
if(isNull(json)){
return Promise.reject('fail to serialize data to JSON')
}
return writeFile(file, json, 'utf-8')
}
const writeJsonSync = (file, data) => {
const json = toJson(data)
if(isNull(json)){
return false
}
return isUndef(writeFileSync(file, json, 'utf-8'))
}
module.exports = {
noop, debug,
isArr, isObj, isStr, isFn, isNum, isDate, isUndef, isNull, isNil,
access, accessSync,
readFile, readFileSync,
writeFile, writeFileSync,
checkFile, checkFileSync,
mkdir, mkdirSync,
unlink, unlinkSync,
readJson, readJsonSync,
writeJson, writeJsonSync
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment