Skip to content

Commit

Permalink
Merge branch 'release/1.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanfoster committed Oct 19, 2017
2 parents a2972d4 + fb6c036 commit 59efa03
Show file tree
Hide file tree
Showing 20 changed files with 662 additions and 328 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
/node_modules
/npm-debug.log
docs
.nyc_output
10 changes: 4 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@ sudo: false
language: node_js
node_js:
- '6'
- '5'
- '4'
- '8'
- node
os:
- osx
- linux
cache:
yarn: true
directories:
- $HOME/.npm
before_install:
- npm config set spin false
script: npm run test:cover
- yarn config set spin false
script: yarn cover
after_script: cat coverage/lcov.info | node_modules/coveralls/bin/coveralls.js
after_success: bash scripts/deploy
deploy:
Expand Down
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
<a name="1.2.0"></a>
# [1.2.0](https://github.com/dylanfoster/parch/compare/1.1.1...1.2.0) (2017-10-19)


### Features

* **application:** add support for project initializers ([dfbacab](https://github.com/dylanfoster/parch/commit/dfbacab))
* **loader:** remove type suffix from module name ([3e8c2f3](https://github.com/dylanfoster/parch/commit/3e8c2f3))



<a name="1.1.1"></a>
## [1.1.1](https://github.com/dylanfoster/parch/compare/1.1.0...1.1.1) (2017-04-09)

Expand Down
70 changes: 37 additions & 33 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
{
"name": "parch",
"version": "1.1.1",
"version": "1.2.0",
"description": "Restify + Sequelize",
"main": "index.js",
"repository": "dylanfoster/parch",
"author": "Dylan Foster",
"license": "MIT",
"keywords": [
"restify",
"sequelize",
"restful"
],
"scripts": {
"build": "babel src -d lib",
"build:watch": "chokidar 'src/**' 'test/**/*.js' -c 'npm run build' --initial",
"coverage": "NO_DEPRECATION=parch NODE_ENV=test istanbul cover ./node_modules/mocha/bin/_mocha -- --compilers js:babel-register --recursive --timeout 40000",
"changelog": "conventional-changelog -s -p angular -i CHANGELOG.md",
"cover": "nyc yarn test",
"docs": "yuidoc . -o docs",
"lint": "eslint ./src ./test",
"prepare-release": "curl -sL https://gist.githubusercontent.com/dylanfoster/35b06db9aaa9237da77eff4a8eec0a22/raw/83ec714ec64f7ec36c5004588aa259354fc87b51/prepare-release.sh | bash -s",
"prepublish": "npm run build",
"test": "NO_DEPRECATION=parch NODE_ENV=test mocha --recursive --compilers js:babel-register --timeout 5000",
"test:cover": "npm run coverage",
"watch": "chokidar 'src/**' 'test/**/*.js' -c 'npm test' --initial"
"prepublish": "yarn build",
"test": "NO_DEPRECATION=parch NODE_ENV=test mocha --recursive --compilers js:babel-register --timeout 6000",
"watch:test": "chokidar 'src/**' 'test/**/*.js' -c 'yarn test' --initial",
"watch:build": "chokidar 'src/**' 'test/**/*.js' -c 'yarn build' --initial",
"watch:cover": "chokidar 'src/**' 'test/**/*.js' -c 'yarn cover' --initial"
},
"keywords": [
"restify",
"sequelize",
"restful"
],
"author": "Dylan Foster",
"license": "MIT",
"repository": "dylanfoster/parch",
"devDependencies": {
"babel-cli": "^6.10.1",
"babel-plugin-add-module-exports": "^0.2.1",
Expand All @@ -36,20 +36,32 @@
"del": "^2.2.1",
"eslint": "^3.11.1",
"eslint-config-unstandard": "^1.4.0",
"istanbul": "^1.0.0-alpha.2",
"jsdoc": "^3.4.0",
"jsonwebtoken": "^7.0.1",
"minimatch": "^3.0.3",
"mocha": "^3.2.0",
"nyc": "^11.2.1",
"sinon": "^2.1.0",
"sinon-chai": "^2.8.0",
"sqlite3": "^3.1.4",
"stream": "0.0.2",
"supertest": "^3.0.0",
"yuidoc-lucid-theme": "^0.1.2",
"yuidoc-parch-theme": "^1.0.0",
"yuidocjs": "^0.10.2"
},
"dependencies": {
"@parch-js/orm": "^0.0.2",
"bunyan": "^1.8.1",
"callsite": "^1.0.0",
"depd": "^1.1.0",
"include-all": "^4.0.3",
"inflect": "^0.3.0",
"lodash": "^4.13.1",
"pg": "^6.1.2",
"pg-hstore": "^2.3.2",
"restify": "^4.1.0",
"restify-errors": "^4.2.3",
"restify-jwt": "^0.4.0",
"sequelize": "^3.23.3"
},
"eslintConfig": {
"extends": "unstandard",
"parserOptions": {
Expand All @@ -76,19 +88,11 @@
]
}
},
"dependencies": {
"@parch-js/orm": "^0.0.2",
"bunyan": "^1.8.1",
"callsite": "^1.0.0",
"depd": "^1.1.0",
"include-all": "^4.0.3",
"inflect": "^0.3.0",
"lodash": "^4.13.1",
"pg": "^6.1.2",
"pg-hstore": "^2.3.2",
"restify": "^4.1.0",
"restify-errors": "^4.2.3",
"restify-jwt": "^0.4.0",
"sequelize": "^3.23.3"
"nyc": {
"reporter": [
"text",
"lcov",
"html"
]
}
}
2 changes: 1 addition & 1 deletion scripts/deploy
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

set -e

npm run docs
yarn docs

if [ "$TRAVIS_BRANCH" == "master" ]; then
pushd docs &> /dev/null
Expand Down
43 changes: 32 additions & 11 deletions src/application.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
"use strict";

import path from "path";
import {
dirname,
resolve
} from "path";

import callsite from "callsite";
import includeAll from "include-all";
Expand Down Expand Up @@ -29,15 +32,13 @@ const DEFAULT_LISTEN_PORT = 3000;
*/
class Application {
/* eslint-disable complexity */
constructor(options = {}) {
constructor(options) {
const projectDirectory = this._getProjectDirectory();
const registry = this.registry = new Registry();

this.DEFAULT_CONTROLLER_LOOKUP_PATH = path.resolve(
projectDirectory,
"controllers"
);
this.DEFAULT_MODEL_LOOKUP_PATH = path.resolve(projectDirectory, "models");
this.DEFAULT_CONTROLLER_LOOKUP_PATH = resolve(projectDirectory, "controllers");
this.DEFAULT_INITIALIZERS_LOOKUP_PATH = resolve(projectDirectory, "initializers");
this.DEFAULT_MODEL_LOOKUP_PATH = resolve(projectDirectory, "models");
options = this._configure(options);

registry.register("config:main", options);
Expand Down Expand Up @@ -65,6 +66,26 @@ class Application {
return this.registry.lookup("service:server");
}

listen(port) {
return new Promise(done => {
this.app.listen(port, () => done());
});
}

runProjectInitializers() {
const config = this.registry.lookup("config:main");
const initializersPath = config.initializers.dir;
const initializerModules = includeAll({
dirname: initializersPath,
filter: /(.+).js$/
});
const initializers = Object.keys(initializerModules);

return Promise.all(initializers.map(name =>
initializerModules[name].initialize(this, this.registry)
));
}

/**
* starts listening on the defined port
*
Expand All @@ -73,9 +94,7 @@ class Application {
* @return {Promise<undefined, Error>}
*/
start(port = DEFAULT_LISTEN_PORT) {
return new Promise((resolve, reject) => {
this.app.listen(port, () => { resolve(); });
});
return this.runProjectInitializers().then(() => this.listen(port));
}

/**
Expand All @@ -93,6 +112,8 @@ class Application {
config.database.connection = config.database.connection || DEFAULT_CONNECTION_SETTINGS;
config.database.models = config.database.models || {};
config.database.models.dir = config.database.models.dir || this.DEFAULT_MODEL_LOOKUP_PATH;
config.initializers = config.initializers || {};
config.initializers.dir = config.initializers.dir || this.DEFAULT_INITIALIZERS_LOOKUP_PATH;
config.logging = config.logging || {};
config.server = config.server || {};
config.server.middlewares = config.server.middlewares || [];
Expand All @@ -109,7 +130,7 @@ class Application {
*/
_getProjectDirectory() {
const caller = callsite()[2].getFileName();
const projectDirectory = path.dirname(caller);
const projectDirectory = dirname(caller);

return projectDirectory;
}
Expand Down
30 changes: 9 additions & 21 deletions src/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,8 @@ class Loader {
return this.modules[name];
}

_getFilter(filter) {
filter = filter || this._getDefaultFilterString();

return new RegExp(filter, "i");
}

_getDefaultFilterString() {
return `(.+)${this.type}\\.js$`;
}

_getDefaultFilter() {
return this._getFilter();
return /(.+).js$/i;
}

_loadModules() {
Expand All @@ -36,20 +26,18 @@ class Loader {
});

Object.keys(modules).forEach(module => {
const moduleName = module.toLowerCase().replace("_", "").replace("-", "");
const moduleName = module.toLowerCase()
.replace("_", "")
.replace("-", "")
.replace(this.type, "");

const key = inflect.singularize(moduleName);

if (modules[module].hasOwnProperty("default")) {
/**
* HACK: Babel no longer exports [default], though I'm pretty sure I have
* that transform installed so....¯\_(ツ)_/¯
*/
modules[key] = modules[module].default;
} else {
if (!modules[key]) {
modules[key] = modules[module];
}

delete modules[module];
delete modules[module];
}
});

return modules;
Expand Down
11 changes: 3 additions & 8 deletions src/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,11 @@ class Logger {
name = name || "api";

const serializers = options.serializers || {};

if (!serializers.req) {
serializers.req = this.req;
}
if (!serializers.res) {
serializers.res = this.res;
}

const streams = [];

serializers.req = serializers.req || this.req;
serializers.res = serializers.res || this.res;

if (options.dir) {
streams.push({
path: path.join(options.dir, `${LOG_ENV}.log`)
Expand Down
2 changes: 1 addition & 1 deletion src/middleware/logger.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";

export default function (options = {}) {
export default function (options) {
return function logger(req, res, next) {
const log = options.log;

Expand Down
23 changes: 13 additions & 10 deletions src/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,23 @@ export default class Registry {

assert(hasBeenInjected, `Attempted to inject unknown object '${lookup}'`);

const obj = this.lookup(lookup);
const [prop] = lookup.split(":");
const property = propertyName || prop;
let obj;

if (Object.prototype.hasOwnProperty.call(context, property)) { return; }

if (obj) {
try {
obj = this.lookup(lookup);
assert(obj, `Attempted to inject unknown object '${lookup}'`);
Object.defineProperty(context, property, {
enumerable: false,
configurable: false,
get() { return obj; },
set() {}
});
} catch (err) {
throw err;
}

return context;
Expand All @@ -78,9 +82,12 @@ export default class Registry {

if (obj) { return obj.instance; }

obj = this._loadModule(moduleLookup, moduleName);

assert(obj, `Attempted to lookup unknown module '${moduleName}'`);
try {
obj = this._loadModule(moduleLookup, moduleName);
assert(obj, `Attempted to lookup unknown ${moduleLookup} '${moduleName}'`);
} catch (err) {
throw err;
}

this.register(name, obj);

Expand Down Expand Up @@ -159,11 +166,7 @@ export default class Registry {
try {
modules = includeAll({ dirname: this._getLookupDirectory(lookup) });
} catch (err) {
if (err.message.match(/dirname/i)) {
throw new Error(`Attempted to lookup unknown module '${name}'`);
}

throw err;
throw new Error(`Attempted to lookup unknown ${lookup} '${name}'`);
}

// TODO: this will need to be configurable somehow (or better regex)
Expand Down

0 comments on commit 59efa03

Please sign in to comment.