fs

fs-sugar

node-fs-sugar 封装了部分fs常用的方法,来方便开发。

Showing:

Popularity

Downloads/wk

3

GitHub Stars

1

Maintenance

Last Commit

7yrs ago

Contributors

1

Package

Dependencies

0

Size (min+gzip)

0.9KB

License

BSD-2-Clause

Type Definitions

Tree-Shakeable

No?

Categories

Readme

node-fs-sugarBuild Status

fs-sugar(甜fs)封装了部分fs常用的方法,来方便开发。

Quick Examples

file

  • createFileSync(filePath) 如果目录不存在,会先创建目录。
var fs = require('fs'),
    sugar = require('fs-sugar');
var file = path.join('/a/b', 'aa.txt');
sugar.createFileSync(file);
assert.equal(fs.existsSync(file), true, 'createFilSyenc fail');
  • isFileSync(filePath) 判断是否为文件,如果不存在,直接抛出异常。
var sugar = require('fs-sugar');
assert.equal(sugar.isFileSync('/a/b'), false, 'isFileSync fail')
  • copyFileSync(srcFile, destDir) 将srcFile文件拷贝至destDir,如果destDir不存在会新建
var sugar = require('fs-sugar');
sugar.copyFileSync('/a/b/aa.txt','/c')

dir

  • isDirectorySync(dirPath) 判断是否为文件夹,如果不存在,直接抛出异常。
var sugar = require('fs-sugar');
assert.equal(sugar.isDirectorySync('/a/b'), true, 'isDirectorySync fail')
  • mkDir(dir/,mode/,cb) 创建多层目录
var path = require('path'),
    sugar = require('../lib/sugar');

var baseDir = path.join('/', 'testSugar' + new Date().getTime()),
    dir = path.join(baseDir, "a/b/c/d/e");
sugar.mkDir(dir, '0755', function (err, dir) {
    console.log(err);
    console.log(dir);
});

sugar.mkDir(dir, function (err, dir) {
    console.log(err);
    console.log(dir);
});
  • mkDirSync(dir/,mode/) 创建多层目录,类似mkdir -p /a/b/c
var sugar = require('fs-sugar');
sugar.mkdirSync('/a/b/c');

sugar.mkdirSync('/a/b/c','0755');
  • rmrDirSync(dir) 直接删除目录,类似rm -rf /a/b
var sugar = require('fs-sugar');
sugar.rmrDirSync('/a/b');
  • listSync(dir) 列出文件夹下面所有文件
var sugar = require('fs-sugar');
var fileNameArray = sugar.listSync(dir);
  • listFilterSync(dir,filter) 按照制定的条件列出子文件、目录
var sugar = require('fs-sugar');
var fileNameArray = sugar.listFilter('/a/b/c',function(fileName,fileStat){
    console.log(fileName);
    console.log(JSON.stringify(fileStat));
    if (fileStat.isFile()) {
        return true;
    } else {
        return false;
    }
});
  • copyDirSync(source, dest) 将source文件夹拷贝成destDir文件夹,类似cp /a/b /c
var sugar = require('fs-sugar');
sugar.copyDirSync('/a/b', '/c');
  • copyDirFilterSync(source, dest, filter) 将source文件夹拷贝成destDir文件夹,其中可以用filter过滤不需要拷贝文件
var sugar = require('fs-sugar');
sugar.copyDirFilterSync('/a/b', '/c', function (fileName, fileStat) {
    console.log(fileName);
    console.log(JSON.stringify(fileStat));
    if (fileStat.isFile()) {
        return true;
    } else {
        return false;
    }
});

TODO

  1. 异步

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100