Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GX-17505] Dismantle core-plugin #23

Merged
merged 7 commits into from
Jul 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/gasket-express-plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
# CHANGELOG

###

- [#23] Dismantling core-plugin

### 1.0.0

- Renaming express-create to expressCreate
- Separate express plugins from core
- Initial release.

[#23]: https://github.com/godaddy/gasket/pull/23
35 changes: 28 additions & 7 deletions packages/gasket-express-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ The `express-plugin` adds `express` to your application.

## Installation

```
```sh
npm install --save @gasket/express-plugin
```

Expand Down Expand Up @@ -45,7 +45,7 @@ module.exports = {

`express` hooks into the following lifecycle in order to work.

#### expressCreate
#### createServers

```js
module.exports = {
Expand All @@ -54,10 +54,11 @@ module.exports = {
/**
* Creates the express app
* @param {Gasket} gasket The Gasket API
* @param {Object} serverOpts Server options.
* @return {Promise<Object>} express app
*/
'expressCreate': async function createExpress(gasket, devServer) {
return express();
'createServers': async function createServers(gasket, serverOpts) {
return { ...serverOpts, handler: express() };
}
}
};
Expand All @@ -75,7 +76,7 @@ module.exports = {
hooks: {
/**
* Add Express middleware
*
*
* @param {Gasket} gasket The Gasket API
* @param {Express} app - Express app instance
* @returns {function|function[]} middleware(s)
Expand All @@ -99,13 +100,33 @@ module.exports = {
hooks: {
/**
* Update Express app instance
*
*
* @param {Gasket} gasket The Gasket API
* @param {Express} express - Express app instance
* @param {Express} express Express app instance
* @returns {function|function[]} middleware(s)
*/
express: async function (gasket, express) {
}
}
}
```

#### errorMiddleware

Executed after the `express` event. All middleware functions returned from
this hook will be applied to express.

```js
module.exports = {
hooks: {
/**
* Add Express error middlewares
*
* @param {Gasket} gasket The Gasket API
* @returns {function|function[]} error middleware(s)
*/
errorMiddleware: function (gasket) {
}
}
}
```
27 changes: 24 additions & 3 deletions packages/gasket-express-plugin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,29 @@ const compression = require('compression');
module.exports = {
name: 'express',
hooks: {
/**
* Add files & extend package.json for new apps.
*
* @param {Gasket} gasket - The gasket API.
* @param {CreateContext} context - Create context
* @param {PackageJson} context.pkg - The Gasket PackageJson API.
* @public
*/
create: async function create(gasket, context) {
context.pkg.add('dependencies', {
express: '^4.16.3'
});
},
/**
* Create the Express instance and setup the lifecycle hooks.
*
* @param {Gasket} gasket Gasket API.
* @param {Object} config application config.
* @param {Object} serverOpts Server options.
* @returns {Express} The web server.
* @public
*/
expressCreate: async function createExpress(gasket) {
// eslint-disable-next-line max-statements
createServers: async function createServers(gasket, serverOpts) {
const { config } = gasket;
const excludedRoutesRegex = config.express && config.express.excludedRoutesRegex;
const app = express();
Expand Down Expand Up @@ -42,7 +56,14 @@ module.exports = {
});

await gasket.exec('express', app);
return app;

const postRenderingStacks = await gasket.exec('errorMiddleware');
postRenderingStacks.forEach(stack => app.use(stack));

return {
...serverOpts,
handler: app
};
}
}
};
74 changes: 65 additions & 9 deletions packages/gasket-express-plugin/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ const proxyquire = require('proxyquire');
const { spy, stub } = require('sinon');
const assume = require('assume');

describe('createExpress', () => {
// eslint-disable-next-line max-statements
describe('createServers', () => {
let plugin;
let express, app;
let cookieParser, cookieParserMiddleware;
Expand Down Expand Up @@ -31,24 +32,52 @@ describe('createExpress', () => {
});
});

it('returns the handler app', async function () {
const result = await plugin.hooks.createServers(gasket, {});
assume(result).deep.equals({ handler: app });
});

it('executes the `middleware` lifecycle', async function () {
await plugin.hooks.expressCreate(gasket);
await plugin.hooks.createServers(gasket, {});
assume(gasket.exec).has.been.calledWith('middleware', app);
});

it('executes the `express` lifecycle', async function () {
await plugin.hooks.expressCreate(gasket);
await plugin.hooks.createServers(gasket, {});
assume(gasket.exec).has.been.calledWith('express', app);
});

it('executes the `errorMiddleware` lifecycle', async function () {
await plugin.hooks.createServers(gasket, {});
assume(gasket.exec).has.been.calledWith('errorMiddleware');
});

it('executes the `middleware` lifecycle before the `express` lifecycle', async function () {
await plugin.hooks.expressCreate(gasket);
await plugin.hooks.createServers(gasket, {});
assume(gasket.exec.firstCall).has.been.calledWith('middleware', app);
assume(gasket.exec.secondCall).has.been.calledWith('express', app);
});

it('executes the `errorMiddleware` lifecycle after the `express` lifecycle', async function () {
await plugin.hooks.createServers(gasket, {});
assume(gasket.exec.secondCall).has.been.calledWith('express', app);
assume(gasket.exec.thirdCall).has.been.calledWith('errorMiddleware');
});

it('adds the errorMiddleware', async () => {
const errorMiddlewares = [spy()];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are these spies reset and cleaned, probably want to use sinon as sandbox here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comes from const { spy, stub } = require('sinon');.
I am not creating global mocks or anything, do they need to be reset and clean?

gasket.exec.withArgs('errorMiddleware').resolves(errorMiddlewares);

await plugin.hooks.createServers(gasket, {});

const errorMiddleware = findCall(
app.use,
(mw) => mw === errorMiddlewares[0]);
assume(errorMiddleware).to.not.be.null();
});

it('adds the cookie-parser middleware before plugin middleware', async () => {
await plugin.hooks.expressCreate(gasket);
await plugin.hooks.createServers(gasket, {});

const cookieParserUsage = findCall(
app.use,
Expand All @@ -66,7 +95,7 @@ describe('createExpress', () => {

it('adds the cookie-parser middleware with a excluded path', async () => {
gasket.config.express = { excludedRoutesRegex: /^(?!\/_next\/)/ };
await plugin.hooks.expressCreate(gasket);
await plugin.hooks.createServers(gasket, {});

const cookieParserUsage = findCall(
app.use,
Expand All @@ -75,7 +104,7 @@ describe('createExpress', () => {
});

it('adds the compression middleware by default', async () => {
await plugin.hooks.expressCreate(gasket);
await plugin.hooks.createServers(gasket, {});

const compressionUsage = findCall(
app.use,
Expand All @@ -85,7 +114,7 @@ describe('createExpress', () => {

it('adds the compression middleware when enabled from gasket config', async () => {
gasket.config.express = { compression: true };
await plugin.hooks.expressCreate(gasket);
await plugin.hooks.createServers(gasket, {});

const compressionUsage = findCall(
app.use,
Expand All @@ -95,7 +124,7 @@ describe('createExpress', () => {

it('does not add the compression middleware when disabled from gasket config', async () => {
gasket.config.express = { compression: false };
await plugin.hooks.expressCreate(gasket);
await plugin.hooks.createServers(gasket, {});

const compressionUsage = findCall(
app.use,
Expand All @@ -108,3 +137,30 @@ describe('createExpress', () => {
return callIdx === -1 ? null : aSpy.getCall(callIdx);
}
});

describe('create', () => {

let plugin, mockContext;

function assumeCreatedWith(assertFn) {
return async function assumeCreated() {
await plugin.hooks.create({}, mockContext);
assertFn(mockContext);
};
}

beforeEach(() => {
plugin = require('./');

mockContext = {
pkg: { add: spy() },
files: { add: spy() }
};
});

it('adds appropriate dependencies', assumeCreatedWith(({ pkg }) => {
assume(pkg.add).calledWith('dependencies', {
express: '^4.16.3'
});
}));
});
4 changes: 4 additions & 0 deletions packages/gasket-https-plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# `@gasket/https-plugin`

###

- [#23] Dismantling core-plugin
- [#7]: Initialize the `https-plugin`

[#7]: https://github.com/godaddy/gasket/pull/7
[#23]: https://github.com/godaddy/gasket/pull/23
27 changes: 25 additions & 2 deletions packages/gasket-https-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ configuration. This plugin is part of the [`@gasket/default-preset`].
- [Installation](#installation)
- [Configuration](#configuration)
- [Lifecycles](#lifecycles)
- [createServers](#createservers)
- [servers](#servers)
- [Configuration](#configuration-1)
- [LICENSE: MIT](#license-mit)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good improvement in itself, but most of our warehouse modules use a license badge rather than adding section at the bottom. I think we want to be consistent, but not feeling strong about it at this point


## Installation

Expand All @@ -31,12 +35,31 @@ Add it to the `plugins` section of your `gasket.config.js`:

## Lifecycles

#### createServers

Executed in order to retrieve the server options and the handler

```js
/**
* In this example returns the express app
*
* @param {Gasket} gasket Gasket API.
* @param {Object} serverOpts Server options.
* @returns {Express} The web server.
* @public
*/
createServers: async function createServers(gasket, serverOpts) {
const newServerOpts;
return { ...serverOpts, ...newServerOpts, handler: express() };
}
```

#### servers

Your application can use this plugin to hook the `servers` hook. These servers
are consumed directly from [`create-servers`].

```js
// lifecyles/servers.js

/**
*
* @param {Gasket} gasket The Gasket API
Expand Down
Loading