Created
February 27, 2019 03:54
-
-
Save grmartin/ca320be2de5ee928f8fdab739c967417 to your computer and use it in GitHub Desktop.
Store Directory Data for Mocking
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For Node.js | |
import fs from 'fs'; | |
import path from 'path'; | |
import vm from 'vm'; | |
const vm_eval = (x)=>{ | |
return vm.runInNewContext(x); | |
}; | |
const lambda = 'λ'; | |
const basePath = '/home/grmartin'; | |
const rooter = (next) => path.join(basePath, next); | |
const defn = (obj) => { | |
const out = obj; | |
const fn = function (x) { return `${lambda}/${x}`; }; | |
obj.isBlockDevice = fn(obj.isBlockDevice()); | |
obj.isCharacterDevice = fn(obj.isCharacterDevice()); | |
obj.isDirectory = fn(obj.isDirectory()); | |
obj.isFIFO = fn(obj.isFIFO()); | |
obj.isFile = fn(obj.isFile()); | |
obj.isSocket = fn(obj.isSocket()); | |
obj.isSymbolicLink = fn(obj.isSymbolicLink()); | |
return out; | |
}; | |
const buildEntry = (filePath) => ({ | |
path: filePath, | |
stat: defn(fs.statSync(filePath)) | |
}); | |
const pretty= (x) => JSON.stringify(x, null, ' '); | |
const parser = (x) => JSON.parse(x, (() => { | |
const dt = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*))(?:Z|([+\-])([\d|:]*))?$/; | |
const fn = RegExp(`^${lambda}\/(.*?)$`); | |
const xforms = [ | |
(_, v) => (typeof v === 'string' && dt.test(v)) ? new Date(v) : v, | |
(_, v) => (typeof v === 'string' && fn.test(v)) ? vm_eval(`()=>(${fn.exec(v)[1]})`) : v, | |
]; | |
return (key, value) => { | |
for (let i = 0; i < xforms.length; i++) { | |
let out = xforms[i](key, value); | |
if (out !== value) { | |
return out; | |
} | |
} | |
return value; | |
} | |
})()); | |
const json = pretty(fs.readdirSync(basePath) | |
.map(rooter) | |
.map(buildEntry)) /*?*/ | |
const out = parser(json) /*?*/ | |
out[0].stat.isDirectory() /*?*/ | |
out[0].stat.isFile() /*?*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment