从文件系统创建对象树

编程入门 行业动态 更新时间:2024-10-22 18:30:10
本文介绍了从文件系统创建对象树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要从CL参数获取目录的路径,并创建一个对象树,其中包含所选目录的文件结构.

I need to get the path of the directory from the CL argument and and create an object tree, which contains the file structure of the selected directory.

如果是文件,则其值应为true.如果是目录,我应该对该目录执行相同的操作(我认为最好的方法是递归).

If it's a file, it's value should be true. And if it's a directory I should do the same for that directory (I think the best approach is recursion).

输出应如下所示:

{ file.txt: true, directory: { one.txt: true, two.txt: true, ... } ... }

到目前为止,我尝试了该版本的递归版本,但失败了,不知道为什么.我认为这是因为我没有正确处理代码的异步部分.这是我的代码:

So far I tried the recursion version of that but it fails and don't know why. I think it's because I didn't handle the async part of my code properly. Here is my code:

const fs = require("fs"); const basePath = process.argv[2]; //Getting the path (it works) const result = {}; //Function to check my path is exist and it's a directory const isDirectory = path => { return new Promise((resolve, reject) => { fs.lstat(path, (err, stats) => { if (err) reject("No such file or Directory"); resolve(stats.isDirectory()); }); }); }; //Recursive function that should create the object tree of the file system const createTree = (path, target) => { return new Promise((resolve, reject) => { reject("Promise Oops..."); fs.readdir(path, (err, data) => { data.forEach(item => { const currentLocation = `${path}/${item}`; isDirectory(currentLocation) .then(isDir => { if (!isDir) { target[item] = true; return; } target[item] = {}; resolve(createTree(currentLocation, target[item])); }) .catch(err => console.log("Oops in then...")); }); }); }); }; //Consuming the createTree function (async () => { try { const res = await createTree(basePath, result); console.log(res); } catch (err) { console.log("Oops consume..."); } })();

推荐答案

您可以使用fs.promises代替基于fs的基于回调的方法,这样可以提高可读性并帮助您轻松发现错误. 您正在记录从createTree函数返回的未定义内容.您应该登录result对象以查看所需的结果.

You can use fs.promises instead of callback based methods of fs which improves the readability and helps you to find the mistakes easily. You are logging what is returned from createTree function which is undefined. You should log result object to see the result you want.

const fs = require("fs"); const basePath = process.argv[2]; //Getting the path (it works) const result = {}; //Function to check my path is exist and it's a directory const isDirectory = async (path) => { try { const stats = await fs.promises.lstat(path); return stats.isDirectory(); } catch (error) { throw new Error("No such file or Directory"); } }; //Recursive function that should create the object tree of the file system const createTree = async (path, target) => { const data = await fs.promises.readdir(path); for (const item of data) { const currentLocation = `${path}/${item}`; const isDir = await isDirectory(currentLocation); if (!isDir) { target[item] = true; continue; } target[item] = {}; await createTree(currentLocation, target[item]); } }; //Consuming the createTree function (async () => { try { await createTree(basePath, result); console.log(result); } catch (error) { console.log(error.message); } })();

更多推荐

从文件系统创建对象树

本文发布于:2023-05-28 06:54:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/315096.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件系统   对象

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!