Coder Social home page Coder Social logo

find-folder's Introduction

find-folder

Get all files in a directory

🚀 Usage

1. installation

  npm install --save find-folder

2. Usage:

If you have the following folders

➜  root pwd
/tmp/root
➜  root tree
.
├── a
│   └── index.js
└── b
    └── index.js
const find = require('find-folder');
(async () => {
  const data = await find('/tmp/root');
  console.log(data);
})();
// Return the following contents
// [ { path: '/tmp/root', type: 'dir' },
//   { path: '/tmp/root/a', type: 'dir' },
//   { path: '/tmp/root/b', type: 'dir' },
//   { path: '/tmp/root/a/index.js', type: 'file' },
//   { path: '/tmp/root/b/index.js', type: 'file' } ]

3. option:

path: Initialization path, example

const find = require('find-folder');
(async () => {
  const data = await find({ path: '/tmp/root' });
  console.log(data);
})();
// Return the following contents
// [ { path: '/tmp/root', type: 'dir' },
//   { path: '/tmp/root/a', type: 'dir' },
//   { path: '/tmp/root/b', type: 'dir' },
//   { path: '/tmp/root/a/index.js', type: 'file' },
//   { path: '/tmp/root/b/index.js', type: 'file' } ]

type: There are two ways to search: DFS depth first and BFS breadth first, default breadth first.

const find = require('find-folder');
(async () => {
  const data = await find({ path: '/tmp/root', type: 'DFS' });
  console.log(data);
})();
// Return the following contents
// [ { path: '/tmp/root', type: 'dir' },
//   { path: '/tmp/root/a', type: 'dir' },
//   { path: '/tmp/root/a/index.js', type: 'file' },
//   { path: '/tmp/root/b', type: 'dir' },
//   { path: '/tmp/root/b/index.js', type: 'file' } ]

ignore: Ignored folder

const find = require('find-folder');
(async () => {
  const data = await find({ path: '/tmp/root', ignore: ['/tmp/root/b'] });
  console.log(data);
})();
// Return the following contents
//[ { path: '/tmp/root', type: 'dir' },
//  { path: '/tmp/root/a', type: 'dir' },
//  { path: '/tmp/root/a/index.js', type: 'file' },

depth: Depth of search

const find = require('find-folder');
(async () => {
  const data = await find({ path: '/tmp/root', ignore: ['/tmp/root/b'] });
  console.log(data);
})();
// Return the following contents
// [ { path: '/tmp/root', type: 'dir' },
// { path: '/tmp/root/a', type: 'dir' },
// { path: '/tmp/root/b', type: 'dir' } ]

file type:

{
  FILE: 'FILE',
  DIRECTORY: 'DIRECTORY',
  SYMBOLICLINK: 'SYMBOLICLINK',
  FIFO: 'FIFO',
  SOCKET: 'SOCKET',
  BLOCKDEVICE: 'BLOCKDEVICE',
  CHARACTERDEVICE: 'CHARACTERDEVICE',
  UNKNOWN: 'UNKNOWN',
}

4. unit testing:

  npm run test
> [email protected] test find-folder
> sh ./test/dir.sh && jest

 PASS  test/index.test.js
  ✓ defualt (36ms)
  ✓ BFS (17ms)
  ✓ DFS (3ms)
  ✓ ignore (4ms)
  ✓ deep (2ms)
  ✓ test type (1ms)

Test Suites: 1 passed, 1 total
Tests:       6 passed, 6 total
Snapshots:   0 total
Time:        1.204s
Ran all test suites.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.