-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from botorjs/create_cord
create cord
- Loading branch information
Showing
9 changed files
with
332 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = {}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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--; | ||
} | ||
} |
Oops, something went wrong.