@humanfs/node
以 尼古拉斯·扎卡斯
如果你觉得有用,请考虑支持我的工作 捐赠 或者说 提名我 为吉他之星.
说明
这个 hfs 用于Node.js和Node.js兼容运行时间的绑定.
[尖叫声] 这个项目是 实验 在v1.0.0之前可能会发生重大变化。 用你自己的谨慎,绝对不要生产!
安装
使用您最喜欢的软件包管理器安装 :
npm install @humanfs/node
# or
pnpm install @humanfs/node
# or
yarn add @humanfs/node
# or
bun install @humanfs/node
使用量
在您的工程中最简单的使用hfs方式是导入 hfs 对象 :
import { hfs } from "@humanfs/node";
然后,你可以使用 API 方法:
// 1. Files
// read from a text file
const text = await hfs.text("file.txt");
// read from a JSON file
const json = await hfs.json("file.json");
// read raw bytes from a text file
const arrayBuffer = await hfs.arrayBuffer("file.txt");
// write text to a file
await hfs.write("file.txt", "Hello world!");
// write bytes to a file
await hfs.write("file.txt", new TextEncoder().encode("Hello world!"));
// append text to a file
await hfs.append("file.txt", "Hello world!");
// append bytes to a file
await hfs.append("file.txt", new TextEncoder().encode("Hello world!"));
// does the file exist?
const found = await hfs.isFile("file.txt");
// how big is the file?
const size = await hfs.size("file.txt");
// when was the file modified?
const mtime = await hfs.lastModified("file.txt");
// copy a file from one location to another
await hfs.copy("file.txt", "file-copy.txt");
// move a file from one location to another
await hfs.move("file.txt", "renamed.txt");
// delete a file
await hfs.delete("file.txt");
// 2. Directories
// create a directory
await hfs.createDirectory("dir");
// create a directory recursively
await hfs.createDirectory("dir/subdir");
// does the directory exist?
const dirFound = await hfs.isDirectory("dir");
// copy the entire directory
hfs.copyAll("from-dir", "to-dir");
// move the entire directory
hfs.moveAll("from-dir", "to-dir");
// delete a directory
await hfs.delete("dir");
// delete a non-empty directory
await hfs.deleteAll("dir");
如果你想创建自己的实例,请导入 NodeHfs 构造器 :
import { NodeHfs } from "@humanfs/node";
import fsp from "fs/promises";
const hfs = new NodeHfs();
// optionally specify the fs/promises object to use
const hfs = new NodeHfs({ fsp });
如果你想要使用简单的胶片,导入 NodeHfsImpl 构造器 :
import { NodeHfsImpl } from "@humanfs/node";
import fsp from "fs/promises";
const hfs = new NodeHfsImpl();
// optionally specify the fs/promises object to use
const hfs = new NodeHfsImpl({ fsp });
处理错误
ENOENT- 在大多数情况下,这些错误是默默处理的.ENFILE财务报告和审定财务报表EMFILE- 导致这些错误的电话要经过长达60秒的重审,然后才能放弃.
许可证
Apache 2.0 (英语)