Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
helloyou2012 committed Aug 6, 2019
0 parents commit f33f7c4
Show file tree
Hide file tree
Showing 12 changed files with 463 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 1,16 @@
# http://editorconfig.org
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false

[Makefile]
indent_style = tab
10 changes: 10 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 1,10 @@
extends: standard

env:
node: true
mocha: true

rules:
camelcase: 0
semi: [2, always]
object-curly-spacing: [2, always]
29 changes: 29 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 1,29 @@
*.iml
.idea/
.ipr
.iws
*~
~*
*.diff
*.patch
*.bak
.DS_Store
Thumbs.db
.project
.*proj
.svn/
*.swp
*.swo
*.log
*.sublime-project
*.sublime-workspace
node_modules/*
npm-debug.log
package-lock.json
tmp/
.buildpath
.settings
.yml
_site
coverage
.nyc_output/
12 changes: 12 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 1,12 @@
.idea
.DS_Store
.nyc_output/
.babelrc
.editorconfig
.eslintrc
.travis.yml
tmp
node_modules
package-lock.json
coverage
test
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 1,9 @@
language: node_js

node_js:
- 8
- 10
- 12

after_success:
- npm run coveralls
57 changes: 57 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 1,57 @@
micro-fs
========

> File system and globbing utilities
[![NPM version](https://img.shields.io/npm/v/micro-fs.svg)](https://www.npmjs.com/package/micro-fs)
[![NPM downloads](https://img.shields.io/npm/dm/micro-fs.svg)](https://www.npmjs.com/package/micro-fs)
[![Build Status](https://travis-ci.org/d-band/micro-fs.svg?branch=master)](https://travis-ci.org/d-band/micro-fs)
[![Coverage Status](https://coveralls.io/repos/github/d-band/micro-fs/badge.svg?branch=master)](https://coveralls.io/github/d-band/micro-fs?branch=master)
[![Dependency Status](https://david-dm.org/d-band/micro-fs.svg)](https://david-dm.org/d-band/micro-fs)
[![Greenkeeper badge](https://badges.greenkeeper.io/d-band/micro-fs.svg)](https://greenkeeper.io/)

---

## Install

```bash
$ npm install micro-fs -S
```

## Usage

```js
const mfs = require('micro-fs');

mfs.copy('src/**/*.js', 'lib').then(() => {
console.log('copy done.');
});

mfs.move('src/**/*.js', 'lib').then(() => {
console.log('move done.');
});

mfs.delete('dist/**').then(() => {
console.log('delete done.');
});

mfs.zip('src/**/*.js', 'source.zip').then(() => {
console.log('archive done.');
});

mfs.glob('src/**/*.js', options).then(files => {
console.log(files);
/**
* [{ cwd, base, path }]
*/
});
```

## Report a issue

* [All issues](https://github.com/d-band/micro-fs/issues)
* [New issue](https://github.com/d-band/micro-fs/issues/new)

## License

micro-fs is available under the terms of the MIT License.
43 changes: 43 additions & 0 deletions lib/fsp.js
Original file line number Diff line number Diff line change
@@ -0,0 1,43 @@
const fs = require('fs');
const path = require('path');
const { promisify } = require('es6-promisify');

const stat = promisify(fs.stat);
const mkdir = promisify(fs.mkdir);
const rmdir = promisify(fs.rmdir);
const copyFile = promisify(fs.copyFile);
const rename = promisify(fs.rename);
const unlink = promisify(fs.unlink);
const mkdirp = src => mkdir(src, { recursive: true });

const wrap = fn => (src, dest) => {
return stat(src).then(stats => {
if (stats.isDirectory()) {
return mkdirp(dest);
} else {
const dir = path.dirname(dest);
return mkdirp(dir).then(() => fn(src, dest));
}
});
};

const remove = (src) => {
return stat(src).then(stats => {
if (stats.isDirectory()) {
return rmdir(src);
} else {
return unlink(src);
}
});
};

module.exports = {
stat: stat,
mkdir: mkdir,
mkdirp: mkdirp,
rmdir: rmdir,
remove: remove,
copy: wrap(copyFile),
move: wrap(rename),
write: promisify(fs.writeFile)
};
17 changes: 17 additions & 0 deletions lib/glob.js
Original file line number Diff line number Diff line change
@@ -0,0 1,17 @@
const gs = require('glob-stream');

module.exports = (globs, options) => {
const stream = gs(globs, options);
return new Promise((resolve, reject) => {
const list = [];
const onData = v => list.push(v);
const onEnd = (err) => {
if (err) return reject(err);
return resolve(list);
};
stream.on('data', onData);
stream.on('end', onEnd);
stream.on('error', onEnd);
stream.on('close', onEnd);
});
};
47 changes: 47 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 1,47 @@
const fs = require('fs');
const path = require('path');
const yazl = require('yazl');
const fsp = require('./fsp');
const glob = require('./glob');

exports.fsp = fsp;
exports.glob = glob;

const globWrap = (fn) => (src, dest, options) => {
return glob(src, options).then((files) => {
return Promise.all(files.map(file => {
const relative = path.relative(file.base, file.path);
const destPath = path.join(dest, relative);
return fn(file.path, destPath);
}));
});
};

exports.copy = exports.cp = globWrap(fsp.copy);
exports.move = exports.mv = globWrap(fsp.move);

exports.delete = exports.rm = exports.del = (src, options) => {
return glob(src, options).then((files) => {
return files.reverse().reduce((p, file) => {
return p.then(() => fsp.remove(file.path));
}, Promise.resolve());
});
};

exports.archive = exports.zip = (src, dest, options) => {
options = Object.assign({}, options, { nodir: true });
const zip = new yazl.ZipFile();
return glob(src, options).then((files) => {
files.forEach(file => {
const relative = path.relative(file.base, file.path);
zip.addFile(file.path, relative);
});
return new Promise((resolve, reject) => {
const stream = fs.createWriteStream(dest);
stream.on('close', resolve);
stream.on('error', reject);
zip.outputStream.pipe(stream);
zip.end();
});
});
};
62 changes: 62 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 1,62 @@
{
"name": "micro-fs",
"version": "1.0.0-beta",
"description": "File system and globbing utilities",
"main": "lib/index.js",
"scripts": {
"lint": "eslint lib test",
"test": "nyc mocha",
"report": "nyc report --reporter=html",
"coveralls": "nyc report --reporter=text-lcov | coveralls"
},
"nyc": {
"all": true,
"include": [
"lib/**/*.js"
]
},
"pre-commit": [
"lint"
],
"repository": {
"type": "git",
"url": "git https://github.com/d-band/micro-fs.git"
},
"keywords": [
"glob",
"fs",
"file-system",
"copy",
"cp",
"move",
"mv",
"rm",
"delete",
"mkdir",
"archive",
"zip"
],
"author": "d-band",
"license": "MIT",
"bugs": {
"url": "https://github.com/d-band/micro-fs/issues"
},
"homepage": "https://github.com/d-band/micro-fs#readme",
"dependencies": {
"es6-promisify": "^6.0.1",
"glob-stream": "^6.1.0",
"yazl": "^2.5.1"
},
"devDependencies": {
"coveralls": "^3.0.5",
"eslint": "^6.1.0",
"eslint-config-standard": "^13.0.1",
"eslint-plugin-import": "^2.18.2",
"eslint-plugin-node": "^9.1.0",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.0",
"mocha": "^6.2.0",
"nyc": "^14.1.1",
"pre-commit": "^1.2.2"
}
}
73 changes: 73 additions & 0 deletions test/glob.test.js
Original file line number Diff line number Diff line change
@@ -0,0 1,73 @@
const os = require('os');
const path = require('path');
const assert = require('assert');
const mfs = require('../lib');

const ROOT = path.join(os.tmpdir(), 'micro-fs');

describe('lib/index.js', function () {
this.timeout(0);

before(() => {
return Promise.all([
mfs.fsp.mkdirp(path.join(ROOT, 'a0/b0/c0')),
mfs.fsp.mkdirp(path.join(ROOT, 'a0/b0/c1')),
mfs.fsp.mkdirp(path.join(ROOT, 'a0/b1/c0')),
mfs.fsp.mkdirp(path.join(ROOT, 'a0/b1/c1'))
]).then(() => {
return Promise.all([
mfs.fsp.write(path.join(ROOT, 'a0/d.js'), 'data: a0/d.js'),
mfs.fsp.write(path.join(ROOT, 'a0/d.css'), 'data: a0/d.css'),
mfs.fsp.write(path.join(ROOT, 'a0/b0/d.js'), 'data: a0/b0/d.js'),
mfs.fsp.write(path.join(ROOT, 'a0/b1/d.css'), 'data: a0/b0/d.css'),
mfs.fsp.write(path.join(ROOT, 'a0/b1/c1/d.js'), 'a1/b1/c1/d.js')
]);
});
});

after(() => {
return mfs.delete('**', { cwd: ROOT });
});

it('test glob all', () => {
return mfs.glob('a0/**/*', {
cwd: ROOT
}).then(files => {
assert.strictEqual(files.length, 11);
});
});

it('test glob files', () => {
return mfs.glob('a0/**/*.js', {
cwd: ROOT
}).then(files => {
assert.strictEqual(files.length, 3);
});
});

it('test glob dirs', () => {
return mfs.glob('a0/**/*/', {
cwd: ROOT
}).then(files => {
assert.strictEqual(files.length, 6);
});
});

it('test glob array', () => {
return mfs.glob(['a0/**/*.js', 'a0/**/*.css'], {
cwd: ROOT
}).then(files => {
assert.strictEqual(files.length, 5);
});
});

it('test glob error', () => {
return mfs.glob('a100', {
cwd: ROOT
}).then(() => {
assert.fail();
}).catch(() => {
assert.ok(true);
});
});
});
Loading

0 comments on commit f33f7c4

Please sign in to comment.