Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Create a JSON representation of a folder structure tree.
$ npm install map-folder
const mapFolder = require('map-folder');
// sync
const result = mapFolder('path/to/folder', {options});
// Async
const resultPromise = mapFolder('path/to/folder', {async: true});
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',
}
}
};
mapFolder(path, options)
A JSON object (or a promise for JSON) that represents the target folder structure.
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')
})
Every entry in the result (including the result itself) holds the following properties:
Prop name | Type | Description |
---|---|---|
name | String | The whole entry name. Includes file extensions. |
path | String | the absolute path to the entry. |
isFile | Boolean | true for folders, false for files. |
Each type has its own additional properties:
Folder
Prop name | Type | Description |
---|---|---|
entries | Object | A JSON object of the folder's child entryMap s (sub-folders and files). |
File
Prop name | Type | Description |
---|---|---|
base | String | The file name without the extension. |
ext | String | The file extension without a dot. |
There are other entry types like symlinks, currently out of this module's scope.
FAQs
Create a JSON representation of a folder structure tree
The npm package map-folder receives a total of 1,072 weekly downloads. As such, map-folder popularity was classified as popular.
We found that map-folder demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
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.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.