Skip to content

引入

ts
import {
  parseCommentJson,
  readCommentJson,
  mkDirsSync,
  copyDir,
  deleteFolder,
  rmEmptyDir,
  deleteFolderRecursive,
  copyFile,
  traverseFolder,
  readJsonLog,
  getJsonLogDir,
  saveJsonToLog,
  saveJsonToLogMore,
  getJsonFromLog,
  getFileName,
  readJson,
  writeFileSync,
  readFileSync
} from 't-comm';

// 不支持 tree-shaking 的项目
import {
  parseCommentJson,
  readCommentJson,
  mkDirsSync,
  copyDir,
  deleteFolder,
  rmEmptyDir,
  deleteFolderRecursive,
  copyFile,
  traverseFolder,
  readJsonLog,
  getJsonLogDir,
  saveJsonToLog,
  saveJsonToLogMore,
  getJsonFromLog,
  getFileName,
  readJson,
  writeFileSync,
  readFileSync
} from 't-comm/lib/fs/index';

// 只支持 ESM 的项目
import {
  parseCommentJson,
  readCommentJson,
  mkDirsSync,
  copyDir,
  deleteFolder,
  rmEmptyDir,
  deleteFolderRecursive,
  copyFile,
  traverseFolder,
  readJsonLog,
  getJsonLogDir,
  saveJsonToLog,
  saveJsonToLogMore,
  getJsonFromLog,
  getFileName,
  readJson,
  writeFileSync,
  readFileSync
} from 't-comm/es/fs/index';

parseCommentJson(content)

描述

解析带注释的 json 文件

参数

参数名描述
content

原始文件内容

返回:

json数据

readCommentJson(file)

描述

获取带注释的 json 文件内容

参数

参数名描述
file

文件路径

返回:

json数据

mkDirsSync(dirname)

描述

递归创建目录(同步方法) 如果目录已存在则直接返回,否则递归创建父目录

参数

参数名描述
dirname

要创建的目录路径

返回:

创建成功返回 true

示例

ts
mkDirsSync('/path/to/new/directory');

copyDir(src, dist, callback)

描述

拷贝目录以及子文件 递归复制整个目录结构,包括所有子目录和文件

参数

参数名描述
src

源目录路径

dist

目标目录路径

callback

可选的回调函数,复制完成后执行

示例

ts
copyDir('/source/path', '/target/path', () => {
  console.log('复制完成');
});

deleteFolder(tPath)

描述

删除目录及其所有内容 递归删除目录下的所有文件和子目录

参数

参数名描述
tPath

要删除的目录路径

示例

ts
deleteFolder('/path/to/folder');

rmEmptyDir(tPath, level)

描述

递归删除空目录 从指定路径开始,递归删除所有空目录(不删除包含文件的目录)

参数

参数名描述
tPath

要检查和删除的目录路径

level

当前递归层级,默认为 0(根层级不会被删除)

示例

ts
rmEmptyDir('/path/to/check');

deleteFolderRecursive(path, options)

描述

递归删除文件夹(可配置是否删除文件) 递归遍历目录,根据配置决定是否删除文件,并删除空目录

参数

参数名描述
path

要处理的目录路径

options

配置选项

options.deleteFile

是否删除文件,默认为 false

options.log

是否输出日志,默认为 false

示例

ts
// 只删除空目录
deleteFolderRecursive('/path/to/folder');

// 删除所有文件和目录
deleteFolderRecursive('/path/to/folder', { deleteFile: true, log: true });

copyFile(from, to)

描述

拷贝单个文件 将文件从源路径复制到目标路径

参数

参数名描述
from

源文件路径

to

目标文件路径

返回:

写入操作的结果

示例

ts
copyFile('/source/file.txt', '/target/file.txt');

traverseFolder(cb, tPath)

描述

递归遍历文件夹,并对每个文件执行回调函数 遍历目录树,对每个文件(非目录)执行指定的回调函数

参数

参数名描述
cb

回调函数,接收文件路径作为参数

tPath

要遍历的文件夹或文件路径

示例

ts
traverseFolder((filePath) => {
  console.log('处理文件:', filePath);
}, '/path/to/folder');

readJsonLog(file, defaultContent)

描述

从日志目录读取 JSON 文件 读取 ./log 目录下的 JSON 文件内容

参数

参数名描述
file

文件名(相对于 log 目录)

defaultContent

文件不存在时返回的默认内容,默认为 ''

返回:

JSON 文件内容字符串

示例

ts
const content = readJsonLog('data.json', '[]');

getJsonLogDir()

描述

获取 JSON 日志目录的绝对路径

参数

返回:

日志目录的绝对路径

示例

ts
const logDir = getJsonLogDir();
console.log(logDir); // /path/to/project/log

saveJsonToLog(content, file, needLog)

描述

将 JSON 对象保存到日志文件 将对象序列化为 JSON 并保存到 ./log 目录下

参数

参数名描述
content

要保存的对象内容

file

文件名(相对于 log 目录)

needLog

是否需要保存日志,默认为 true

示例

ts
saveJsonToLog({ status: 'success', data: [1, 2, 3] }, 'result.json');

saveJsonToLogMore(content, file, options)

描述

将内容追加保存到日志文件(支持保留历史记录) 以数组形式保存多条日志记录,每条记录包含时间戳和数据,支持限制最大记录数

参数

参数名描述
content

要保存的内容

file

文件名(相对于 log 目录)

options

配置选项

options.needLog

是否需要保存日志,默认为 true

options.max

最大保留记录数,默认为 10

示例

ts
saveJsonToLogMore({ action: 'upload', status: 'success' }, 'history.json', {
  needLog: true,
  max: 20
});

getJsonFromLog(file)

描述

从日志目录读取并解析 JSON 文件 读取 ./log 目录下的 JSON 文件并解析为对象

参数

参数名描述
file

文件名(相对于 log 目录)

返回:

解析后的 JSON 对象,解析失败或文件不存在时返回空对象

示例

ts
const data = getJsonFromLog('config.json');
console.log(data);

getFileName(file)

描述

从文件路径中提取文件名(不含扩展名)

参数

参数名描述
file

文件路径

返回:

不含扩展名的文件名

示例

ts
const name = getFileName('/path/to/file.txt');
console.log(name); // 'file'

readJson(content, file)

描述

解析 JSON 字符串为对象 安全地解析 JSON 字符串,解析失败时输出错误信息并返回空对象

参数

参数名描述
content

JSON 字符串内容

file

文件路径(用于错误日志)

返回:

解析后的对象,解析失败时返回空对象

示例

ts
const data = readJson('{"name":"test"}', 'config.json');
console.log(data); // { name: 'test' }

writeFileSync(file, data, [isJson])

描述

写入文件

参数

参数名类型描述
filestring

文件地址

dataany

文件数据

[isJson]boolean

是否需要 json 序列化

示例

ts
writeFileSync('a', 'b.txt', false);

writeFileSync({ a: 1 }, 'b.json', true);

readFileSync(file, [isJson])

描述

读取文件

参数

参数名类型描述
filestring

文件地址

[isJson]boolean

是否需要 json 反序列化

返回: any

文件内容

示例

ts
readFileSync('b.txt', false);

readFileSync('b.json', true);