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

Adding @gasket/lifecycle-plugin #30

Merged
merged 1 commit into from
Jul 30, 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
4 changes: 4 additions & 0 deletions packages/gasket-lifecycle-plugin/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"root": true,
"extends": "godaddy"
}
1 change: 1 addition & 0 deletions packages/gasket-lifecycle-plugin/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock.json binary
62 changes: 62 additions & 0 deletions packages/gasket-lifecycle-plugin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Logs (npm-debug.log, yarn-error.log, etc)
logs
*.log

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# OSX Finder cache files
.DS_Store

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

# Editor files
.idea
*.iml
69 changes: 69 additions & 0 deletions packages/gasket-lifecycle-plugin/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Logs (npm-debug.log, yarn-error.log, etc)
logs
*.log

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# OSX Finder cache files
.DS_Store

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

# Editor files
.idea
*.iml

### ▲ .gitignore contents above
### ▼ additional ignore files for publishing

test/*
.eslintrc
.gitattributes
21 changes: 21 additions & 0 deletions packages/gasket-lifecycle-plugin/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2019 GoDaddy Operating Company, LLC.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
70 changes: 70 additions & 0 deletions packages/gasket-lifecycle-plugin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# @gasket/lifecycle-plugin

Subscribe to gasket lifecycle events using files in a `lifecycles` directory.

## Installation

```
npm install --save @gasket/lifecycle-plugin
```

## Usage

Add the plugin to the plugin section of your `gasket.config.js` file:

```
{
plugins: {
add: ['lifecycle']
}
}
```

Next, create a `lifecycles` folder in the root of your application. This folder
should contain files that can interact with the various of gasket lifecycle
events that are implemented by the plugins.

The name of the file (excluding the `.js` extension) is used as the name of
the lifecycle event and the exported function of that file is used as handler
of the event. So you end up with the following application structure:

```
gasket.config.js
pages/
lifecycles/
express.js
middleware.js
webpack.js
```

In the example above we register to 3 different lifecycle hooks:

- `express`
- `middleware`
- `webpack`

Each of these files export a function that is called when the lifecycle is
executed. For example, for `middleware` the file could look like:

```js
const cors = require('access-control');

/**
* Introduce new middleware layers to the stack.
*
* @param {Gasket} gasket Reference to the gasket instance
*/
module.exports = function middleware(gasket) {
return cors({
maxAge: '1 hour',
credentials: true,
origins: 'http://example.com'
});
};
```

It is recommended that you use `kebab-case` or `snake_case` naming conventions
for multi-word lifecycle events to avoid problems with case sensitivity in
different file systems. This plugin will automatically map these to the
`camelCased` event names. For example, `/lifecycles/app-env-config.js` will
properly hook `appEnvConfig` events.
76 changes: 76 additions & 0 deletions packages/gasket-lifecycle-plugin/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
const debug = require('diagnostics')('gasket:lifecycle');
const path = require('path');
const fs = require('fs');
const { promisify } = require('util');
const { camelCase } = require('lodash');

const readDir = promisify(fs.readdir);

/**
* Resolves a given directory to valid `lifecycle` plugins.
*
* @param {String} root Directory we need to search.
* @param {String} name Name of the directory that contains the lifecycles.
* @returns {Array} Lifecycle methods.
* @public
*/
async function resolve(root, name) {
const dir = path.join(root, name);

let files = [];
try {
files = await readDir(dir);
} catch (err) {
if (err.code !== 'ENOENT') {
throw err;
}
}

return files.filter(function filter(file) {
return path.extname(file) === '.js';
}).map(function each(file) {
const extname = path.extname(file);
const event = camelCase(path.basename(file, extname));

let hook = require(path.join(dir, file));
debug('found %s as lifecycle for %s', file, event);

if (typeof hook === 'function') {
hook = {
handler: hook
};
}

return {
pluginName: file,
event,
...hook
};
});
}

/**
* Register all hooks as lifecycle that are in a given directory.
*
* @param {Gasket} gasket Gasket/Plugin-Engine instance.
* @public
*/
async function init(gasket) {
const lifecycles = await resolve(gasket.config.root, 'lifecycles');

lifecycles.forEach(function each(cycle) {
gasket.hook(cycle);
});
}

/**
* Expose the plugin.
*
* @public
*/
module.exports = {
name: 'lifecycle',
hooks: {
init
}
};
41 changes: 41 additions & 0 deletions packages/gasket-lifecycle-plugin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"name": "@gasket/lifecycle-plugin",
"version": "1.0.0",
"description": "Allows a gasket/ directory to be used for lifecycle hooks in applications.",
"main": "lib/index.js",
"scripts": {
"lint": "eslint --fix *.js test/*.js",
"mocha": "mocha test/lifecycle.js",
"test": "nyc --reporter=text --reporter=json-summary npm run mocha",
"posttest": "npm run lint"
},
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/godaddy/gasket"
},
"keywords": [
"gasket",
"plugin",
"lifecycle"
],
"author": "GoDaddy Operating Company, LLC",
"license": "MIT",
"bugs": {
"url": "https://github.com/godaddy/gasket/issues"
},
"homepage": "https://github.com/godaddy/gasket/tree/master/packages/gasket-lifecycle-plugin",
"dependencies": {
"diagnostics": "^1.1.0",
"lodash": ">=3.0.0 < 5.0.0"
},
"devDependencies": {
"@gasket/plugin-engine": "^1.0.0",
"assume": "^2.0.1",
"eslint": "^4.19.1",
"eslint-config-godaddy": "^2.1.0",
"eslint-plugin-json": "^1.2.0",
"eslint-plugin-mocha": "^4.12.1",
"mocha": "^5.2.0",
"nyc": "^12.0.2"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const proxy = require('../../../proxy');

module.exports = (...args) => {
proxy.emit('someEvent', ...args);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const proxy = require('../../../proxy');

module.exports = async function middleware() {
proxy.emit('middleware', ...arguments);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const proxy = require('../../../proxy');

module.exports = function middleware() {
proxy.emit('middleware', ...arguments);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const proxy = require('../../../proxy');

module.exports = {
timing: {
after: ['foo', 'bar']
},
handler() {
proxy.emit('withTiming', ...arguments);
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Here to make sure this directory exists.
Loading