io-helper

3.0.11 • Public • Published

io操作辅助功能

对文件常规操作辅助工具库

  1. 所有异步方法均采用promise化接口
  2. 对文件的增删改操作,递归文件路径操作 等等
  3. 未使用sync结尾的方法或者未说明是同步方法的一定是返回promise的异步方法
  4. 如果参数名以is开头说明一定是boolean类型

安装

    $ npm install --save io-helper

用例

import ioHelper from 'io-helper'

属性

fs

fs 对应 import * as fs from 'fs-extra-promise'

pathTool

nodejs内置path模块的别名

pathTool 对应 import * as pathTool from 'path'

join

同步路径拼接

join(...path)

  • 返回 string

resolve

同步获取路径拼接后的绝对路径

resolve(...path)

  • 返回 string

relative

同步获取两个路径之间的相对路径

relative(fromPath, toPath)

  • 返回 string

globby

路径特征查找

globby(patterns: string | Array<string>, options: any = {})

  • options { dot: true, silent: true, strict: false, ...options }
  • patterns:参见glob模块对应pattern参数
  • 选项参考 globby
  • 返回 Promise<string> 文件数组

renameSync

renameSync(dirname, oldFilename, newFilename)

  • 同步重命名文件 /xx/yy/z.js
  • dirname:文件的目录 /xx/yy/
  • oldFilename: 文件原名 z.js
  • newFilename: 新文件名 new.js

rename

rename(dirname, oldFilename, newFilename)

  • 异步重命名文件 /xx/yy/z.js
  • dirname:文件的目录 /xx/yy/
  • oldFilename: 文件原名 z.js
  • newFilename: 新文件名 new.js

move

移动文件或者目录

move(fromPath, toPath, options?)

  • fromPath: 源路径 xxxx/xxx/1.txt
  • toPath: 目的路径 自动创建路径 qqq/yyyy只移动不改名 或者 qqq/yyy/222.txt移动并且改名
  • options: {overwrite:true} 覆盖同名文件

readLine

逐行读取文本

readLine(filePath, cb)

  • filePath: 文件的路径
  • cb: 读取每一行时的回调cb(line) line:每一行的字符串
  • 返回 Promise<[string]>

findRecurseSync

递归查找文件目录

findRecurseSync(path:string, filter?:string)

  • filter: filter = filter || '**/*.*' glob库的pattern特性串
  • 返回 [{ pathName, filePath, relative, fileName, isFolder, isFile,fileName}]

findRecurseTreeSync

同步递归查找目录返回树形数据结构  
  • 返回:[{fileName,filePath,isFile,pathName,relative,chidren:[与自身相同结构]}]

makeDir

创建路径递归

makeDir(directory)

  • directory:要创建的路径

makeDirSync

同步创建路径递归

makeDirSync(directory)

  • directory:要创建的路径

exists

文件存在判断

exists(path)

  • 返回 Promise<boolean>

getDirName

获取目录地址

getDirName(path:string|[string])

  • require('path').dirname 使用一致
  • 此方法加入是为了支持path参数 可以是一个数组

replaceFileNameExt

替换文件类型

replaceFileNameExt(filePath, newExt)

  • filePath: 文件路径 如 /xxx/xxx/a.jpg
  • newExt: 新文件后缀 如 .png

copy

异步复制文件

copy(path, toPath)

  • path: 源路径
  • toPath: 目标路径

readFile

读取文件内容

readFile(path)

  • 返回 Promise<string>
  • 默认用utf编码

readFileStream

读取一个文件返回流

readFileStream(path:string)

  • path: 文件路径

saveFile

保存文件

saveFile(path, content)

delete

删除文件或文件夹

delete(path)

  • path: 文件夹路径 或者 具体的文件路径

writeFile

写文件或者在新行追加文件

async writeFile(path, content:string, isAppend = false)

  • 需要使用 await 调用
  • path: 文件路径
  • content: 字符串内容
  • isAppend: 是否追加否则覆盖 默认是覆盖

replaceSep

将反斜杠 替换为正斜杠

replaceSep(path)

  • path: 文件路径
  • 返回 Promise

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
3.0.114latest

Version History

VersionDownloads (Last 7 Days)Published
3.0.114
3.0.101
3.0.90
3.0.81
3.0.70
3.0.61
3.0.50
3.0.41
3.0.31
3.0.21
3.0.10
3.0.01
2.0.21
2.0.11
1.0.80
1.0.71
1.0.60
1.0.51
1.0.40
1.0.31
1.0.21
1.0.10
0.1.41
0.1.31
0.1.20
0.1.10
0.1.01
0.0.91
0.0.80
0.0.71
0.0.61
0.0.50
0.0.40
0.0.30
0.0.21
0.0.10

Package Sidebar

Install

npm i io-helper

Weekly Downloads

24

Version

3.0.11

License

MIT

Unpacked Size

16 kB

Total Files

4

Last publish

Collaborators

  • g120hbq