πŸš€ Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more β†’
Socket
Sign inDemoInstall
Socket

fs-hierarchy

Package Overview
Dependencies
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fs-hierarchy

create a hierarchy map of files and folders

2.0.4
latest
Source
npm
Version published
Weekly downloads
33
312.5%
Maintainers
1
Weekly downloads
Β 
Created
Source

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
filter
type
empty: boolean
match: string | string[]
options: MatchOptions
rule: MatchRule
filter options the resulting Hierarchy object
flattenbooleanwhen true , the hierarchy will be flattened.
include
type
extension: boolean
pathname: boolean
stats: boolean
type: boolean
included in the return object
rootNamestringthe used text of the root node
symlinksbooleanwhen true , symlinks are followed

Keywords

hierarchy

FAQs

Package last updated on 06 May 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts