Skip to content

Commit

Permalink
feat(loader): split module and config loader
Browse files Browse the repository at this point in the history
  • Loading branch information
eyolas committed Oct 2, 2017
1 parent 69c514d commit 6c1ed1d
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 91 deletions.
Expand Up @@ -27,7 +27,7 @@ Registry {
`;

exports[`whitout other scan 2`] = `
Loader {
LoaderModule {
"loadedFolder": Array [
"<PROJECT_ROOT>/__tests__/fixtures/loader/withoutscan",
],
Expand Down Expand Up @@ -80,7 +80,7 @@ Registry {
`;

exports[`with plugin 2`] = `
Loader {
LoaderModule {
"loadedFolder": Array [
"<PROJECT_ROOT>/__tests__/fixtures/loader/withoutscan",
"<PROJECT_ROOT>/__tests__/fixtures/loader/withscan",
Expand Down Expand Up @@ -116,7 +116,7 @@ Registry {
`;

exports[`with plugin and false scan path 2`] = `
Loader {
LoaderModule {
"loadedFolder": Array [
"<PROJECT_ROOT>/__tests__/fixtures/loader/withoutscan",
],
Expand Down Expand Up @@ -154,7 +154,7 @@ Registry {
`;

exports[`with scan 2`] = `
Loader {
LoaderModule {
"loadedFolder": Array [
"<PROJECT_ROOT>/__tests__/fixtures/loader/withscan",
"<PROJECT_ROOT>/__tests__/fixtures/loader/subfoldertoscan",
Expand Down
@@ -1,23 +1,21 @@
// tslint:disable:one-line
// tslint:disable:no-unused-expression
import { Loader } from '../src/loader';
import { LoaderConfig } from '../src/loaders';
import * as mock from 'mock-fs';
import * as path from 'path';

let loader: Loader;
let loader: LoaderConfig;
beforeEach(() => {
loader = new Loader();
loader = new LoaderConfig();
});

test(`with config folder doesn't exist`, () => {
const config = loader.loadConfig('doesntexist/config');
const config = loader.load('doesntexist/config');
expect(config).toMatchSnapshot();
});

test(`with bad application.yml`, () => {
const config = loader.loadConfig(
path.resolve(__dirname, './fixtures/badyml')
);
const config = loader.load(path.resolve(__dirname, './fixtures/badyml'));
expect(config).toMatchSnapshot();
});

Expand All @@ -27,7 +25,7 @@ test(`with application.yml empty`, () => {
'application.yml': ``
}
});
const config = loader.loadConfig('test/config');
const config = loader.load('test/config');
expect(config).toMatchSnapshot();
mock.restore();
});
Expand All @@ -41,7 +39,7 @@ test(`with application.yml with nothing`, () => {
`
}
});
const config = loader.loadConfig('test/config');
const config = loader.load('test/config');
expect(config).toEqual({});
mock.restore();
});
Expand All @@ -64,17 +62,17 @@ describe('with application.yml', () => {
});

test('load application.yml', () => {
const config = loader.loadConfig('test/config');
const config = loader.load('test/config');
expect(config).toMatchSnapshot();
});

test('load with bad profile', () => {
const config = loader.loadConfig('test/config', 'int');
const config = loader.load('test/config', 'int');
expect(config).toMatchSnapshot();
});

test('load with prod profile', () => {
const config = loader.loadConfig('test/config', 'prod');
const config = loader.load('test/config', 'prod');
expect(config).toMatchSnapshot();
});

Expand Down
@@ -1,17 +1,17 @@
// tslint:disable:one-line
// tslint:disable:no-unused-expression
import { Loader } from '../src/loader';
import { LoaderModule } from '../src/loaders';
import * as path from 'path';
import { Scan } from '../src/index';
import { METADATA_KEY } from '../src/constants';

let loader: Loader;
let loader: LoaderModule;
beforeEach(() => {
loader = new Loader();
loader = new LoaderModule();
});

test('whitout other scan', () => {
const registry = loader.loadModules(
const registry = loader.load(
path.resolve(__dirname, './fixtures/loader/withoutscan'),
[]
);
Expand All @@ -20,7 +20,7 @@ test('whitout other scan', () => {
});

test('with scan', () => {
const registry = loader.loadModules(
const registry = loader.load(
path.resolve(__dirname, './fixtures/loader/withscan'),
[]
);
Expand All @@ -32,7 +32,7 @@ test('with plugin', () => {
@Scan(path.resolve(__dirname, './fixtures/loader/withscan'))
class Plugin {}

const registry = loader.loadModules(
const registry = loader.load(
path.resolve(__dirname, './fixtures/loader/withoutscan'),
[new Plugin()]
);
Expand All @@ -45,7 +45,7 @@ test('with plugin and false scan path', () => {

Reflect.defineMetadata(METADATA_KEY.scan, [], Plugin);

const registry = loader.loadModules(
const registry = loader.load(
path.resolve(__dirname, './fixtures/loader/withoutscan'),
[new Plugin()]
);
Expand Down
10 changes: 6 additions & 4 deletions packages/core/src/gabliam.ts
Expand Up @@ -8,7 +8,7 @@ import {
CORE_CONFIG,
VALUE_EXTRACTOR
} from './constants';
import { Loader } from './loader';
import { LoaderModule, LoaderConfig } from './loaders';
import { createContainer } from './container';
import { Registry } from './registry';
import * as d from 'debug';
Expand All @@ -34,7 +34,9 @@ export class Gabliam {
*/
public registry: Registry = new Registry();

public loader: Loader = new Loader();
public loaderModule: LoaderModule = new LoaderModule();

public loaderConfig: LoaderConfig = new LoaderConfig();

public plugins: interfaces.GabliamPlugin[] = [];

Expand Down Expand Up @@ -89,7 +91,7 @@ export class Gabliam {
* Loading phase
*/
this.registry.addRegistry(
this.loader.loadModules(this.options.scanPath, this.plugins)
this.loaderModule.load(this.options.scanPath, this.plugins)
);

/**
Expand Down Expand Up @@ -184,7 +186,7 @@ export class Gabliam {
* Load config file and bind result in APP_CONFIG
*/
private _initializeConfig() {
const config = (this.config = this.loader.loadConfig(
const config = (this.config = this.loaderConfig.load(
this.options.configPath
));
this.container.bind(APP_CONFIG).toConstantValue(config);
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/loaders/index.ts
@@ -0,0 +1,2 @@
export * from './loader-config';
export * from './loader-module';
57 changes: 57 additions & 0 deletions packages/core/src/loaders/loader-config.ts
@@ -0,0 +1,57 @@
import * as glob from 'glob';
import * as yaml from 'js-yaml';
import * as fs from 'fs';
import * as _ from 'lodash';
import * as d from 'debug';

const debug = d('Gabliam:loader-config');

export class LoaderConfig {
/**
* Load configuration
* @param {string} folder the configuration folder
* @returns any
*/
load(folder: string, profile = process.env.PROFILE || null): any {
debug('loadConfig', folder);
const files = glob.sync('**/application?(-+([a-zA-Z])).yml', {
cwd: folder
});
let config = {};

if (!files || files.length === 0) {
return config;
}

const defaultProfileFile = files.find(file => file === 'application.yml');

if (defaultProfileFile) {
config = this.loadYmlFile(`${folder}/${defaultProfileFile}`);
}

if (profile) {
const profileFile = files.find(
file => file === `application-${profile}.yml`
);

if (profileFile) {
config = _.merge(
{},
config,
this.loadYmlFile(`${folder}/${profileFile}`)
);
}
}
debug('loadConfig', config);
return config;
}

private loadYmlFile(ymlPath: string) {
const data = fs.readFileSync(ymlPath, 'utf8');
try {
return yaml.load(data) || {};
} catch (e) {
return {};
}
}
}
@@ -1,21 +1,19 @@
import * as glob from 'glob';
import * as yaml from 'js-yaml';
import * as fs from 'fs';
import * as _ from 'lodash';
import * as d from 'debug';

import { Registry } from './registry';
import { METADATA_KEY } from './constants';
import { RegistryMetada, GabliamPlugin } from './interfaces';
import { isObject } from './utils';
import { Registry } from '../registry';
import { METADATA_KEY } from '../constants';
import { RegistryMetada, GabliamPlugin } from '../interfaces';
import { isObject } from '../utils';

const debug = d('Gabliam:loader');
const reg = /^.*(git|svn|node_modules|dist|build).*/;

/**
* Loader
*/
export class Loader {
export class LoaderModule {
/**
* List of loaded folder
*/
Expand All @@ -26,7 +24,7 @@ export class Loader {
* @param {string} scan
* @param {GabliamPlugin[]} plugins
*/
loadModules(scan: string, plugins: GabliamPlugin[]) {
load(scan: string, plugins: GabliamPlugin[]) {
const folders = plugins.reduce(
(prev, current) => {
if (
Expand All @@ -48,45 +46,6 @@ export class Loader {
return this.loadFolders(...folders);
}

/**
* Load configuration
* @param {string} folder the configuration folder
* @returns any
*/
loadConfig(folder: string, profile = process.env.PROFILE || null): any {
debug('loadConfig', folder);
const files = glob.sync('**/application?(-+([a-zA-Z])).yml', {
cwd: folder
});
let config = {};

if (!files || files.length === 0) {
return config;
}

const defaultProfileFile = files.find(file => file === 'application.yml');

if (defaultProfileFile) {
config = this.loadYmlFile(`${folder}/${defaultProfileFile}`);
}

if (profile) {
const profileFile = files.find(
file => file === `application-${profile}.yml`
);

if (profileFile) {
config = _.merge(
{},
config,
this.loadYmlFile(`${folder}/${profileFile}`)
);
}
}
debug('loadConfig', config);
return config;
}

/**
* Load on folder
* @param {string} folder
Expand Down Expand Up @@ -143,13 +102,4 @@ export class Loader {
}
return registry;
}

private loadYmlFile(ymlPath: string) {
const data = fs.readFileSync(ymlPath, 'utf8');
try {
return yaml.load(data) || {};
} catch (e) {
return {};
}
}
}
8 changes: 4 additions & 4 deletions packages/core/src/testing/gabliam.ts
@@ -1,14 +1,14 @@
import * as path from 'path';
import { Gabliam } from '../gabliam';
import { LoaderTest } from './loader';
import { LoaderConfigTest } from './loader';
import { METADATA_KEY } from '../constants';
import { RegistryMetada } from '../interfaces';
import * as _ from 'lodash';

export class GabliamTest {
public gab: Gabliam;

public loader: LoaderTest;
public loaderConfig: LoaderConfigTest;

constructor(gab?: Gabliam) {
if (gab) {
Expand All @@ -20,7 +20,7 @@ export class GabliamTest {
});
}

this.loader = this.gab.loader = new LoaderTest();
this.loaderConfig = this.gab.loaderConfig = new LoaderConfigTest();
}

async build() {
Expand All @@ -43,7 +43,7 @@ export class GabliamTest {
}

addConf(p: string, conf: any) {
this.loader.addConfig(p, conf);
this.loaderConfig.addConfig(p, conf);
}

addClass(ctrl: any) {
Expand Down

0 comments on commit 6c1ed1d

Please sign in to comment.