Skip to content

Commit 69639ce

Browse files
author
Umed Khudoiberdiev
committed
renamed modules to loaders
1 parent 618e316 commit 69639ce

File tree

11 files changed

+81
-75
lines changed

11 files changed

+81
-75
lines changed

README.md

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
# Microframework
22

3-
Microframework is a simple tool that allows you to execute your modules in a proper order,
3+
Microframework is a simple tool that allows you to execute and configure your modules (called *loaders*) in a proper order,
44
helping you to organize bootstrap code in your application.
55

66
## Usage
77

8-
First, install the module:
8+
First, install microframework:
99

1010
```
1111
npm i microframework
1212
```
1313

14-
Second, create a simple "module" named `expressModule`:
14+
Second, create a simple "loader" named `expressLoader`.
15+
Loader is a place where you can configure all your modules during microframework bootstrap.
16+
All loaders are executed one by one in a sequential order.
1517

1618
```typescript
1719
import {MicroframeworkSettings} from "microframework";
1820

19-
export function expressModule(settings: MicroframeworkSettings) {
21+
export function expressLoader(settings: MicroframeworkSettings) {
2022

2123
// create express app
2224
const app = express();
@@ -28,69 +30,70 @@ export function expressModule(settings: MicroframeworkSettings) {
2830
// run application to listen on given port
2931
app.listen(3000);
3032

31-
// your module also can return a promise
33+
// your loader also can return a promise and microframework will wait for it in a proper order
3234
}
3335
```
3436

35-
Create `app.ts` and bootstrap a microframework and your express module:
37+
Create `app.ts` and bootstrap a microframework and your express loader:
3638

3739
```typescript
3840
import {bootstrapMicroframework} from "microframework";
39-
import {expressModule} from "./expressModule";
41+
import {expressLoader} from "./expressLoader";
4042

4143
bootstrapMicroframework([
42-
expressModule
44+
expressLoader
4345
])
4446
.then(() => console.log("Application is up and running."))
4547
.catch(error => console.log("Application is crashed: " + error));
4648
```
4749

48-
That's all. You can do same for other modules.
49-
Take a look on sample to understand better how concept of modules and their bootstrapping in microframework.
50+
That's all. You can do same for other loaders.
51+
Take a look on sample to understand better how concept of loaders and their bootstrapping in microframework.
5052

5153
## Settings
5254

5355
You can specify additional options to microframework.
5456

55-
* `logo` - Logo needs to be used before application launches. To use logo ansi-art module should be installed.
56-
* `showBootstrapTime` - If set to true then framework shows how much time was spend to bootstrap all modules.
57-
* `bootstrapTimeout` - Number of milliseconds to wait before framework will bootstrap all modules.
57+
* `logo` - Logo needs to be used before application launches. To use logo ansi-art node module should be installed.
58+
* `showBootstrapTime` - If set to true then framework shows how much time was spend to bootstrap microframework.
59+
* `bootstrapTimeout` - Number of milliseconds to wait before framework will bootstrap.
5860

5961
Example of using settings:
6062

6163
```typescript
6264
import {bootstrapMicroframework} from "microframework";
65+
import {expressLoader} from "./expressLoader";
6366

6467
bootstrapMicroframework({
6568
config: {
6669
logo: "MyApp",
6770
showBootstrapTime: true,
6871
bootstrapTimeout: 10
6972
},
70-
modules: [
71-
expressModule,
73+
loaders: [
74+
expressLoader,
7275
// ...
7376
]
7477
})
7578
.then(() => console.log("Application is up and running."))
7679
.catch(error => console.log("Application is crashed: " + error));
7780
```
7881

79-
## Sharing data across modules
82+
## Sharing data across loaders
8083

81-
Sometimes few modules need to communicate between each other and use shared data.
82-
For such purpose you can store the data in `settings` object passed to each module
83-
and use stored data across all other modules. For example:
84+
Sometimes few loaders need to communicate between each other and use shared data.
85+
For such purpose you can store the data in `settings` object passed to each loader
86+
and use stored data across all other loaders. For example:
8487

8588
```typescript
8689
import {MicroframeworkSettings} from "microframework";
8790

88-
export function expressModule(settings: MicroframeworkSettings) {
91+
export function expressLoader(settings: MicroframeworkSettings) {
8992

9093
// create express app
9194
const app = express();
9295

93-
// register all routes, Routes are just routes that should be stored outside of this module
96+
// register all routes, Routes are just routes that should be stored outside of this loader
9497
const routes: any = Routes;
9598
Object.keys(routes).forEach(routePath => app.get(routePath, routes[routePath]));
9699

@@ -101,12 +104,12 @@ export function expressModule(settings: MicroframeworkSettings) {
101104
}
102105
```
103106

104-
And another modules can use data this way:
107+
And another loaders can use data this way:
105108

106109
```typescript
107110
import {MicroframeworkSettings} from "microframework";
108111

109-
export function socketIoModule(settings: MicroframeworkSettings) {
112+
export function socketIoLoader(settings: MicroframeworkSettings) {
110113
const io = io();
111114
io.useExpress(settings.getData("express_app"));
112115
}
@@ -118,15 +121,16 @@ In the case if you want to shutdown running application you need to do following
118121

119122
```typescript
120123
import {bootstrapMicroframework} from "microframework";
124+
import {expressLoader} from "./expressLoader";
121125

122126
bootstrapMicroframework({
123127
config: {
124128
logo: "MyApp",
125129
showBootstrapTime: true,
126130
bootstrapTimeout: 10
127131
},
128-
modules: [
129-
expressModule,
132+
loaders: [
133+
expressLoader,
130134
// ...
131135
]
132136
})
@@ -142,7 +146,7 @@ bootstrapMicroframework({
142146
.catch(error => console.log("Application is crashed: " + error));
143147
```
144148

145-
All modules which use resources should release them, for example:
149+
All loaders which use resources should release them, for example:
146150

147151
```typescript
148152
export async function typeormModule(settings: MicroframeworkSettings) {
@@ -156,6 +160,6 @@ export async function typeormModule(settings: MicroframeworkSettings) {
156160
}
157161
});
158162

159-
settings.onShutdown(() => connection.close());
163+
settings.onShutdown(() => connection.close()); // closing connection on microframework shutdown
160164
}
161165
```

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "microframework",
33
"private": true,
4-
"version": "0.6.2",
4+
"version": "0.6.3",
55
"description": "Microframework is a minimalistic framework you can use with TypeScript and JavaScript.",
66
"license": "MIT",
77
"readmeFilename": "README.md",

sample/app.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import {bootstrapMicroframework} from "../src/index";
2-
import {expressModule} from "./modules/expressModule";
3-
import {typeormModule} from "./modules/typeormModule";
4-
import {winstonModule} from "./modules/winstonModule";
2+
import {expressLoader} from "./modules/expressLoader";
3+
import {typeormLoader} from "./modules/typeormLoader";
4+
import {winstonLoader} from "./modules/winstonLoader";
55

66
bootstrapMicroframework({
77
config: { // completely optional
88
logo: "MyAwesomeApp", // completely optional, to use this setting you'll need to install ascii-art module
99
showBootstrapTime: true, // completely optional
1010
},
11-
modules: [
12-
expressModule,
13-
winstonModule,
14-
typeormModule, // comment this module if you don't have database to setup connection with
11+
loaders: [
12+
expressLoader,
13+
winstonLoader,
14+
typeormLoader, // comment this module if you don't have database to setup connection with
1515

1616
// here we can setup other databases, any other lib we want to setup in our application
1717
]

sample/modules/expressModule.ts renamed to sample/modules/expressLoader.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as express from "express";
22
import {MicroframeworkSettings} from "../../src/MicroframeworkSettings";
33
import {Routes} from "../controllers/controllers";
44

5-
export function expressModule(settings: MicroframeworkSettings) {
5+
export function expressLoader(settings: MicroframeworkSettings) {
66

77
// create express app
88
const app = express();
@@ -14,6 +14,6 @@ export function expressModule(settings: MicroframeworkSettings) {
1414
// run application to listen on given port
1515
app.listen(3000);
1616

17-
// here we can set the data for other modules
17+
// here we can set the data for other loaders
1818
settings.setData("express_app", app);
1919
}

sample/modules/typeormModule.ts renamed to sample/modules/typeormLoader.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {createConnection} from "typeorm";
22
import {MicroframeworkSettings} from "../../src/MicroframeworkSettings";
33

4-
export async function typeormModule(settings: MicroframeworkSettings) {
4+
export async function typeormLoader(settings: MicroframeworkSettings) {
55
const connection = await createConnection({
66
driver: {
77
type: "mysql",

sample/modules/winstonModule.ts renamed to sample/modules/winstonLoader.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {MicroframeworkSettings} from "../../src/MicroframeworkSettings";
22
const winston = require("winston");
33

4-
export function winstonModule(settings: MicroframeworkSettings) {
4+
export function winstonLoader(settings: MicroframeworkSettings) {
55
winston.configure({
66
transports: [
77
new winston.transports.File({

src/Microframework.ts

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {MicroframeworkSettings} from "./MicroframeworkSettings";
2-
import {MicroframeworkModule} from "./MicroframeworkModule";
2+
import {MicroframeworkLoader} from "./MicroframeworkLoader";
33
import {MicroframeworkConfig} from "./MicroframeworkConfig";
44
import {MicroframeworkNotBootstrappedError} from "./error/MicroframeworkNotBootstrappedError";
55
import {MicroframeworkAsciiArtNotInstalledError} from "./error/MicroframeworkAsciiArtNotInstalledError";
@@ -19,12 +19,12 @@ export class Microframework {
1919
private frameworkConfig?: MicroframeworkConfig;
2020

2121
/**
22-
* Stores all registered microframework modules.
22+
* Stores all registered microframework loaders.
2323
*/
24-
private modules: MicroframeworkModule[] = [];
24+
private loaders: MicroframeworkLoader[] = [];
2525

2626
/**
27-
* Stores all settings of modules bootstrapped by microframework.
27+
* Stores all settings of loaders bootstrapped by microframework.
2828
* If its undefined it means framework is not bootstrapped yet.
2929
*/
3030
private frameworkSettings?: MicroframeworkSettings;
@@ -42,39 +42,39 @@ export class Microframework {
4242
}
4343

4444
/**
45-
* Registers module in the framework.
45+
* Registers loader in the framework.
4646
*/
47-
registerModule(module: MicroframeworkModule): this {
48-
this.modules.push(module);
47+
registerLoader(loader: MicroframeworkLoader): this {
48+
this.loaders.push(loader);
4949
return this;
5050
}
5151

5252
/**
53-
* Registers modules in the framework.
53+
* Registers loaders in the framework.
5454
*/
55-
registerModules(modules: MicroframeworkModule[]): this;
55+
registerLoaders(loaders: MicroframeworkLoader[]): this;
5656

5757
/**
58-
* Registers modules in the framework.
58+
* Registers loaders in the framework.
5959
*/
60-
registerModules(...modules: MicroframeworkModule[]): this;
60+
registerLoaders(...loaders: MicroframeworkLoader[]): this;
6161

6262
/**
63-
* Registers modules in the framework.
63+
* Registers loaders in the framework.
6464
*/
65-
registerModules(modules: any /* MicroframeworkModule[]|MicroframeworkModule[][] */): this {
66-
((modules as any[]) || []).forEach(module => {
67-
if (module instanceof Array) {
68-
this.modules.push(...module);
65+
registerLoaders(loaders: any /* MicroframeworkModule[]|MicroframeworkModule[][] */): this {
66+
((loaders as any[]) || []).forEach(loader => {
67+
if (loader instanceof Array) {
68+
this.loaders.push(...loader);
6969
} else {
70-
this.modules.push(module);
70+
this.loaders.push(loader);
7171
}
7272
});
7373
return this;
7474
}
7575

7676
/**
77-
* Bootstraps all modules.
77+
* Bootstraps microframework and loads all loaders.
7878
*/
7979
bootstrap(): Promise<this> {
8080
const settings = new MicroframeworkSettings();
@@ -86,9 +86,9 @@ export class Microframework {
8686
return this.createBootstrapTimeout();
8787

8888
}).then(() => {
89-
return this.runInSequence(this.modules, module => {
90-
const moduleResult = module(settings);
91-
return moduleResult instanceof Promise ? moduleResult : Promise.resolve();
89+
return this.runInSequence(this.loaders, loader => {
90+
const loaderResult = loader(settings);
91+
return loaderResult instanceof Promise ? loaderResult : Promise.resolve();
9292
});
9393

9494
}).then(() => {
@@ -100,7 +100,7 @@ export class Microframework {
100100
}
101101

102102
/**
103-
* Shutdowns all modules.
103+
* Shutdowns microframework and everything loaders registered for shutdown.
104104
*/
105105
shutdown(): Promise<this> {
106106
if (!this.frameworkSettings)
@@ -113,7 +113,7 @@ export class Microframework {
113113
}
114114

115115
/**
116-
* Returns microframework settings used and modified by bootstrapped modules.
116+
* Returns microframework settings used and modified by bootstrapped loaders.
117117
* If framework was not bootstrapped yet, this accessor will throw an error.
118118
*/
119119
get settings(): MicroframeworkSettings {
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {MicroframeworkConfig} from "./MicroframeworkConfig";
2-
import {MicroframeworkModule} from "./MicroframeworkModule";
2+
import {MicroframeworkLoader} from "./MicroframeworkLoader";
33

44
/**
55
* Microframework bootstrap configuration.
@@ -8,12 +8,14 @@ export interface MicroframeworkBootstrapConfig {
88

99
/**
1010
* Microframework configuration.
11+
* Can be file name, or array of file names from where to read configuration.
12+
* File should be relative to package.json of the project (project root).
1113
*/
12-
config?: MicroframeworkConfig;
14+
config?: MicroframeworkConfig|string|string[];
1315

1416
/**
15-
* Modules needs to be registered in microframework.
17+
* Loaders to be registered in microframework and executed on microframework bootstrap.
1618
*/
17-
modules?: MicroframeworkModule[];
19+
loaders?: MicroframeworkLoader[];
1820

1921
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import {MicroframeworkSettings} from "./MicroframeworkSettings";
22

33
/**
4-
* Microframework module is some functionality needs to be executed in order.
4+
* Microframework loader is a function that executes in order on microframework bootstrap.
55
*/
6-
export interface MicroframeworkModule {
6+
export interface MicroframeworkLoader {
77
(options?: MicroframeworkSettings): Promise<any>|any;
88
}

src/MicroframeworkSettings.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {ShutdownHandler} from "./ShutdownHandler";
22

33
/**
4-
* Bootstrap settings used across all modules.
4+
* Bootstrap settings used across all loader.
55
* Used to register shutdown handlers - special functions that will be executed when framework is shutdown.
66
* Also can be used to store and share data across modules.
77
*/

0 commit comments

Comments
 (0)