2.0.3 • Published 6 days ago

fs-hierarchy v2.0.3

Weekly downloads
2
License
MIT
Repository
github
Last release
6 days ago

fs-hierarchy

Create a hierarchy map of a filesystem using node's built-in fs.

You can use the CLI or include it directly in your project. It returns a structure that can be used in different contexts like for d3-hierarchy.

Additionally it is possible:

  • to include extra informations like the file extension, the absolute path, the type and stats
  • filter paths and names
  • follow symbolic links
  • in CLI: output in json, yaml or in a tree to stdout or file

Version Downloads/week License

CLI

Using the CLI, fs-hierarchy supports different output formats and options.

Commands

fs-hierarchy [DIR] [OUTPUT]

Create a hierarchy map of a filesystem using node's built-in fs.

USAGE
  $ fs-hierarchy  [DIR] [OUTPUT] [-e] [--flat] [-f tree|yaml|json] [-h] [-i ext|path|stats|type] [-m
    <value>] [-M all|none|some] [--minify] [-r <value>] [-s] [-v]

ARGUMENTS
  DIR     [default: .] path to create a hierarchy from
  OUTPUT  output filename

FLAGS
  -M, --match-rule=<option>  [default: some] rule for matching paths
                             <options: all|none|some>
  -e, --empty                include child nodes that have no children
  -f, --format=<option>      [default: json] the used output format
                             <options: tree|yaml|json>
  -h, --help                 show this help
  -i, --include=<option>...  the included informations in return object
                             <options: ext|path|stats|type>
  -m, --match=<value>...     filter matching paths
  -r, --root=<value>         the used name for the root-folder
  -s, --symlinks             follow symbolic links
  -v, --version              show the version
      --flat                 flatten the output
      --minify               minify the output

DESCRIPTION
  Create a hierarchy map of a filesystem using node's built-in *fs*.

FLAG DESCRIPTIONS
  -M, --match-rule=all|none|some  rule for matching paths

    when set to **all** all filters must resolve successfully,
    when set to **some** at least one filter must resolve successfully,
    when set to **none** no filter must resolve successfully

  -m, --match=<value>...  filter matching paths

    use glob pattern for matching
    negate by leading '!'
    e.g. -m '**/*.ts' '!**/node_modules/**'

  --flat  flatten the output

    if true the full path will be included by default. using tree format the full path will be used instead of the
    filenames

  --minify  minify the output

    only for json format

JSON

$ fs-hierarchy ./src -m 'index.ts'
{
  "name": "./src",
  "children": [
    {
      "name": "commands",
      "children": [
        {
          "name": "index.ts"
        }
      ]
    },
    {
      "name": "index.ts"
    },
    {
      "name": "lib",
      "children": [
        {
          "name": "index.ts"
        }
      ]
    }
  ]
}

Tree

$ fs-hierarchy ./src -f tree -m '*.ts'
./src
 ├─ commands
 │  ╰─ index.ts
 ├─ index.ts
 ╰─ lib
    ├─ format
    │  ├─ json.ts
    │  ├─ tree.ts
    │  ╰─ yaml.ts
    ├─ hierarchy.ts
    ├─ index.ts
    ├─ types.ts
    ├─ utils
    │  ├─ factories.ts
    │  ├─ flatten.ts
    │  ├─ leaf.ts
    │  ├─ read-dir.ts
    │  ├─ type.ts
    │  ╰─ typeguards.ts
    ╰─ write
       ├─ file.ts
       ╰─ stdout.ts

More Examples

Programmatic use

$ npm install fs-hierarchy
const { hierarchy } = require('fs-hirarchy');

const root = path.resolve(__dirname, 'files');
const options = {
  filter: { match: '*.json' },
  include: { path: true },
  rootName: 'HomeSweetHome'
};

const matches = hierarchy(root, options);

hierarchy

function

Creates a hierarchy tree structure based on the given root path and options.

defined in fs-hierarchy/src/lib/hierarchy.ts

parameters

NameTypeDescription
root*stringThe root path of the hierarchy.
options*OptionsThe options for configuring the hierarchy.
returnsLeaf | NodeThe hierarchy tree structure.

Type

union

Types of a Leaf or Node entry

defined in fs-hierarchy/src/lib/types.ts

values

"block-device" | "char-device" | "dir" | "file" | "pipe" | "socket" | "symlink"

Leaf

type

a Leaf of the hierarchy

defined in fs-hierarchy/src/lib/types.ts

properties

NameTypeDescription
extensionstringoptionally included extension (only for Leaf s)
name*stringthe name of the entry (without the base path)
pathstringoptionally included absolute path
statsStatsA fs.Stats object provides information about a file. Objects returned from [{@link lstat} and {@link fstat](stat},) and their synchronous counterparts are of this type. If bigint in the options passed to those methods is true, the numeric values will be bigint instead of number , and the object will contain additional nanosecond-precision properties suffixed with Ns . console Stats { dev: 2114, ino: 48064969, mode: 33188, nlink: 1, uid: 85, gid: 100, rdev: 0, size: 527, blksize: 4096, blocks: 8, atimeMs: 1318289051000.1, mtimeMs: 1318289051000.1, ctimeMs: 1318289051000.1, birthtimeMs: 1318289051000.1, atime: Mon, 10 Oct 2011 23:24:11 GMT, mtime: Mon, 10 Oct 2011 23:24:11 GMT, ctime: Mon, 10 Oct 2011 23:24:11 GMT, birthtime: Mon, 10 Oct 2011 23:24:11 GMT } bigint version: console BigIntStats { dev: 2114n, ino: 48064969n, mode: 33188n, nlink: 1n, uid: 85n, gid: 100n, rdev: 0n, size: 527n, blksize: 4096n, blocks: 8n, atimeMs: 1318289051000n, mtimeMs: 1318289051000n, ctimeMs: 1318289051000n, birthtimeMs: 1318289051000n, atimeNs: 1318289051000000000n, mtimeNs: 1318289051000000000n, ctimeNs: 1318289051000000000n, birthtimeNs: 1318289051000000000n, atime: Mon, 10 Oct 2011 23:24:11 GMT, mtime: Mon, 10 Oct 2011 23:24:11 GMT, ctime: Mon, 10 Oct 2011 23:24:11 GMT, birthtime: Mon, 10 Oct 2011 23:24:11 GMT }
typeTypeTypes of a Leaf or Node entry

Node

type

a Node of the hierarchy

defined in fs-hierarchy/src/lib/types.ts

properties

NameTypeParentDescription
children*((Leaf, Node))[]children of the Node
statsStatsLeafA fs.Stats object provides information about a file. Objects returned from [{@link lstat} and {@link fstat](stat},) and their synchronous counterparts are of this type. If bigint in the options passed to those methods is true, the numeric values will be bigint instead of number , and the object will contain additional nanosecond-precision properties suffixed with Ns . console Stats { dev: 2114, ino: 48064969, mode: 33188, nlink: 1, uid: 85, gid: 100, rdev: 0, size: 527, blksize: 4096, blocks: 8, atimeMs: 1318289051000.1, mtimeMs: 1318289051000.1, ctimeMs: 1318289051000.1, birthtimeMs: 1318289051000.1, atime: Mon, 10 Oct 2011 23:24:11 GMT, mtime: Mon, 10 Oct 2011 23:24:11 GMT, ctime: Mon, 10 Oct 2011 23:24:11 GMT, birthtime: Mon, 10 Oct 2011 23:24:11 GMT } bigint version: console BigIntStats { dev: 2114n, ino: 48064969n, mode: 33188n, nlink: 1n, uid: 85n, gid: 100n, rdev: 0n, size: 527n, blksize: 4096n, blocks: 8n, atimeMs: 1318289051000n, mtimeMs: 1318289051000n, ctimeMs: 1318289051000n, birthtimeMs: 1318289051000n, atimeNs: 1318289051000000000n, mtimeNs: 1318289051000000000n, ctimeNs: 1318289051000000000n, birthtimeNs: 1318289051000000000n, atime: Mon, 10 Oct 2011 23:24:11 GMT, mtime: Mon, 10 Oct 2011 23:24:11 GMT, ctime: Mon, 10 Oct 2011 23:24:11 GMT, birthtime: Mon, 10 Oct 2011 23:24:11 GMT }
typeTypeLeafTypes of a Leaf or Node entry
name*stringLeafthe name of the entry (without the base path)
pathstringLeafoptionally included absolute path

Hierarchy

union

defined in fs-hierarchy/src/lib/types.ts

values

Leaf | Node

MatchOptions

type

`minimatch` options for filtering (https://github.com/isaacs/minimatch#options)

defined in fs-hierarchy/src/lib/types.ts

properties

NameTypeParent
dotboolean
matchBaseboolean
nobracebooleanMinimatchOptions
nocommentbooleanMinimatchOptions
nonegatebooleanMinimatchOptions
debugbooleanMinimatchOptions
noglobstarbooleanMinimatchOptions
noextbooleanMinimatchOptions
nonullbooleanMinimatchOptions
windowsPathsNoEscapebooleanMinimatchOptions
allowWindowsEscapebooleanMinimatchOptions
partialbooleanMinimatchOptions
nocasebooleanMinimatchOptions
nocaseMagicOnlybooleanMinimatchOptions
magicalBracesbooleanMinimatchOptions
flipNegatebooleanMinimatchOptions
preserveMultipleSlashesbooleanMinimatchOptions
optimizationLevelnumberMinimatchOptions
platform"aix" | "android" | "darwin" | "freebsd" | "haiku" | "linux" | "openbsd" | "sunos" | "win32" | "cygwin" | "netbsd"MinimatchOptions
windowsNoMagicRootbooleanMinimatchOptions

MatchRule

union

The logical rule how filter patterns should be applied when set to `all` all filters must resolve successfully, when set to `some` at least one filter must resolve successfully, when set to `none` no filter must resolve successfully

defined in fs-hierarchy/src/lib/types.ts

values

"all" | "none" | "some"

Options

type

Use the options when you want to filter the resulting Hierarchy object or want to include extra informations.

defined in fs-hierarchy/src/lib/types.ts

properties

NameTypeDescription
filtertypeempty: booleanmatch: string | string[]options: MatchOptionsrule: MatchRulefilter options the resulting Hierarchy object
flattenbooleanwhen true , the hierarchy will be flattened.
includetypeextension: booleanpathname: booleanstats: booleantype: booleanincluded in the return object
rootNamestringthe used text of the root node
symlinksbooleanwhen true , symlinks are followed
2.0.3

6 days ago

2.0.2

6 days ago

2.0.1

6 days ago

2.0.0

8 days ago

1.2.2

3 years ago

1.2.1

3 years ago

1.2.0

4 years ago

1.1.3

4 years ago

1.1.1

4 years ago

1.1.2

4 years ago

1.1.0

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

1.0.0-0

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.6

4 years ago

0.0.3

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago