# assemble-handle

> Assemble pipeline plugin for handling custom middleware stages.

Latest version **1.0.0** (published 2018-12-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install assemble-handle
pnpm add assemble-handle
yarn add assemble-handle
bun add assemble-handle
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2018-12-12 |
| First published | 2016-01-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=6 |
| Dependencies | 1 |
| Unpacked size | 10.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Jon Schlinkert |
| Maintainers | doowb, jonschlinkert |
| Keywords | assemble, assembleplugin, boilerplate, build, cli, cli-app, command-line, create, dev, development, framework, front, frontend, handle, handler, middleware, plugin, project, projects, run, scaffold, scaffolder, scaffolding, stage, template, templates, use, webapp, yeoman, yo |

## Links

- npm: https://www.npmjs.com/package/assemble-handle
- Repository: https://github.com/assemble/assemble-handle
- Issues: https://github.com/assemble/assemble-handle/issues
- npm.io page: https://npm.io/package/assemble-handle

## Dependencies (1)

- [through2](https://npm.io/package/through2.md) ^3.0.0

## Alternatives

- [@salesforce/cli](https://npm.io/package/@salesforce/cli.md) — 389.7K weekly downloads
- [@mintlify/cli](https://npm.io/package/@mintlify/cli.md) — 208.9K weekly downloads
- [@grafana/e2e-selectors](https://npm.io/package/@grafana/e2e-selectors.md) — 128.7K weekly downloads
- [mintlify](https://npm.io/package/mintlify.md) — 112.0K weekly downloads
- [@intlayer/cli](https://npm.io/package/@intlayer/cli.md) — 22.8K weekly downloads

## Recent versions

- 1.0.0 (latest) — 2018-12-12
- 0.1.4 — 2017-05-28
- 0.1.3 — 2016-07-21
- 0.1.2 — 2016-05-17
- 0.1.1 — 2016-05-17
- 0.1.0 — 2016-01-12

## README

# assemble-handle [![NPM version](https://img.shields.io/npm/v/assemble-handle.svg?style=flat)](https://www.npmjs.com/package/assemble-handle) [![NPM monthly downloads](https://img.shields.io/npm/dm/assemble-handle.svg?style=flat)](https://npmjs.org/package/assemble-handle) [![NPM total downloads](https://img.shields.io/npm/dt/assemble-handle.svg?style=flat)](https://npmjs.org/package/assemble-handle) [![Linux Build Status](https://img.shields.io/travis/assemble/assemble-handle.svg?style=flat&label=Travis)](https://travis-ci.org/assemble/assemble-handle)

> Assemble pipeline plugin for handling custom middleware stages.

Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support.

## Install

Install with [npm](https://www.npmjs.com/):

```sh
$ npm install --save assemble-handle
```

## Usage

```js
const handle = require('assemble-handle');
```

### handle

Handle middleware for the given middleware "stage".

```js
app.task('default', function() {
  return app.src('*.js')
    .pipe(handle(app, 'handlerName')) //<= handle middleware
    .pipe(app.dest('foo'))
});
```

**Example**

```js
const assemble = require('assemble');
const handle = require('assemble-handle');
const app = assemble();

/**
 * create some middleware "stages"
 */

app.handler('onStream');
app.handler('preWrite');
app.handler('postWrite');

/**
 * Create middleware
 */

app.onStream(/\.(js|css)$/, function(file, next) {
  // lint javascript
  next();
});

app.preWrite(/\.(jpg|png)$/, function(file, next) {
  // minify images
  next();
});

app.postWrite(/./, function(file, next) {
  // create files tree or something
  next();
});

/**
 * Run (handle) the middleware 
 */

app.task('site', function() {
  return app.src('assets/**/*.*')
    .pipe(handle(app, 'onStream'))  // handle onStream
    .pipe(handle(app, 'preWrite'))  // handle preWrite
    .pipe(app.dest('site/'));
    .pipe(handle(app, 'postWrite')) // handle postWrite
});
```

### handle.once

A `.once` method is exposed, which has the same exact behavior as the main function, but will ensure that middleware is only handled once for a given "stage".

**Example**

For example the given middleware will only run once.

```js
const assemble = require('assemble-core');
const handle = require('assemble-handle');
const app = assemble();

app.handler('onFoo');

app.onFoo(/./, function(file, next) {
  file.count = file.count || 0;
  file.count++;
  next();
});

app.task('handle-once', function(cb) {
  let files = [];
  app.src('test/**/*.*')
    .pipe(handle.once(app, 'onFoo'))
    .pipe(handle.once(app, 'onFoo'))
    .pipe(handle.once(app, 'onFoo'))
    .pipe(handle.once(app, 'onFoo'))
    .pipe(handle.once(app, 'onFoo'))
    .on('data', function(file) {
      files.push(file);
    })
    .pipe(app.dest('test/actual'))
    .on('end', function() {
      console.log(files[0].count);
      //=> 1
      cb();
    });
});

app.task('handle', function(cb) {
  let files = [];
  app.src('test/**/*.*')
    .pipe(handle(app, 'onFoo'))
    .pipe(handle(app, 'onFoo'))
    .pipe(handle(app, 'onFoo'))
    .pipe(handle(app, 'onFoo'))
    .pipe(handle(app, 'onFoo'))
    .on('data', function(file) {
      files.push(file);
    })
    .pipe(app.dest('test/actual'))
    .on('end', function() {
      console.log(files[0].count);
      //=> 5
      cb();
    });
});
```

## Custom handlers

Create custom middleware handlers.

```js
app.handler('onFoo');
```

This adds an `.onFoo` method to the instance:

```js
app.onFoo(/\.hbs$/, function(file, next) {
  // do stuff to file
  next();
});
```

All `.onFoo` middleware will now run when the `onFoo` handler is called:

```js
app.task('default', function() {
  return app.src('*.hbs')

    // call the `onFoo` handler
    .pipe(handle(app, 'onFoo')) 
});
```

## About

<details>
<summary><strong>Contributing</strong></summary>

Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).

</details>

<details>
<summary><strong>Running Tests</strong></summary>

Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:

```sh
$ npm install && npm test
```

</details>

<details>
<summary><strong>Building docs</strong></summary>

_(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_

To generate the readme, run the following command:

```sh
$ npm install -g verbose/verb#dev verb-generate-readme && verb
```

</details>

### Related projects

You might also be interested in these projects:

* [assemble-core](https://www.npmjs.com/package/assemble-core): The core assemble application with no presets or defaults. All configuration is left to the… [more](https://github.com/assemble/assemble-core) | [homepage](https://github.com/assemble/assemble-core "The core assemble application with no presets or defaults. All configuration is left to the implementor.")
* [assemble-fs](https://www.npmjs.com/package/assemble-fs): Light wrapper for vinyl-fs to add streams support in a way that plays nice with… [more](https://github.com/assemble/assemble-fs) | [homepage](https://github.com/assemble/assemble-fs "Light wrapper for vinyl-fs to add streams support in a way that plays nice with Assemble middleware.")
* [assemble-streams](https://www.npmjs.com/package/assemble-streams): Assemble pipeline plugin for pushing views into a vinyl stream. | [homepage](https://github.com/assemble/assemble-streams "Assemble pipeline plugin for pushing views into a vinyl stream.")
* [assemble](https://www.npmjs.com/package/assemble): Get the rocks out of your socks! Assemble makes you fast at creating web projects… [more](https://github.com/assemble/assemble) | [homepage](https://github.com/assemble/assemble "Get the rocks out of your socks! Assemble makes you fast at creating web projects. Assemble is used by thousands of projects for rapid prototyping, creating themes, scaffolds, boilerplates, e-books, UI components, API documentation, blogs, building websit")

### Contributors

| **Commits** | **Contributor** |  
| --- | --- |  
| 17 | [jonschlinkert](https://github.com/jonschlinkert) |  
| 3  | [doowb](https://github.com/doowb) |  

### Author

**Jon Schlinkert**

* [GitHub Profile](https://github.com/jonschlinkert)
* [Twitter Profile](https://twitter.com/jonschlinkert)
* [LinkedIn Profile](https://linkedin.com/in/jonschlinkert)

### License

Copyright © 2018, [Jon Schlinkert](https://github.com/jonschlinkert).
Released under the [MIT License](LICENSE).

***

_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.8.0, on December 11, 2018._

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