Skip to content

Commit

Permalink
fix(application): add guard for missing initializer directory
Browse files Browse the repository at this point in the history
commit 68b76e7
Author: Dylan Foster <dylan947@gmail.com>
Date:   Sat Nov 11 18:21:45 2017 -0800

    fix(application): add guard for missing initializer directory
  • Loading branch information
dylanfoster committed Nov 12, 2017
1 parent bd024a7 commit 0538c76
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 10 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"babel-preset-env": "^1.6.1",
"babel-register": "^6.9.0",
"chai": "^4.1.2",
"chai-as-promised": "^7.1.1",
"chokidar-cli": "^1.2.0",
"conventional-changelog-cli": "^1.2.0",
"coveralls": "^3.0.0",
Expand Down
29 changes: 20 additions & 9 deletions src/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,26 @@ class Application {
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)
));
let initializerModules;

/* eslint-disable no-empty */
try {
initializerModules = includeAll({
dirname: initializersPath,
filter: /(.+).js$/
});
} catch (err) {}
/* eslint-enable no-empty */

if (initializerModules) {
const initializers = Object.keys(initializerModules);

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

/**
Expand Down
22 changes: 22 additions & 0 deletions test/application_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import fs from "fs";
import path from "path";

import chai, { expect } from "chai";
import chaiAsPromised from "chai-as-promised";
import del from "del";
import jwt from "jsonwebtoken";
import restify from "restify";
Expand All @@ -14,6 +15,7 @@ import sinonChai from "sinon-chai";
import stream from "stream";
import supertest from "supertest";

chai.use(chaiAsPromised);
chai.use(sinonChai);

import Application from "../src/application";
Expand Down Expand Up @@ -47,6 +49,26 @@ describe("Application", function () {
expect(application.foo.foo).to.eql("bar");
});
});

it("doesn't throw for a missing initializers directory", function () {
application = new Application({
controllers: {
dir: path.resolve(__dirname, "fixtures", "controllers")
},
database: {
connection,
models: { dir: path.resolve(__dirname, "fixtures/models") }
},
initializers: {
dir: path.resolve(__dirname, "fixtures", "missing-initializers")
},
serializers: {
dir: path.resolve(__dirname, "fixtures", "serializers")
}
});

return expect(application.runProjectInitializers()).to.be.fulfilled;
});
});

describe("#map", function () {
Expand Down
8 changes: 7 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1038,6 +1038,12 @@ center-align@^0.1.1:
align-text "^0.1.3"
lazy-cache "^1.0.3"

chai-as-promised@^7.1.1:
version "7.1.1"
resolved "https://registry.yarnpkg.com/chai-as-promised/-/chai-as-promised-7.1.1.tgz#08645d825deb8696ee61725dbf590c012eb00ca0"
dependencies:
check-error "^1.0.2"

chai@^4.1.2:
version "4.1.2"
resolved "https://registry.yarnpkg.com/chai/-/chai-4.1.2.tgz#0f64584ba642f0f2ace2806279f4f06ca23ad73c"
Expand Down Expand Up @@ -1067,7 +1073,7 @@ chalk@^2.0.0, chalk@^2.1.0:
escape-string-regexp "^1.0.5"
supports-color "^4.0.0"

check-error@^1.0.1:
check-error@^1.0.1, check-error@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/check-error/-/check-error-1.0.2.tgz#574d312edd88bb5dd8912e9286dd6c0aed4aac82"

Expand Down

0 comments on commit 0538c76

Please sign in to comment.