Skip to content

Commit

Permalink
Merge pull request #3 from botorjs/create_cord
Browse files Browse the repository at this point in the history
create cord
  • Loading branch information
NortonBen committed Mar 7, 2019
2 parents 0a488ad + 19d3442 commit 24a9add
Show file tree
Hide file tree
Showing 9 changed files with 332 additions and 7 deletions.
161 changes: 159 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,159 @@
# cord
Library that core of Botorjs is connect component
# Cord
[![Build Status](https://travis-ci.org/botorjs/cord.svg?branch=master)](https://travis-ci.org/botorjs/cord)
[![Coverage Status](https://coveralls.io/repos/github/botorjs/cord/badge.svg?branch=master)](https://coveralls.io/github/botorjs/cord?branch=master)

### Library that core of Botorjs is connect component using [Boot](https://github.com/botorjs/boot)

# Installation
```
npm install @botorjs/cord --save
```

# Setup and Example

* Folder
```
project
└───services
│ │ Service.ts
└───providers
│ │ ProviderTest.ts
└───start
│ │ app.ts
|
│ .env
| index.ts
```

* Service.ts
```js

export class ServiceTest {
private _count: number;

constructor() {
this._count = 0;
}

public get count(): number {
return this._count
}

public inc() {
this._count++;
}

public sub() {
this._count--;
}
}

```

* ProviderTest.ts
```js

import { ServiceProvider, Boot } from '@botorjs/boot';
import { ServiceTest } from '../services/Service';

export class ProviderTest extends ServiceProvider {

register(app: Boot) {
app.ioc.singleton(ServiceTest.name, ServiceTest)
app.alias.add("service", ServiceTest);
}

boot(app: Boot) {
const service = app.ioc.get<ServiceTest>(ServiceTest);
service.inc();
}
}

```

* app.ts
```js
import { App } from '@botorjs/cord';
import { ProviderTest } from '../providers/ProviderTest';

const app: App = {
providers: [
ProviderTest
],
aliases: {
"APP_PATH": "ROOT",
},
}
export = app;

```

* .env
```
TEST_ENV=test
```

* index.ts
```js

import { App, Cord } from '@botorjs/cord';


var cord: Cord = new Cord();

cord.boot.hook.on("start", () => {
console.log("start work");
})

cord.root(__dirname)
.preload()
.fire();

var service = cord.boot.get<ServiceTest>(ServiceTest);
expect(service.count).to.eql(1);
service.sub();
expect(service.count).to.eql(0);

var env: Env = cord.boot.get<Env>(Env);
var val = env.get("TEST_ENV");
expect(val).to.eql("test");
```

# API

## Cord
* Cord is backgroud connect to component

| Property | Description |
|---------- |:-------------|------|
| config | get config cord |
| boot | get Boot |
| root(folder) | set folder root of appliction |
| preload() | Preload application, it will load the ServiceProvider to Boot and load Alisa to boot. Load environment and file .env (defaul config) to Env and add to IoC of Boot |
| fire | call event to system notification start application |

## Env
* load config in file `.env`(default) to environment and get set environment

| Property | Description |
|---------- |:-------------|------|
| get | get value environment |
| set | set value environment |

## ConfigCord
* load config in file `.env`(default) to environment and get set environment

| Property | Description | Defaul |
|---------- |:-------------|------|------------|
| start_folder | Folder the file start is config input of component | "start" |
| app_file | sFile config provider of Boot | "app.ts" |
| root | Root folder of application | null |
| env_file | File environment of application | ".env" |

## App
* Config default of App

| Property | Description |
|---------- |:-------------|------|
| providers | the ServiceProvider of Boot |
| aliases | the alias of Boot |
6 changes: 6 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Env } from './src/Env';
import { App } from './src/App';
import { Cord} from './src/Cord';
import { ConfigCord } from './src/ConfigCord';

export { Env, Cord, ConfigCord, App }
9 changes: 8 additions & 1 deletion src/App.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@

export class App {
public provivders: any[] = [];
/**
* the ServiceProvider of Boot
*/
public providers: any[] = [];

/**
* the alias of Boot
*/
public aliases: object = {};
}
15 changes: 15 additions & 0 deletions src/ConfigCord.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@


export class ConfigCord {
/**
* Folder the file start is config input of component
*/
public start_folder: string = "start";

/**
* File config provider of Boot
*/
public app_file: string = "app.ts"

/**
* Root folder
*/
public root: string;

/**
* File environment of application
*/
public env_file: string = '.env';
}
45 changes: 41 additions & 4 deletions src/Cord.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
import { Boot } from '@botorjs/boot';
import { Boot, TypeContainer } from '@botorjs/boot';
import { ConfigCord } from './ConfigCord';
import { App } from './App';
import path from 'path';
import * as path from 'path';
import { Env } from './Env';


/**
* Cord is backgroud connect to component
*
*/
export class Cord {
private _boot: Boot;
private _config: ConfigCord;
private _app_config: App;
private _env: Env;

/**
* contructor object cord with config, if config default is null, when it will create config default
*
* @param config config corder
*/
constructor(config: ConfigCord = null) {
this._boot = new Boot();
if(config == null) {
Expand All @@ -17,29 +29,54 @@ export class Cord {
}
}

/**
* call event to system notification start application
*/
public fire(): Cord {
this._boot.hook.emit("start");
return this;
}

/**
* get Boot
*/
public get boot(): Boot {
return this._boot;
}

/**
* Preload application, it will load the ServiceProvider to Boot and load Alisa to boot
* Load environment and file .env (defaul config) to Env and add to IoC of Boot
*/
public preload(): Cord {
const file_app = path.join(this._config.root, this._config.app_file, this._config.app_file);
this._env = new Env(this._config);
this._boot.ioc.singleton(Env.name, this._env as any, TypeContainer.Contant);
this._boot.alias.add(Env, Env.name);
this._boot.ioc.singleton("ROOT", this._config.root as any);
const file_app = path.join(this._config.root, this._config.start_folder, this._config.app_file);
var app = require(file_app);
this._app_config = app as App;
this._boot.loader.add(this._app_config.provivders);
this._boot.loader.add(this._app_config.providers);
for(var key of Object.keys(this._app_config.aliases)) {
this._boot.alias.add(key, this._app_config.aliases[key]);
}
this._boot.preload();
return this;
}

/**
* set folder root of appliction
*
* @param root folder root of application
*/
public root(root: string): Cord {
this._config.root = root;
return this;
}

/**
* get config cord
*/
public get config(): ConfigCord {
return this._config;
}
Expand Down
56 changes: 56 additions & 0 deletions tests/cord.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { expect } from "chai";
import { Cord } from '../src/Cord';
import { ConfigCord } from "../src/ConfigCord";
import { ServiceTest } from "./start/Service";
import { Env } from "../src/Env";

var config: ConfigCord = new ConfigCord();
config.env_file = "test.env";

var cord: Cord = null;

describe('Cord test', function() {

before(function () {
cord = new Cord(config);
})

after(function(){
cord = null;
})

it('set root and get config', function() {
cord.root(__dirname);
var config = cord.config;
expect(config.root).to.eql(__dirname);
});

it('test preload cord', function() {
cord.preload();
var service = cord.boot.get<ServiceTest>(ServiceTest);
expect(service.count).to.eql(1);
service.sub();
expect(service.count).to.eql(0);
});


it('get env', function() {
var env: Env = cord.boot.get<Env>(Env);
var val = env.get("TEST_ENV");
expect(val).to.eql("test");
});


it('fire cord', function() {
cord.boot.hook.on("start", () => {
expect(true).to.eql(true);
})
cord.fire();
});

it('cord not config', function() {
cord = new Cord();
expect(cord.config).to.not.eql(null);
});

});
15 changes: 15 additions & 0 deletions tests/start/ProviderTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { ServiceProvider, Boot } from '@botorjs/boot';
import { ServiceTest } from './Service';

export class ProviderTest extends ServiceProvider {

register(app: Boot) {
app.ioc.singleton(ServiceTest.name, ServiceTest)
app.alias.add("service", ServiceTest);
}

boot(app: Boot) {
const service = app.ioc.get<ServiceTest>(ServiceTest);
service.inc();
}
}
20 changes: 20 additions & 0 deletions tests/start/Service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

export class ServiceTest {
private _count: number;

constructor() {
this._count = 0;
}

public get count(): number {
return this._count
}

public inc() {
this._count++;
}

public sub() {
this._count--;
}
}

0 comments on commit 24a9add

Please sign in to comment.