项目作者: nemoxps

项目描述 :
Retrieves the directory structure of a path.
高级语言: JavaScript
项目地址: git://github.com/nemoxps/dir-tree.git
创建时间: 2017-04-15T23:24:06Z
项目社区:https://github.com/nemoxps/dir-tree

开源协议:MIT License

下载


dir-tree

Retrieves the directory structure of a path.

Installation

Unfortunately the name dir-tree is already taken at npm (and I don’t have a better one), so for now you need to install it this way:

  1. $ npm install nemoxps/dir-tree --save

API

dir-tree provides 2 functions to retrieve a directory tree.

  1. /**
  2. * @param {string} dirPath A directory path.
  3. * @param {(number|boolean)} [depth=0]
  4. * {number} A depth of recursive searching.
  5. * {boolean} `true` equals `Infinity`, `false` equals `0`.
  6. * @returns {Promise} `Promise.then({Directory} tree, {Error} err);`
  7. */
  8. dirTree(dirPath, depth)
  1. /**
  2. * @param {Object} dirList A directory list.
  3. * @returns {Promise} `Promise.then({Directory[]} trees, {Error} err);`
  4. */
  5. dirTree.of(dirList)

A Directory object has the following properties:

  • {string} path: The directory’s path
  • {string} name: The directory’s name
  • {Directory[]} dirs: An array of sub-directories
  • {File[]} files: An array of sub-files
  • {number} size: The directory’s size (in bytes)
  • {boolean} isSearched: Indicates whether dirs and files contain the sub-directories and sub-files or not
  • {boolean} isFullySearched: Indicates whether every sub-(sub-*)directory is searched or not
  • {string} type = 'directory': Shows that the object is a directory

And methods:

  1. /**
  2. * @param {boolean} [useColors=false] `true` if the output should be colorful.
  3. * @returns {string} The stringified Directory.
  4. */
  5. Directory.prototype.toString(useColors)
  6. /*
  7. toString() outputs something like this (the separator depends on the OS):
  8. rootPath/
  9. ├── dir-1/
  10. │ ├── dir-11/
  11. │ │ └── file-11-1.txt
  12. │ ├── dir-12/
  13. │ ├── dir-13/
  14. │ └── file-1-1.txt
  15. ├── dir-2/
  16. │ └── file-2-1.txt
  17. ├── file-1.txt
  18. └── file-2.txt
  19. */
  1. /**
  2. * @param {boolean} [thisInclusive=true] `true` if this Directory should appear in the output.
  3. * @returns {Directory[]} The list of directories.
  4. */
  5. Directory.prototype.getDirectories(thisInclusive)
  1. /**
  2. * @returns {File[]} The list of files.
  3. */
  4. Directory.prototype.getFiles()

A File object has the following properties:

  • {string} path: The file’s path
  • {string} name: The file’s basename (name + ext)
  • {string} ext: The file’s extension
  • {number} size: The file’s size (in bytes)
  • {string} type = 'file': Shows that the object is a file

Usage

Retrieving a complete directory tree:

  1. let dirTree = require('dir-tree');
  2. dirTree('path/to/my/folder', true)
  3. .then((tree) => {
  4. // do something
  5. })
  6. .catch((err) => {
  7. // something went wrong
  8. });

Retrieving multiple directories:

  1. let dirTree = require('dir-tree');
  2. dirTree.of({
  3. 'path/to/dir1': {
  4. 'path/to/subdir1': true,
  5. 'path/to/subdir2': false,
  6. },
  7. 'path/to/dir2': 1,
  8. })
  9. .then((trees) => {
  10. // do something
  11. })
  12. .catch((err) => {
  13. // something went wrong
  14. });