代码拉取完成,页面将自动刷新
标准化处理 normalize
,你不用担心路径的格式问题;esm
模块化,支持天生支持 树摇(tree-shaking)
,不用担心打包后的体积;commonjs
规范的 cjs
版本;fs/promises
实现;hound
依赖于 FileHound,用于搜索文件;如果你不需要它,可以视为 0
个第三方包依赖;npm install @kwooshung/files
yarn add @kwooshung/files
pnpm add @kwooshung/files
import { normalize } from '@kwooshung/files';
normalize('a/b/c'); // a/b/c
normalize('a//b/////c'); // a/b/c
normalize('a\\b\\c'); // a/b/c
import { exists } from '@kwooshung/files';
async () => {
await exists('a/b/c'); // true
};
import { notExists } from '@kwooshung/files';
async () => {
await notExists('a/b/c/aNotExists'); // true
};
import { isFile } from '@kwooshung/files';
async () => {
await isFile('a/b/c/test.txt'); // true
};
import { isDir } from '@kwooshung/files';
async () => {
await isDir('a/b/c'); // true
};
import { makeDir } from '@kwooshung/files';
async () => {
await makeDir('a/b/c'); // 创建单个目录
await makeDir(['a/b/c/1', 'd/e/f/2']); // 创建多个目录
};
import { getDir } from '@kwooshung/files';
async () => {
await getDir('a/b/c'); // 返回文件或目录路径数组
};
import { read } from '@kwooshung/files';
async () => {
await read('a/b/c/test.txt'); // 返回文件内容
await read('a/b/c/test.txt', 'utf8'); // 返回文件内容,指定编码
};
{ append: false, overwrite: true, encoding: 'utf8' }
ts
,那么 第三个参数 append
和 overwrite
的类型是互斥的,即:append
为 true
时,overwrite
为 false
,反之亦然。import { write } from '@kwooshung/files';
async () => {
await write('a/b/c/test.txt', 'hello world'); // true
await write('a/b/c/test.txt', 'hello world', {
append: true, // 追加,文件若不存在,则新建
overwrite: false, // 不覆盖
encoding: 'utf8' // 指定编码
}); // true
await write('a/b/c/test.txt', 'hello world', {
append: false, // 追加
overwrite: true // 覆盖, 文件若不存在,则新建
encoding: 'utf8' // 指定编码
}); // true
await write('a/b/c/test.txt', 'hello world', {
append: false, // 不追加
overwrite: true // 覆盖, 文件若不存在,则新建
}); // true
await write('a/b/c/test.txt', 'hello world', {
encoding: 'binary' // 指定编码
}); // true
};
只要不抛出异常,就表示复制成功,哪怕目标文件已存在。
import { copyFile } from '@kwooshung/files';
async () => {
await copyFile('a/b/c/1.txt', 'd/e/f/2.txt'); // 表示覆盖目标文件
await copyFile('a/b/c/1.txt', 'd/e/f/2.txt', false); // 即使目标文件已存在,虽然也不会覆盖,但是表示执行成功
};
false
同名文件不会覆盖;import { copyDir } from '@kwooshung/files';
async () => {
await copyDir('a/b/c', 'd/e/f');
await copyDir('a/b/c', 'd/e/f', false);
};
移动文件,本质上是复制文件,然后删除源文件;这样做的好处是,若移动失败,源文件还在,不会丢失数据。即 copy/file
和 remove
的 功能组合
import { moveFile } from '@kwooshung/files';
async () => {
await moveFile('a/b/c/1.txt', 'd/e/f/2.txt'); // 表示覆盖目标文件
await moveFile('a/b/c/1.txt', 'd/e/f/2.txt', false); // 即使目标文件已存在,虽然也不会覆盖,但是表示执行成功
};
复制目录,copy/dir
和 remove
的 功能组合,因此,也具有 copy/dir
的合并逻辑。
import { moveDir } from '@kwooshung/files';
async () => {
await moveDir('a/b/c', 'd/e/f');
await moveDir('a/b/c', 'd/e/f', false);
};
import { remove } from '@kwooshung/files';
async () => {
await remove('a/b/c'); // 删除单个文件或目录
await remove(['a/b/c/1', 'd/e/f/2']); // 删除多个文件或目录
};
会删除指定路径下的所有空文件夹,包括指定路径本身。
import { removeEmptyDirs } from '@kwooshung/files';
async () => {
await removeEmptyDirs('a/b/c'); // 删除单个文件或目录
await removeEmptyDirs(['a/b/c/1', 'd/e/f/2']); // 删除多个文件或目录
};
import { size } from '@kwooshung/files';
async () => {
await size('a/b/c'); // [{path: 'a/b/c', size: 1200;}]
await size(['a/b/c', 'd/e/f']); // [{path: 'a/b/c', size: 1200;}, {path: 'd/e/f', size: 2024;}]
};
import { sizeHuman } from '@kwooshung/files';
sizeHuman(1024); // {size: '1.25', unit: 'KB'}
sizeHuman(1024, 3); // {size: '1.234', unit: 'KB'}
建议结合 size 使用
import { sizeUnit } from '@kwooshung/files';
async () => {
await sizeUnit({path: 'a/b/c'; size: 1024;}); // [{path: 'a/b/c', size: '1200', unit: {size: '1.2', unit: 'KB'}}]
await sizeUnit([{path: 'a/b/c'; size: 1024;}, {path: 'd/e/f'; size: 2024;}]); // [{path: 'a/b/c', size: '1200', unit: {size: '1.2', unit: 'KB'}}, {path: 'd/e/f', size: '2024', unit: {size: '2.02', unit: 'KB'}}]
};
import { hound } from '@kwooshung/files';
const fh1 = hound();
fh1
.modified("< 2 days")
.find()
.each(console.log);
const fh2 = FileHound.create();
filehound
.addFilter(customFilter)
.find()
.each(console.log);
hound(fh1, fh2);
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。
1. 开源生态
2. 协作、人、软件
3. 评估模型