# spelunk

> Traverse a folder in node, turning its contents into an object for easy consumption

Latest version **0.5.0** (published 2015-10-08) · 0 weekly downloads

## Install

```sh
npm install spelunk
pnpm add spelunk
yarn add spelunk
bun add spelunk
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.5.0 |
| Published | 2015-10-08 |
| First published | 2014-01-12 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Known vulnerabilities | 0 (+5 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 30 |
| Author | Rich Harris |
| Maintainers | rich_harris |

## Links

- npm: https://www.npmjs.com/package/spelunk
- Repository: https://github.com/Rich-Harris/spelunk
- Issues: https://github.com/Rich-Harris/spelunk/issues
- npm.io page: https://npm.io/package/spelunk

## Dependencies (3)

- [minimatch](https://npm.io/package/minimatch.md) ~0.2.14
- [es6-promise](https://npm.io/package/es6-promise.md) ^1.0.0
- [graceful-fs](https://npm.io/package/graceful-fs.md) ~2.0.1

## Recent versions

- 0.5.0 (latest) — 2015-10-08
- 0.4.0 — 2015-04-26
- 0.3.0 — 2014-09-26
- 0.2.0 — 2014-09-13
- 0.1.2 — 2014-07-17
- 0.1.1 — 2014-01-13
- 0.1.0 — 2014-01-12

## README

# spelunk.js

```
$ npm install spelunk
```

**spelunk.js** turns a folder into an object. This folder...

```
data
|- config.json
|- tables
   |- population.csv
   |- growth.csv
|- slides
   |- 0.txt
   |- 1.txt
   |- 2.txt
   |- 3.txt
|- i18n
   |- en-GB.json
   |- en-US.json
   |- fr.json
   |- de.json
   |- ...
```

...becomes this object:


```js
{
  config: { <contents of config.json> }, // parsed as JSON
  tables: {
    population: <contents of population.csv> // as a string
    growth:     <contents of growth.csv>
  },
  slides: [
    <contents of 0.txt>,  // because these files have
    <contents of 1.txt>,  // numeric names, `slides` is
    <contents of 2.txt>,  // an array, not an object
    <contents of 3.txt>
  ],
  i18n: {
    "en-GB": <contents of en-GB.json>,
    "en-US": <contents of en-US.json>,
    "fr":    <contents of fr.json>,
    "de":    <contents of de.json>,
    ...
  }
}
```


If a file contains JSON, it is parsed as JSON; if not, it is treated as text. If a folder only contains items with numeric filenames (as in the case of the `slides` folder above), it will become an array rather than an object.


## Usage

spelunk.js uses the standard Node callback pattern...

```js
callback = function ( error, result ) {
  if ( error ) {
    return handleError( error );
  }

  doSomething( result );
};

spelunk( 'myFolder', options, callback ); // you can omit options
```

...but it also returns a promise, because this is 2015 dammit and callbacks are a lousy flow control mechanism:

```js
spelunk( 'myFolder', options ).then( doSomething, handleError );
```

### Synchronous usage

```js
var result = spelunk.sync( 'myFolder', options );
```


## Options

### options.exclude

Exclude files that match a certain pattern (this uses [minimatch](https://github.com/isaacs/minimatch) syntax):

```js
spelunk( 'myFolder', { exclude: '**/README.md' }).then( doSomething );
```

The value of `exclude` can be an string, or an array of strings.

### options.keepExtensions

If you have multiple files with the same name but different extensions, they'll conflict. This option allows you to keep their extensions, e.g. `result['config.json']` instead of `result.config` (but really, you're better off keeping your filenames distinct).

```js
spelunk( 'myFolder', { keepExtensions: true }).then( doSomething );
```


## Why the name?

Because traversing a folder tree and mapping all its nooks and crannies feels a bit like spelunking. Plus it's fun to say.

## Testing

```
$ npm run test
```

## License

[MIT](LICENSE.md), copyright 2014 [@Rich_Harris](http://twitter.com/Rich_Harris)

---
_Source: https://npm.io/package/spelunk · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
