README
控制台输入输出
print( color , text )
向控制台输出一串字符串(默认不换行)
color: 可选,输出字符串的颜色
color = { red, green, blue, white, black, yellow, reset }
text: 输出的文本
return: text
示例
print("red","这是红色的信息") print("这是默认颜色的信息") print("这是换行的信息\n")
readLine ( color , text )
从控制台读取一行字符串
color: 可选,同
print()
的color
参数text: 提示文本
return: 读取的字符串(不含结尾的
\n
)示例
let name = await readLine("输入你的名字:") let stst = await readLine("red","确定退出吗?\n") await readLine()
文件操作
file.list( dir, deepth )
遍历path目录
dir: 遍历的目录
deepth: 递归遍历文件夹的深度,默认遍历所有子文件夹,为
1
时只遍历dir文件夹return: 文件路径数组
示例
file.list('./') file.list('./',2)
file.info ( filePath )
文件信息(选取于fs.stat)
filePath: 文件路径
return:
{ size, baseName, extName, dirName, 访问时间, 修改时间, 创建时间, }
注意,extName默认被转为小写字母格式示例
file.info('./baseTool.js')
结果:
{ size: 3158, baseName: 'baseTool.js', extName: '.js', dirName: '.', '访问时间': 2020-08-12T08:06:49.695Z, '修改时间': 2020-08-12T08:06:49.480Z, '创建时间': 2020-08-12T04:16:35.553Z }
file.rename ( filePath , rename )
重命名文件
filePath: 文件路径
rename:
- ( fileName, filePath ) => newName
- string: newName
return: 改名后的文件路径
示例
file.rename('./a.txt','b.txt') file.rename('./a.txt',(v)=>'b.txt') //批量重命名当前文件夹的png图片 file.list('./',1) .filter(v=>file.info(v).extName==".png") .forEach((v,i)=>file.rename(v,i+".png"))
file.read ( file )
以utf8格式读取文件
- file: 文件路径
- return: 文件内容
file.write ( file )
以utf8重写文件
- file: 文件路径
- return: 写入内容
file.json( file )
创建file.json对象
file: 文件路径
return: Object
Object:
set : (key,value)
设置json[key]=value
get: (key)
获取json[key]
clear: ()
清除json
示例
let a = file.json('./a.json') // set a.set({a:23,b:"hello"}) a.set("c",23) // get a.get("c") // 23 a.get(["a","b","c"]) // [23,"hello",23] json_a=a.get() // {a,b,c} // clear a.clear() // {}
execSync ( cmd )
执行cmd命令
cmd: 命令
return {stdout,stderr}
示例
await execSync("echo | npm --version")
file.encrypt ( file, pwd, newFile=file )
用aes-192-cbc加密
data: 原始数据
key: 密码
newFile: 加密后的文件路径,默认覆盖原文件
示例
await file.encrypt("1.txt","password")
file.decrypt ( file, pwd, newFile=file )
对应file.encrypt
的解密函数
示例
await file.decrypt("1.txt","password")
字符串操作
encrypt(str, pwd)
字符串加密
decrypt(str, pwd)
字符串解密