forked from yargs/yargs
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for global middleware, useful for shared tasks like…
… metrics (yargs#1119)
- Loading branch information
Showing
6 changed files
with
104 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module.exports = function (globalMiddleware, context) { | ||
return function (callback) { | ||
if (Array.isArray(callback)) { | ||
Array.prototype.push.apply(globalMiddleware, callback) | ||
} else if (typeof callback === 'object') { | ||
globalMiddleware.push(callback) | ||
} | ||
return context | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
'use strict' | ||
/* global describe, it, beforeEach, afterEach */ | ||
|
||
const middlewareFactory = require('../lib/middleware') | ||
let yargs | ||
require('chai').should() | ||
|
||
describe('middleware', () => { | ||
beforeEach(() => { | ||
yargs = require('../') | ||
}) | ||
|
||
afterEach(() => { | ||
delete require.cache[require.resolve('../')] | ||
}) | ||
it('should add a list of callbacks to global middleware', () => { | ||
const globalMiddleware = [] | ||
|
||
middlewareFactory(globalMiddleware)(['callback1', 'callback2']) | ||
|
||
globalMiddleware.should.have.lengthOf(2) | ||
}) | ||
|
||
it('should add a single callback to global middleware', () => { | ||
const globalMiddleware = [] | ||
|
||
middlewareFactory(globalMiddleware)({}) | ||
|
||
globalMiddleware.should.have.lengthOf(1) | ||
}) | ||
|
||
it('runs all middleware before reaching the handler', function (done) { | ||
yargs(['mw']) | ||
.middleware([ | ||
function (argv) { | ||
argv.mw1 = 'mw1' | ||
}, | ||
function (argv) { | ||
argv.mw2 = 'mw2' | ||
} | ||
]) | ||
.command( | ||
'mw', | ||
'adds func list to middleware', | ||
function () {}, | ||
function (argv) { | ||
// we should get the argv filled with data from the middleware | ||
argv.mw1.should.equal('mw1') | ||
argv.mw2.should.equal('mw2') | ||
return done() | ||
} | ||
) | ||
.exitProcess(false) // defaults to true. | ||
.argv | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters