npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

file-structure-tree

v1.0.5

Published

This Node.js script is designed to print a visual representation of the directory structure of the current directory

Downloads

7

Readme

File-Structure-tree

install

In terminal

npm install -g node-structure-tree

Usage

fstree

Result

node-youtube
├── ./01Tutorial
│   ├── index.js
│   ├── math.js
│   └── server.js
├── ./02Tutorial
│   ├── ./files
│   │   ├── lorem.txt
│   │   ├── newReply.txt
│   │   └── starter.txt
│   └── index.js
├── ./test
│   ├── ./tes
│   │   ├── ./mr
│   │   │   ├── mr.js
│   │   │   └── sd.js
│   │   ├── fe.txt
│   │   └── te.txt
│   ├── generateTree.js
│   └── west.txt
├── generateTree.js
└── README.md

This Node.js script is designed to print a visual representation of the directory structure of the current directory where the script is located. Here's a step-by-step explanation:

  1. Imports and Function Definition:
const fs = require('fs');
const path = require('path');
  • fs is the Node.js File System module used to read and interact with the filesystem.
  • path is a Node.js module used to handle and transform file paths.
  1. printTree Function:
function printTree(dir, indent = '') {
    ...
}
  • This function takes a directory path dir and an optional indent string used for formatting the output.
const items = fs.readdirSync(dir).filter(file => !file.startsWith('.'));
  • Read all items ( files and directories ) in the given directory dir
  • Filters out hidden files ( those start with a dot . ).
const directories = items.filter(file => fs.statSync(path.join(dir, file)).isDirectory());
const files = items.filter(file => !fs.statSync(path.join(dir, file)).isDirectory());
  • Separates items into directories and files by checking if each item is a directory or not directory.
const sortedItems = [...directories, ...files].sort((a, b) => {
    const aStat = fs.statSync(path.join(dir, a));
    const bStat = fs.statSync(path.join(dir, b));

    if (aStat.isDirectory() && !bStat.isDirectory()) return -1;
    if (!aStat.isDirectory() && bStat.isDirectory()) return 1;
    return a.localeCompare(b);
});
  • Combines directories and files into one list, with directories coming before files.
  • Sort the combined list. Directories come before files in the list.
  • If both are of the same type, it sorted Alphabetically.
sortedItems.forEach((item, index) => {
    const fullPath = path.join(dir, item);
    const isDirectory = fs.statSync(fullPath).isDirectory();
    const last = index === sortedItems.length - 1;
  • Iterates over each item in the sorted list.
  • Determines if the item is a directory and if it is the last item in the list ( for use formatting purposes ).
const displayName = isDirectory ? `./${item}` : item;

console.log(`${indent}${last ? '└── ' : '├── '}${displayName}`);
  • Set a displayName to prefix directions witch ./.
  • Print the item with a grapical tree representation. Uses └── for the last item and ├── for the others.
if (isDirectory) {
    printTree(fullPath, `${indent}${last ? '    ' : '│   '}`);
}
  • If the item is a directory, it recursively calls printTree to print its content, with adjusted indentation.
  1. Execution:
const currentDirectoryPath = path.dirname(__filename);
const currentDirectoryName = path.basename(currentDirectoryPath);
console.log(currentDirectoryName);
printTree(currentDirectoryPath);
  • path.dirname(__filename) gets the directory path of the current script.
  • path.basename(currentDirectoryPath) gets the name of that directory.
  • Print the name of this directory.
  • Call printTree to print the directory structure of the current directory.

Overall, this script will produce a text-based tree view of the directory structure where it is executed, showing directories and files in a structured, indented format.