Socket
Socket
Sign inDemoInstall

node-json-file-tree

Package Overview
Dependencies
10
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    node-json-file-tree

Node Js module to generate a tree structure representation of a folder structure / file system.


Version published
Maintainers
1
Created

Readme

Source

Json File Tree for Node JS - IoT Shaman

npm badge Build Status Coverage Status

Tree structure representing the contents of a given system drive. Simply pass in a start folder, or a list of files / folders, and this module will return an object that you can use in a user interface to represent your files!

Requirements

In order to use node-json-file-tree you will need the following resource(s):

  • npm

Installation

npm install node-json-file-tree --save

Quick Start

The file tree structure can be created in 1 of 2 ways, either by providing a list of files / folders to it's constructor, or using the "seed" factory and providing an address for the root directory.

File Tree Seed (factory method)

Use this factory method to generate a file "seed". A seed will "grow" a file tree for you, first running a recursive directory scan, then converting the results into a file tree. This method is asynchornous and is provided as a Promise.

Methods

get(windows?: boolean = false): Promise<FileTreeSeed>;

Implementation

let ftsf = require('node-json-file-tree').FileTreeSeedFactory
// generate file tree seed from factory
ftsf.get().then(seed => { 
    // "grow" file tree from seed
    let start = __dirname;
    seed.getFileTree(start).then(tree => {
        // ...
    });
});
Manaul (via constructor)

When you already have a list of files / folders, you can manually populate a new instance of FileTree.

let FileTree = require('node-json-file-tree').FileTree;
let tree = new FileTree(['/path/to/file.txt'], ['/path/to/']);
// ...
File Tree Structure

The file tree object has the below definition (in Typescript):

interface FileTree {
    files: string[];
    folders: string[];
    nodes: FileTreeNode;
}
interface FileTreeNode {
    _files_: string[];
    [folder: string]: FileTreeNode;
}

Given the below list of files / folders, the file tree would look like:

  • /small/path.txt
  • /longer/folder/path2.txt
{
    "small": {
        "_files_": [ "path.txt" ]
    },
    "longer": {
        "folder": {
            "_files_": [ "path2.txt" ]
        },
        "_files_": []
    },
    "_files_": []
}

API Reference

interface FileTreeApi {
	getDirectoryFromPath(path: string, isFilePath?: boolean = false): FileTreeNode;
	addFolder(path: string, folderName: string, seed?: any = null): void;
	renameFolder(path: string, oldName: string, newName: string): void;
	renameFile(path: string, oldName: string, newName: string): void;
	copyFolder(fromPath: string, toPath: string, folderName: string): void;
	deleteFolder(folder: string): void;
	deleteFiles(folder: string, files: string[]): void;
	cloneNodeByPath(path: string): void;
}

--eof

FAQs

Last updated on 14 Aug 2018

Did you know?

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc