Skip to content
This repository has been archived by the owner on Jan 24, 2022. It is now read-only.

Commit

Permalink
Add methods addFactoriesFromDir and addRoutesFromDir to server
Browse files Browse the repository at this point in the history
  • Loading branch information
onechiporenko committed Mar 26, 2018
1 parent 3563bc6 commit ee6039e
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Factory, Lair} from 'lair-db/dist';
import {Factory, Lair} from 'lair-db';
import Cron from './cron';
import Job from './job';
import Route from './route';
Expand Down
2 changes: 1 addition & 1 deletion lib/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as express from 'express';
import {Lair} from 'lair-db/dist';
import {Lair} from 'lair-db';
import {CRUDOptions} from 'lair-db/dist/lair';
import methods = require('methods');
import {assert} from './utils';
Expand Down
27 changes: 25 additions & 2 deletions lib/server.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import bodyParser = require('body-parser');
import colors = require('colors/safe');
import * as express from 'express';
import read = require('fs-readdir-recursive');
import * as http from 'http';
import {Factory, Lair} from 'lair-db/dist';
import {Factory, Lair} from 'lair-db';
import winston = require('winston');
import {printRoutesMap} from './express';
import Route from './route';
Expand Down Expand Up @@ -84,14 +85,22 @@ export default class Server {
routes.map(route => this.addRoute(route));
}

public addFactory(factory: Factory, name: string) {
public addRoutesFromDir(path: string) {
read(path).forEach(routePath => this.add('route', path, routePath));
}

public addFactory(factory: Factory, name?: string) {
this.lair.registerFactory(factory, name);
}

public addFactories(factories: Array<[Factory, string]>) {
factories.map(args => this.addFactory.apply(this, args));
}

public addFactoriesFromDir(path: string) {
read(path).forEach(factoryPath => this.add('factory', path, factoryPath));
}

public createRecords(factoryName: string, count: number) {
this.createRecordsQueue.push([factoryName, count]);
}
Expand Down Expand Up @@ -166,4 +175,18 @@ export default class Server {
this.expressApp._router.stack.forEach(printRoutesMap.bind(null, []));
}
}

private add(type: string, parent: string, path: string) {
if (path.match(/\.ts$/) !== null || path.match(/\.js$/) !== null) {
const instance = require(`${parent}/${path}`).default;
if (instance) {
if (type === 'route' && instance instanceof Route) {
this.addRoute(instance);
}
if (type === 'factory' && instance instanceof Factory) {
this.addFactory(instance);
}
}
}
}
}
13 changes: 9 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"body-parser": "^1.18.2",
"colors": "^1.2.1",
"express": "^4.16.3",
"lair-db": "^1.5.4",
"fs-readdir-recursive": "^1.1.0",
"lair-db": "^1.6.0",
"methods": "^1.1.2",
"node-cron": "^1.2.1",
"winston": "^2.4.1"
Expand Down
36 changes: 36 additions & 0 deletions tests/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,17 @@ describe('#Server', () => {
expect(this.lair.getDevInfo()).to.have.property('b').that.is.an('object');
});
});

describe('#addFactoriesFromDir', () => {
it('should register factories in the Lair', () => {
expect(this.lair.getDevInfo()).to.be.eql({});
Server.getServer().addFactoriesFromDir(`${__dirname}/test-factories`);
const devInfo = this.lair.getDevInfo();
expect(devInfo).to.have.property('unit').that.is.an('object');
expect(devInfo).to.have.property('squad').that.is.an('object');
expect(devInfo).to.have.property('dir').that.is.an('object');
});
});
});

describe('#Server integration', () => {
Expand Down Expand Up @@ -151,6 +162,31 @@ describe('#Server integration', () => {
});
});

describe('#addRoutesFromDir', () => {

beforeEach(() => {
this.server.addRoutesFromDir(`${__dirname}/test-routes`);
});

it('should add a first route', done => {
this.server.startServer(() => chai.request(this.server.server)
.get('/api/v2/path')
.end((err, res) => {
expect(res).to.have.property('status', 200);
done();
}));
});

it('should add a second route', done => {
this.server.startServer(() => chai.request(this.server.server)
.get('/api/v2/path/subpath')
.end((err, res) => {
expect(res).to.have.property('status', 200);
done();
}));
});
});

describe('#createRecords', () => {
beforeEach(() => {
this.server.addFactory(modelA, 'a');
Expand Down
5 changes: 5 additions & 0 deletions tests/test-factories/squad.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {Factory} from 'lair-db';

export default Factory.create({
name: 'squad',
});
5 changes: 5 additions & 0 deletions tests/test-factories/subdir/dir.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {Factory} from 'lair-db';

export default Factory.create({
name: 'dir',
});
5 changes: 5 additions & 0 deletions tests/test-factories/unit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {Factory} from 'lair-db';

export default Factory.create({
name: 'unit',
});
3 changes: 3 additions & 0 deletions tests/test-routes/path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Route from '../../lib/route';

export default Route.createRoute('get', '/path');
3 changes: 3 additions & 0 deletions tests/test-routes/sub/subpath.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Route from '../../../lib/route';

export default Route.createRoute('get', '/path/subpath');

0 comments on commit ee6039e

Please sign in to comment.