Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const daggy = require('daggy');
- const Task = require('data.task');
- const path = require('path');
- const fs = require('fs');
- const { sequence } = require('ramda');
- // type Entity
- // = File String
- // | Dir String (List Entity)
- const Entity = daggy.taggedSum('Entity', {
- File: ['name'],
- Dir: ['name', 'items'],
- });
- Entity.prototype.inspect = function inspect() {
- return this.cata({
- File: name => `File(${name})`,
- Dir: (name, items) => `Dir(${name}, ${items.map(x => x.inspect())})`,
- });
- };
- // -- isDir :: Path -> Task e Bool
- const isDir = entityPath =>
- new Task((rej, res) =>
- fs.stat(entityPath, (err, stats) => {
- return err ? rej(err) : res(stats.isDirectory());
- }));
- // -- readdir :: Path -> Task e [String]
- const readdir = dirPath =>
- new Task((rej, res) =>
- fs.readdir(dirPath, (err, items) => {
- return err ? rej(err) : res(items);
- }));
- // -- itemToEntity :: (String, Path) -> Bool -> Task e Entity
- const itemToEntity = (item, newPath) => (isItDir) => {
- return isItDir
- ? readDir2TaskListEntity(newPath).map(items => Entity.Dir(item, items))
- : Task.of(Entity.File(item));
- };
- // -- items2Entities :: (Path, List String) -> List (Task e Entity)
- const listItem2ListTaskEntity = (dirPath, items) =>
- items.map((item) => {
- const newPath = path.join(dirPath, item);
- return isDir(newPath)
- .chain(itemToEntity(item, newPath));
- });
- // -- readDir2TaskListEntity :: Path -> Task e (List Entity)
- const readDir2TaskListEntity = dirPath =>
- readdir(dirPath)
- .chain(items => sequence(Task.of, listItem2ListTaskEntity(dirPath, items)));
- // -- main :: String -> Path -> Entity
- const main = (dirName, rootPath) =>
- readDir2TaskListEntity(path.join(rootPath, dirName))
- .map(items => Entity.Dir(dirName, items));
- // Program
- main('test', __dirname).fork(
- console.log,
- console.error
- );
Add Comment
Please, Sign In to add comment