包详细信息

@wrote/read-dir-structure

wrote1.4kMIT1.3.1

Reads directory structure.

@wrote, directory, dir, folder

自述文件

@wrote/read-dir-structure

npm version

@wrote/read-dir-structure is Node.JS package to a read directory structure.

yarn add @wrote/read-dir-structure

Table Of Contents

API

There is a single default export function, import it with the following statement:

import readDirStructure from '@wrote/read-dir-structure'

The types and externs for Google Closure Compiler via Depack are defined in the _readDirStructure namespace.

Types

The return type of the function is a DirectoryStructure. It is a recursive object, where items have either File, Directory or SymLink types specified in the type field, and if the item is a directory, it has the content property which is another DirectoryStructure.

Object<string, !DirectoryStructure> Content: The recursive content of the directory.

DirectoryStructure: A directory structure representation.

Name Type Description
type string The type of the item.
content !Content The recursive content if the item is a directory.

async readDirStructure(
  dirPath: string,
  opts=: !ReadDirStructureOpts,
): DirectoryStructure

Read a directory, and return its structure as an object. Only Files, Directories and Symlinks are included!

  • <kbd>dirPath*</kbd> string: Path to the directory.
  • <kbd>opts</kbd> !ReadDirStructureOpts (optional): The options.

Reads the structure of the directory.

/* yarn example/ */
import readDirStructure from '@wrote/read-dir-structure'

(async () => {
  const res = await readDirStructure('example/directory')
  console.log(JSON.stringify(res, null, 2))
})()

Output for the example/directory:

{
  "content": {
    "fileA-ln.txt": {
      "type": "SymbolicLink"
    },
    "fileA.txt": {
      "type": "File"
    },
    "fileB.txt": {
      "type": "File"
    },
    "test.json": {
      "type": "File"
    },
    "subdirectory": {
      "content": {
        "subdirFileA.txt": {
          "type": "File"
        },
        "subdirFileB.txt": {
          "type": "File"
        }
      },
      "type": "Directory"
    }
  },
  "type": "Directory"
}

getFiles(
  content: !Content,
  path: string,
): !Array<string>

After running the readDirStructure, this function can be used to flatten the content output and return the list of all files (not including symlinks).

  • <kbd>content*</kbd> !Content: The content from the readDirStructure result.
  • <kbd>path*</kbd> string: The original path to the directory.
/* yarn example/ */
import readDirStructure, { getFiles } from '@wrote/read-dir-structure'

(async () => {
  const path = 'example/directory'
  const res = await readDirStructure(path)
  const files = getFiles(res.content, path)
  console.log(JSON.stringify(files, null, 2))
})()
[
  "example/directory/fileA.txt",
  "example/directory/fileB.txt",
  "example/directory/test.json",
  "example/directory/subdirectory/subdirFileA.txt",
  "example/directory/subdirectory/subdirFileB.txt"
]

Reasons for Errors

The following errors can happen and have been context tested against:

Happens when... code Message
not passing any path - Please specify a path to the directory
passing a path to a symbolic link ENOTDIR Path is not a directory
passing a path to a file ENOTDIR Path is not a directory
directory does not exist ENOENT ENOENT: no such file or directory, lstat '%DIRECTORY%'

Copyright

Art Deco © Art Deco for Wrote 2019 Wrote Tech Nation Visa Tech Nation Visa Sucks

更新日志

3 February 2019

1.3.1

  • [fix] Update makePromise.

25 November 2019

1.3.0

  • [feature] Ignore specific directories when traversing the structure.
  • [api] Create the api.xml file, compile with Closure Compiler.

10 April 2019

1.2.0

  • [externs] Provide externs for Google Closure Compiler via Depack.
  • [doc] Document types recursively.
  • [doc] Update makepromise.

5 April 2019

1.1.0

  • [feature] Implement getFiles method.

2 April 2019

1.0.3

  • [deps] Update makepromise.

15 January 2019

1.0.2

  • [package] Add the "module" field.
  • [package] Remove source maps, migrate to ÀLaMode.

4 July 2018

1.0.1

  • [package] Move to wrote org on github, add tags.
  • [doc] Embed output of the example.

21 June 2018

1.0.0

  • Create @wrote/read-dir-structure with [mnp][https://mnpjs.org]
  • [repository]: src, test