UNPKG

majas

Version:

Format-agnostic structure converter.

26 lines (25 loc) 752 B
import fs from 'fs-extra'; import p from 'path'; export function write(tree, path) { if (typeof tree === 'string') { fs.writeFileSync(path, tree); return; } fs.ensureDirSync(path); for (const [name, child] of tree) { write(child, p.resolve(path, name)); } } export function read(item, encoding) { const [path, stat] = typeof item === 'string' ? [item, fs.statSync(item)] : [p.resolve(item.parentPath, item.name), item]; if (!stat.isDirectory()) { return fs.readFileSync(path, encoding); } const node = new Map(); for (const ent of fs.readdirSync(path, { encoding, withFileTypes: true })) { node.set(ent.name, read(ent, encoding)); } return node; }