Skip to content

Commit

Permalink
v0.3.2 Scan (#25)
Browse files Browse the repository at this point in the history
* remove hard-coded functions names for `promisify` (#9)

* remove hard-coded functions names for `promisify`

* fixing eslint

* fix code style

* add atomics keyword, and bump to v0.1.5

* It requires Node.js 8.1.0 (#10)

* fix npm

npm wouldn't let me fix the pulished version correctly, had to bump package to fix.

* Modualized Refactor and Docs (#12)

* modualise and document

* fix util requires

* fix outputjsonatomic requires

* finish out docs

* generated docs

* Fix travis

* bump version to v0.2.0

* Fix lint

* remove search

the search didn't really work

* cname for js.org

* more docs customization

* More document coverage

* Fix lint

* finish documentation coverage

* Add a Codacy badge to README.md (#13)

* Add Codacy badge

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* update docs

* Create LICENSE

* update dev deps, add tests to travis, unskip all tests

* Some fixes, and a bunch more tests

I am aware builds are going to fail for a while

* Fix one test

* finish tests draft

* fix a couple tests

* fix more tests

* Fix build with test skips

* 3 more skips

* more test work

* remove fixes

* this is probably going to fail build

* maybe fixed 1 test?

* fix move test

* fix some test bugs

* skip symlink tests until I can rewrite to remove fs-mock

* Create .travis-deploy.sh

* Update package.json

* Update .travis.yml

* Update .travis-deploy.sh

* try auto docs

* Update .travis-deploy.sh

* fix .travis-deploy?

* Update .travis-deploy.sh

* Update .travis-deploy.sh

* Update .travis-deploy.sh

* Update .travis-deploy.sh

* Update .travis-deploy.sh

* Update .travis-deploy.sh

* Update .travis-deploy.sh

* Site build: c4a1ec1

* fix docs, from auto docs test

* Site build: b8d9a56

* Update .travis-deploy.sh

* Update .travis-deploy.sh

* Update .travis.yml

* Update .travis-deploy.sh

* add .travis-deploy.sh to npmignore

* update deps

* Docs build: c947062

* Docs build: 01c52e8

* Add Greenkeeper badge 🌴 (#15)

* docs(readme): add Greenkeeper badge

* Update README.md

* Fix travis deploy

* Docs build: 21de27f

* update eslint

* Update README.md

* Docs build: 39e4515

* code cleanup

* Fix typo (#16)

* Docs build: e1f8556

* fix util mkDir

* Docs build: 011f8b7

* Fix copy error

* 0.2.1

* Copy file (#18)

* copyFile changes

* some docs, still very wip

* Update .travis.yml

* mock fs can't handle the new fs.copyFile yet

* emptyDir bugfix

* last bug fix

* Typings. (#19)

* Create tslint.json

* Fixed typos, added typings.

* Fixed all JSDocs

* minor fixes

* fix lint and friends

* missed docs

* le typings

* Docs build: 29fb5da

* update readme

* Docs build: 83dc519

* Coverage (#21)

* start coverage and docs changes

* remove docs from future master

* oops travis

* make codacy happy more tests, still wip

* progress, but mock-fs sucks

* fix scripts

* begin module structure and ditching mock-fs

* add another test to see if somethings wrong...

* oops

* Update Messages, Add test

* tests progress

* push progress

* push progress

* oops

* push progress

* oops

* finish basic tests

* small fixes

* oops

* bug fix

* test fixes

* should fix a lot of symlink bugs?

* maybe fix move tests?

* take 2 move tests

* take 3 move

* fix remove tests

* ahah!

* see how this fares for copy

* idek anymore

* try this on for size

* duh

* fingers crossed

* more copy coverage

* copy coverage

* Update README.md

* More unit tests (#22)

* copy tests

* oh yeah

* symlink copy test

* interesting edge test

* oops

* up threshold

* update npm ignore for prepping patch release

* 0.3.1

* Remove mock-fs since it's not used anymore

* Scan (#24)

* impliment scan idea (klaw without nastiness)

* impliment limit

* umm?

* ohh!

* should do the first directory.

* add filter

* fix limit

* docs

* copy paste OP

* tests

* fix test

* unneeded `async`

* typings I think

* 0.3.2
  • Loading branch information
bdistin committed Oct 18, 2017
1 parent dc708cb commit d3d017f
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 2 deletions.
3 changes: 1 addition & 2 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "fs-nextra",
"version": "0.3.1",
"version": "0.3.2",
"description": "Node.js V8 native fs enhanced with util.promisify and standard extra methods.",
"main": "src/index.js",
"types": "typings/index.d.ts",
Expand Down Expand Up @@ -40,7 +40,6 @@
"eslint": "^4.6.1",
"ink-docstrap": "github:bdistin/docstrap",
"jsdoc": "github:jsdoc3/jsdoc",
"mock-fs": "^4.4.1",
"nyc": "^11.2.1",
"tslint": "^5.7.0",
"tsubaki": "^1.2.0",
Expand Down
10 changes: 10 additions & 0 deletions src/fs.js
Expand Up @@ -53,6 +53,16 @@ for (const [key, value] of Object.entries(fs)) {
* @property {number} constants.S_IXOTH File mode indicating executable by others.
*/

/**
* Identical to {@link https://nodejs.org/api/fs.html#fs_fs_copyfile_src_dest_flags_callback|fs.copyFile} but returns a promise instead.
* @function copyFile
* @memberof fsn/fs
* @param {string|Buffer|URL} src File path to copy
* @param {string|Buffer|URL} dest Destination to copy to
* @param {number} [flags = 0] modifiers for copy operation.
* @return {Promise<void>}
*/

/**
* Objects returned from {@link fsn/fs.watch|watch()} are of this type.
* Identical to {@link https://nodejs.org/api/fs.html#fs_class_fs_fswatcher|fs.FSWatcher}.
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Expand Up @@ -35,6 +35,7 @@ const nextra = {
readJSON: require('./nextra/readJSON'),
readJson: require('./nextra/readJSON'),
remove: require('./nextra/remove'),
scan: require('./nextra/scan'),
symlinkAtomic: require('./nextra/symlinkAtomic'),
writeFileAtomic: require('./nextra/writeFileAtomic'),
writeJSON: require('./nextra/writeJSON'),
Expand Down
21 changes: 21 additions & 0 deletions src/nextra/scan.js
@@ -0,0 +1,21 @@
const { resolve } = require('path');
const { scanDeep } = require('../util');

/**
* @typedef {object} scanOptions
* @memberof fsn/nextra
* @property {Function} [filter] A filter function recieving (stats, path) to determin if the returned map should include a given entry
* @property {number} [depthLimit] How many directories deep the scan should go (0 is just the children of the passed root directory, no subdirectory files)
*/

/**
* Recursivly scans a directory, returning a map of stats keyed on the full path to the item.
* @function scan
* @memberof fsn/nextra
* @param {string} root The path to scan
* @param {scanOptions} [options = {}] The options for the scan
* @return {Promise<Map<string, Stats>>}
*/
module.exports = function scan(root, options = {}) {
return scanDeep(resolve(root), new Map(), -1, options);
};
9 changes: 9 additions & 0 deletions src/util.js
Expand Up @@ -180,6 +180,15 @@ exports.isSrcKid = (src, dest) => {
}
};

exports.scanDeep = async (dir, results, level, options) => {
const stats = await lstat(dir);
if (!options.filter || options.filter(stats, dir)) results.set(dir, stats);
if (stats.isDirectory() && (typeof options.depthLimit === 'undefined' || level < options.depthLimit)) {
await Promise.all((await readdir(dir)).map(part => this.scanDeep(join(dir, part), results, ++level, options)));
}
return results;
};

exports.uuid = () => {
const id = randomBytes(32).toString('hex');
return (Array(32).join(0) + id).slice(-32).replace(/^.{8}|.{4}(?!$)/g, '$&-');
Expand Down
44 changes: 44 additions & 0 deletions test/scan.js
@@ -0,0 +1,44 @@
const ava = require('ava');
const { tempFile, tempFileLoc, tempDir, tempDirLoc } = require('./lib');
const nextra = require('../src');

ava('file', async test => {
const file = tempFile();
const map = await nextra.scan(file);
await test.true(map.size === 1 && map.get(file).isFile());
});

ava('non-existant file', async test => {
await test.throws(nextra.scan(tempFileLoc()));
});

ava('empty directory', async test => {
const dir = tempDir();
const map = await nextra.scan(dir);
test.true(map.size === 1 && map.get(dir).isDirectory());
});

ava('full directory', async test => {
const dir = tempDir();
const file = tempFile(dir);
const map = await nextra.scan(dir, { filter: stats => stats.isFile() });
test.true(map.size === 1 && map.has(file));
});

ava('non-existant directory', async test => {
await test.throws(nextra.scan(tempDirLoc()));
});

ava('deep directory', async test => {
const dir = tempDir();
const file = tempFile(tempDir(dir));
const map = await nextra.scan(dir, { filter: stats => stats.isFile() });
test.true(map.size === 1 && map.has(file));
});

ava('deep directory w/ limit', async test => {
const dir = tempDir();
const file = tempFile(tempDir(dir));
const map = await nextra.scan(dir, { filter: stats => stats.isFile(), depthLimit: 0 });
test.true(map.size === 0 && !map.has(file));
});
7 changes: 7 additions & 0 deletions typings/index.d.ts
Expand Up @@ -9,6 +9,7 @@ declare module 'fs-nextra' {
export function chmod(path: string|Buffer|URL, mode?: number): Promise<void>;
export function chown(path: string|Buffer|URL, uid: number, gid: number): Promise<void>;
export function close(fd: number): Promise<void>;
export function copyFile(existingPath: string|Buffer|URL, newPath: string|Buffer|URL, flags?: number): Promise<void>;
export function createReadStream(path: string|Buffer|URL, options?: readStreamOptions|string): Promise<void>;
export function createWriteStream(path: string|Buffer|URL, options?: writeStreamOptions|string): Promise<void>;
export function exists(path: string|Buffer|URL): Promise<boolean>;
Expand Down Expand Up @@ -186,6 +187,7 @@ declare module 'fs-nextra' {
export function writeJSON(file: string, object: Object, options?: jsonOptions, atomic?: boolean): Promise<void>;
export function writeFileAtomic(file: string, data: string|Buffer|Uint8Array, options?: writeOptions|string): Promise<void>;
export function symlinkAtomic(source: string, destination: string, type?: SymLinkType): Promise<void>;
export function scan(path: string, options?: scanOptions): Promise<Map<string, Stats>>;
export function remove(path: string, options?: removeOptions): Promise<void>;
export function readJson(file: string, options?: readJSONOptions|string): Promise<Object>;
export function readJSON(file: string, options?: readJSONOptions|string): Promise<Object>;
Expand Down Expand Up @@ -250,6 +252,11 @@ declare module 'fs-nextra' {

export type SymLinkType = 'dir'|'file';

export type scanOptions = {
filter?: Function;
depthLimit?: number;
};

export type jsonOptions = {
replacer: Function;
spaces?: number;
Expand Down

0 comments on commit d3d017f

Please sign in to comment.