Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

map-folder

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

map-folder

Create a JSON representation of a folder structure tree

  • 1.2.7
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.2K
increased by2.47%
Maintainers
1
Weekly downloads
 
Created
Source

License: MIT Build Status

map-folder

Create a JSON representation of a folder structure tree.

Installation

$ npm install map-folder

Usage

const mapFolder = require('map-folder');

// sync
const result = mapFolder('path/to/folder', {options});

// Async
const resultPromise = mapFolder('path/to/folder', {async: true});

Result

Example folder structure:

└─ my-project
   ├─ common
   │  └─ utils.js
   └─ index.js

Results:

{
    path: 'path/to/folder',
    isFile: false,
    name: 'my-project',
    entries: {
        'common': {
            path:'path/to/folder/common',
            name: 'common',
            isFile: false,
            entries: {
                "utils.js": {
                    path:'path/to/folder/common/utils.js',
                    isFile: true,
                    name: 'utils.js',
                    base: 'utils',
                    ext: 'js',
                }
            }
        },
        'index.js': {
            path:'path/to/folder/index.js',
            isFile: true,
            name: 'index.js',
            base: 'index',
            ext: 'js',
        }
    }
};

API


mapFolder(path, options)

Arguments:

  • path - A path to an existing folder.
  • options - Exclude/Include items. See below.

Return:

A JSON object (or a promise for JSON) that represents the target folder structure.

Options


async

Type: Boolean
Set to true when you want map-folder to map asyncronously and return a promise.
Default is false (sync).

skipEmpty

Type: Boolean
Empty folders are mapped by default but when using the include option empty folders are skipped by default. Change this behavior by setting skipEmpty with a boolean.

mapFolder('./my-project', {
    include: ['.js', '.json'],
    skipEmpty: false
})

include & exclude

By default all entries (file & folders) are mapped recursively. Use the exclude option to skip/ignore certain entries. Using the include option means only map the given items.

Both are array of strings, which are the entry names or file extensions you want to map. File extensions should start with a dot (e.g. '.log').
include also accept objects, see below.

Both are compared to the entry full name (and extension) by lower-cased comparison, meaning an 'abc' item will also match an 'ABC' folder.

When used together, excluded items will be substract from the included entries (include comes first). The only exception is when you include a folder, then it will be mapped completely (by default), with no exclusions. You can include sub-folders with their own options, with their own include/exclude lists etc.

exclude

Type: Array of strings
Use the exclude option to skip/ignore certain entries. Items are names and extensions.

// map everything but "node_modules"
mapFolder('./my-project', {
    exclude: ['node_modules']
})

// map everything but "node_modules" and log files
mapFolder('./my-project', {
    exclude: ['node_modules', '.log']
})

include

Type: Array of strings and objects
Use the include option when you only want to map certain entries in the target folder. Items are names and extensions.

// only map js files & the package.json file
mapFolder('./my-project', {
    include: ['.js', 'package.json']
})

By default, when including a folder name, the whole folder will be mapped, regardless of other options, including all entries in that folder. If you want a sub-folder to be mapped with its own rules you can pass in an object. This object is an options object but should also have the folder name (a name property).

// map all .js files in the target folder and 
// all .svg files in "icons" sub-folder
mapFolder('./my-project', {
    include: [
        '.js',
        {
            name: 'icons',
            include: ['.svg']
        }
    ]
})

The async option will be ignored for sub-folders, and they will all share the initial top async option value.

filter

Type: Predicate Function
If you need more control over which entries to map and which should be skipped you can pass a function to the filter option. This function gets called for every entry that was not handled by your include/exclude options. It gets called with an entryMap object (see below).

Return true to map the entry or false to skip it.

// map all folders and files that starts with an "a"
mapFolder('./my-project', {
    filter: ({isFile, base}) => !isFile || base.startsWith('a')
})

Entry Map Object

Every entry in the result (including the result itself) holds the following properties:

Prop nameTypeDescription
nameStringThe whole entry name. Includes file extensions.
pathStringthe absolute path to the entry.
isFileBooleantrue for folders, false for files.

Each type has its own additional properties:

  • Folder

    Prop nameTypeDescription
    entriesObjectA JSON object of the folder's child entryMaps (sub-folders and files).
  • File

    Prop nameTypeDescription
    baseStringThe file name without the extension.
    extStringThe file extension without a dot.

There are other entry types like symlinks, currently out of this module's scope.

Keywords

FAQs

Package last updated on 26 Jul 2020

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

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc