UNPKG

@bevry/fs-tree

Version:

Read and write to the filesystem as a tree, useful for quick scaffolding and comparisons of filesystem directories

29 lines (28 loc) 863 B
// builtin import { join } from 'path'; // external import { isPlainObject } from 'typechecker'; import write from '@bevry/fs-write'; import { scanDirectory, toTree, } from 'scandirectory'; /** Read a directory to a tree */ export async function readTree(directory, opts = { encoding: 'utf8', }) { const results = await scanDirectory({ ...opts, directory, includeRoot: true }); return toTree(results); } /** Write a tree to a directory */ export async function writeTree(directory, tree, opts = { encoding: 'utf8', }) { for (const basename of Object.keys(tree)) { const value = tree[basename]; const path = join(directory, basename); if (isPlainObject(value)) { await writeTree(path, value, opts); } else { await write(path, value === true ? '' : value, opts); } } }