Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does npm draw the dependency tree?

npm does a nifty job of drawing a package's dependency hierarchy as a tree in the console:

$ npm ls
[email protected]
├── [email protected]
├─┬ [email protected]
│ ├─┬ [email protected]
│ │ └── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ └── [email protected]
└── [email protected]

How does npm do this?

like image 954
josh3736 Avatar asked Mar 15 '13 18:03

josh3736


3 Answers

npm uses the Unicode box drawing characters (U+2500-2800) to draw the pretty lines of the tree.

To draw a similar tree in your own application, the best route is probably to use the same module that npm itself uses – archy.

var archy = require('archy');
var s = archy({
  label : 'beep',
  nodes : [
    'ity',
    {
      label : 'boop',
      nodes : [
        {
          label : 'o_O',
          nodes : [
            {
              label : 'oh',
              nodes : [ 'hello', 'puny' ]
            },
            'human'
          ]
        },
        'party\ntime!'
      ]
    }
  ]
});
console.log(s);

Outputs

beep
├── ity
└─┬ boop
  ├─┬ o_O
  │ ├─┬ oh
  │ │ ├── hello
  │ │ └── puny
  │ └── human
  └── party
      time!
like image 113
josh3736 Avatar answered Nov 08 '22 08:11

josh3736


For list your folders and files you can use tree-cli:

https://www.npmjs.com/package/tree-cli

Just install:

npm install -g tree-cli

And use inside your folder:

tree -L 2, -d
like image 2
Liko Avatar answered Nov 08 '22 07:11

Liko


You could also use console2 which does almost the same thing as archy does, but gives you useful additional features like improved stack traces, object inspection and more:

enter image description here

Feature screenshot

Full disclosure: I'm the author of the repository

like image 1
RienNeVaPlu͢s Avatar answered Nov 08 '22 08:11

RienNeVaPlu͢s