commit
						b31001bd93
					
				|  | @ -0,0 +1,7 @@ | |||
| 1.0.0 / 2017-02-26 | ||||
| ================== | ||||
| 
 | ||||
|   * new project | ||||
|    | ||||
| 
 | ||||
| 
 | ||||
|  | @ -0,0 +1,118 @@ | |||
|  | ||||
| 
 | ||||
| # iofs | ||||
| > `iofs` is a bash-like module for reading and writing files on Node.js. It base on Node.js's native `fs` module. | ||||
| 
 | ||||
| ## property | ||||
| 
 | ||||
| ### self | ||||
| > It return the native `fs` module for more requests. | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| ## API | ||||
| 
 | ||||
| ### cat(file) | ||||
| - file `<String>` | ||||
| 
 | ||||
| > Just like bash's cat, it will read a file and return a Buffer. | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| ### ls(path) | ||||
| - path `<String>` | ||||
| 
 | ||||
| > List all files and folders of the path given exclude '.' and '..'. I t return an array. | ||||
| 
 | ||||
| 
 | ||||
| ### echo(data, file[, append][, encode]) | ||||
| - data `<String>` | `<Buffer>` | `<Number>` | ||||
| - file `<String>` | ||||
| - append `<Boolean>` optional | ||||
| - encode `<String>` optional | ||||
| 
 | ||||
| > Write/Append data to a file. creating the file if it does not yet exist. | ||||
| > If `append` is set true, it will append data to the file. | ||||
| > Default `encode` is utf8. | ||||
| 
 | ||||
| ```javascript | ||||
| let fs = require('iofs') | ||||
| 
 | ||||
| fs.echo('hello ', 'test.txt') // replacing test.txt if it exists. | ||||
| 
 | ||||
| fs.echo('world', 'test.txt', true) // append the data to test.txt | ||||
| 
 | ||||
| ``` | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| ### chmod(file, mode) | ||||
| - file `<String>` | `<Buffer>` | ||||
| - mode `<Integer>` | ||||
| 
 | ||||
| > Changes the mode of the file specified whose pathname is given. | ||||
| 
 | ||||
| ```javascript | ||||
| 
 | ||||
| fs.chmod('test.txt', 777) // replacing test.txt if it exists. | ||||
| 
 | ||||
| ``` | ||||
| 
 | ||||
| 
 | ||||
| ### mv(from, to) | ||||
| - from `<String>` | ||||
| - to `<String>` | ||||
| 
 | ||||
| > Move a fil to the target location. It can also use to renaming a file. | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| ### cp(from, to) | ||||
| - from `<String>` | ||||
| - to `<String>` | ||||
| 
 | ||||
| > Copy a fil to the target location. | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| ### rm(path, recursion) | ||||
| - path `<String>` | ||||
| - recursion `<Boolean>` | ||||
| 
 | ||||
| > Delete a file or a folder. If path is a folder, `recursion` must be set to true. | ||||
| 
 | ||||
| ```javascript | ||||
| 
 | ||||
| fs.rm('./foo/test.txt') | ||||
| 
 | ||||
| fs.rm('./foo', true) | ||||
| 
 | ||||
| ``` | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| ### stat(path) | ||||
| - path `<String>` | ||||
| 
 | ||||
| > Returns an instance of fs.Stats. | ||||
| 
 | ||||
| 
 | ||||
| ### isdir(path) | ||||
| - path `<String>` | ||||
| 
 | ||||
| > Return true if the path is a folder, and false when it is a file or not yet exists. | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| ### mkdir(path) | ||||
| - path `<String>` | ||||
| 
 | ||||
| > Build a folder in where you want. | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| ### exists(path) | ||||
| - path `<String>` | ||||
| 
 | ||||
| > Return true if the path exists, and false not. | ||||
|  | @ -0,0 +1,219 @@ | |||
| /** | ||||
|  *  | ||||
|  * @authors yutent (yutent@doui.cc) | ||||
|  * @date    2015-12-28 14:28:38 | ||||
|  * | ||||
|  */ | ||||
| "use strict"; | ||||
| 
 | ||||
| const fs = require('fs'), | ||||
|     path = require('path'); | ||||
| 
 | ||||
| class Iofs { | ||||
| 
 | ||||
|     constructor(){ | ||||
|         this.self = fs | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
|     /** | ||||
|      * [cat 文件读取] | ||||
|      * @param  {String} file [文件路径] | ||||
|      * @param  {Function} cb   [回调] 可选 | ||||
|      */ | ||||
|     cat(file){ | ||||
|         try{ | ||||
|             return fs.readFileSync(file) | ||||
|         }catch(err){ | ||||
|             console.error(err) | ||||
|             return null | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * [ls 读取整个目录(不遍历子目录)] | ||||
|      * @param  {string} file [目标路径] | ||||
|      * @return {array}      [返回目标目录所有文件名和子目录名, 不包括'.'和'..'] | ||||
|      */ | ||||
|     ls(file){ | ||||
|         try{ | ||||
|             return fs.readdirSync(file) | ||||
|         }catch(err){ | ||||
|             console.error(err) | ||||
|             return null | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
|     /** | ||||
|      * [echo 写文件] | ||||
|      * @param  {String|Buffer|Number} data   [要写入的数据] | ||||
|      * @param  {String} file   [要写的文件] | ||||
|      * @param  {Boolean} append [是否在后面追加,默认否] | ||||
|      * @param  {String} encode [编码, 默认utf8] | ||||
|      */ | ||||
|     echo(data, file, append, encode){ | ||||
|         if(!file){ | ||||
|             return data; | ||||
|         } | ||||
| 
 | ||||
|         let updir = path.parse(file).dir, | ||||
|             opt = {}; | ||||
|         if(!this.isdir(updir)){ | ||||
|             this.mkdir(updir) | ||||
|         } | ||||
| 
 | ||||
|         if(append && typeof append === 'string'){ | ||||
|             encode = append; | ||||
|             append = false; | ||||
|             opt.encoding = encode; | ||||
|         }else{ | ||||
|             if(typeof encode === 'string'){ | ||||
|                 opt.encoding = encode | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         try{ | ||||
|             if(!!append){ | ||||
|                 fs.appendFileSync(file, data, opt) | ||||
|             }else{ | ||||
|                 fs.writeFileSync(file, data, opt) | ||||
|             } | ||||
|         }catch(err){ | ||||
|             console.error(err) | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
|     //修改权限
 | ||||
|     chmod(path, mode){ | ||||
|         try{ | ||||
|             fs.chmodSync(path, mode) | ||||
|         }catch(err){ | ||||
|             console.error(err) | ||||
|         } | ||||
|     } | ||||
|      | ||||
| 
 | ||||
|     /** | ||||
|      * [mv 移动文件,兼具重命名功能] | ||||
|      * @param  {String} from [原路径/原名] | ||||
|      * @param  {String} to   [目标路径/新名] | ||||
|      */ | ||||
|     mv(from, to){ | ||||
| 
 | ||||
|         let updir = path.parse(to).dir | ||||
|         if(!this.isdir(updir)) | ||||
|             this.mkdir(updir) | ||||
| 
 | ||||
|         try{ | ||||
|             fs.renameSync(from, to) | ||||
|         }catch(e){ | ||||
|             let rs = fs.createReadStream(from), | ||||
|             ws = fs.createWriteStream(to); | ||||
| 
 | ||||
|             rs.pipe(ws) | ||||
|             rs.on('end', err => { | ||||
|                 this.rm(from) | ||||
|             }) | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
|     cp(from, to){ | ||||
|         let updir = path.parse(to).dir | ||||
|         if(!this.isdir(updir)){ | ||||
|             this.mkdir(updir) | ||||
|         } | ||||
| 
 | ||||
|         let rs = fs.createReadStream(from), | ||||
|             ws = fs.createWriteStream(to); | ||||
| 
 | ||||
|         rs.pipe(ws) | ||||
|         rs.on('end', err => console.error(err)) | ||||
| 
 | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
|     /** | ||||
|      * [rm 删除文件/目录] | ||||
|      * @param  {[type]} from      [源文件/目录路径] | ||||
|      * @param  {[type]} recursion [是否递归删除,若删除目录,此值须为true] | ||||
|      */ | ||||
|     rm(from, recursion){ | ||||
|         try{ | ||||
|             if(!!recursion){ | ||||
|                 fs.rmdirSync(from) | ||||
|             }else{ | ||||
|                 fs.unlinkSync(from) | ||||
|             } | ||||
|         }catch(err){ | ||||
|             console.error(err) | ||||
|              | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
|     /** | ||||
|      * [stat 返回文件/目录的状态信息] | ||||
|      * @param  {[type]} path [目标路径] | ||||
|      */ | ||||
|     stat(path){ | ||||
|         try{ | ||||
|             return fs.statSync(path) | ||||
|         }catch(err){ | ||||
|             return null | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
|     /** | ||||
|      * [isdir 判断目标是否为目录] | ||||
|      * @param  {String} path [目标路径] | ||||
|      */ | ||||
|     isdir(path){ | ||||
|         try{ | ||||
|             return this.stat(path).isDirectory() | ||||
|         }catch(err){ | ||||
|             return false | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
|     /** | ||||
|      * [mkdir 新建目录] | ||||
|      * @param  {String} dir [目标路径] | ||||
|      */ | ||||
|     mkdir(dir){ | ||||
|         let updir = path.parse(dir).dir | ||||
|         if(!updir) | ||||
|             return | ||||
| 
 | ||||
|         if(!this.isdir(updir)){ | ||||
|             this.mkdir(updir) | ||||
|         } | ||||
| 
 | ||||
|         try{ | ||||
|             fs.mkdirSync(dir) | ||||
|         }catch(err){ | ||||
|             console.error(err) | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * [exists 判断目标(文件/目录)是否存在] | ||||
|      * @param  {String} file [目标路径] | ||||
|      */ | ||||
|     exists(file){ | ||||
|         return fs.existsSync(file) | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| module.exports = new Iofs | ||||
|  | @ -0,0 +1,19 @@ | |||
| { | ||||
|     "name": "iofs", | ||||
|     "version": "1.0.0", | ||||
|     "description": "Base on native fs module, for easy using.", | ||||
|     "main": "index.js", | ||||
|     "repository": { | ||||
|         "type": "git", | ||||
|         "url": "https://github.com/yutent/iofs.git" | ||||
|     }, | ||||
|     "keywords": [ | ||||
|         "dojs", | ||||
|         "fs", | ||||
|         "iofs", | ||||
|         "fs.io", | ||||
|         "file" | ||||
|     ], | ||||
|     "author": "yutent", | ||||
|     "license": "MIT" | ||||
| } | ||||
		Loading…
	
		Reference in New Issue
	
	 宇天
						宇天