Skip to content

Commit dce12b7

Browse files
authored
feat: add loader given env to config registry (#246)
1 parent af45031 commit dce12b7

File tree

18 files changed

+143
-92
lines changed

18 files changed

+143
-92
lines changed

e2e/game/client/main.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@ import { exampleSystem } from "./systems/example.system";
1414
export async function main(options: IRunOptions) {
1515
const app = NanoforgeFactory.createClient({
1616
tickRate: 60,
17-
environment: {
18-
serverAddress: "127.0.0.1",
19-
serverTcpPort: "4445",
20-
serverUdpPort: "4444",
21-
},
2217
});
2318

2419
const assetManager = new AssetManagerLibrary();
@@ -37,7 +32,15 @@ export async function main(options: IRunOptions) {
3732
app.useNetwork(network);
3833
app.use(Symbol("music"), music);
3934

40-
await app.init(options);
35+
await app.init({
36+
...options,
37+
env: {
38+
...options.env,
39+
SERVER_ADDRESS: "127.0.0.1",
40+
SERVER_TCP_PORT: "4445",
41+
SERVER_UDP_PORT: "4444",
42+
},
43+
});
4144

4245
const registry = ecs.registry;
4346

example/pong-network/client/main.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import { controlPlayer, draw, move, packetHandler } from "./systems";
1212

1313
export const app = NanoforgeFactory.createClient({
1414
tickRate: 60,
15-
environment: { serverTcpPort: "4445", serverUdpPort: "4444", serverAddress: "127.0.0.1" },
1615
});
1716

1817
export const layer = new Layer();
@@ -30,7 +29,15 @@ export const main = async (options: IRunOptions) => {
3029
app.useAssetManager(assetManager);
3130
app.useInput(input);
3231

33-
await app.init(options);
32+
await app.init({
33+
...options,
34+
env: {
35+
...options.env,
36+
SERVER_TCP_PORT: "4445",
37+
SERVER_UDP_PORT: "4444",
38+
SERVER_ADDRESS: "127.0.0.1",
39+
},
40+
});
3441

3542
const registry = ecsLibrary.registry;
3643

example/pong-network/server/main.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { bounce, move, packetHandler } from "./systems";
99

1010
export const app = NanoforgeFactory.createServer({
1111
tickRate: 60,
12-
environment: { listeningTcpPort: "4445", listeningUdpPort: "4444" },
1312
});
1413

1514
export const main = async (options: IRunOptions) => {
@@ -21,7 +20,10 @@ export const main = async (options: IRunOptions) => {
2120
app.useNetwork(network);
2221
app.useAssetManager(assetManager);
2322

24-
await app.init(options);
23+
await app.init({
24+
...options,
25+
env: { ...options.env, LISTENING_TCP_PORT: "4445", LISTENING_UDP_PORT: "4444" },
26+
});
2527

2628
const registry = ecsLibrary.registry;
2729

packages/common/src/context/contexts/executions/init.context.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { BaseContext } from "./base.context";
66
export class InitContext extends BaseContext {
77
private readonly _canvas: IRunClientOptions["canvas"] | undefined;
88
private readonly _files: IRunOptions["files"];
9+
private readonly _env: Record<string, string | undefined>;
910
private readonly _config: IConfigRegistry;
1011

1112
constructor(
@@ -18,6 +19,7 @@ export class InitContext extends BaseContext {
1819

1920
this._canvas = (options as IRunClientOptions)["canvas"];
2021
this._files = options.files;
22+
this._env = options.env;
2123
this._config = configRegistry;
2224
}
2325

@@ -29,6 +31,10 @@ export class InitContext extends BaseContext {
2931
return this._files;
3032
}
3133

34+
get env(): Record<string, string | undefined> {
35+
return this._env;
36+
}
37+
3238
get config(): IConfigRegistry {
3339
return this._config;
3440
}

packages/common/src/options/types/options.type.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ export type IRunOptions = IRunClientOptions | IRunServerOptions;
33
export interface IRunClientOptions {
44
canvas: HTMLCanvasElement;
55
files: Map<string, string>;
6+
env: Record<string, string | undefined>;
67
}
78

89
export interface IRunServerOptions {
910
files: Map<string, string>;
11+
env: Record<string, string | undefined>;
1012
}

packages/config/README.md

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,47 @@ bun add @nanoforge-dev/config
3131

3232
## Warning
3333

34-
This library is of exclusive usage for other libraries. To put variables in the environment to allow libraries to use it through this config library, you must put it in factory options :
34+
This library is of exclusive usage for other libraries. To put variables in the environment to allow libraries to use it through this config library, you must put it in `.env` file at the root of your project (or in your environment) :
35+
36+
```dotenv
37+
NANOFORGE_CLIENT_SERVER_TCP_PORT=4445
38+
NANOFORGE_CLIENT_SERVER_UDP_PORT=4444
39+
NANOFORGE_CLIENT_SERVER_ADDRESS=127.0.0.1
40+
```
41+
42+
You must prefix your variables according to this table :
43+
44+
| Prefix | Availability |
45+
| :------------------ | :----------------------- |
46+
| `NANOFORGE_CLIENT_` | Available in client only |
47+
| `NANOFORGE_SERVER_` | Available in server only |
48+
| `NANOFORGE_` | Available in both apps |
49+
50+
⚠️ Prefixs are removed in libs
51+
52+
You can also put it in init options (every value must be string or undefined) :
3553

3654
```ts
3755
import { type IRunOptions } from "@nanoforge-dev/common";
3856
import { NanoforgeFactory } from "@nanoforge-dev/core";
3957
import { NetworkLibrary } from "@nanoforge-dev/network";
4058

4159
export async function main(options: IRunOptions) {
42-
const app = NanoforgeFactory.createClient({
43-
environment: {
44-
serverTcpPort: "4445",
45-
serverUdpPort: "4444",
46-
serverAddress: "127.0.0.1",
47-
},
48-
});
60+
const app = NanoforgeFactory.createClient();
4961

5062
const network = new NetworkLibrary();
5163

5264
app.useNetwork(network);
5365

54-
await app.init(options);
66+
await app.init({
67+
...options,
68+
env: {
69+
...options.env,
70+
SERVER_TCP_PORT: "4445",
71+
SERVER_UDP_PORT: "4444",
72+
SERVER_ADDRESS: "127.0.0.1",
73+
},
74+
});
5575

5676
await app.run();
5777
}
@@ -92,23 +112,23 @@ export class ClientConfigNetwork {
92112
@Expose()
93113
@IsOptional()
94114
@IsPort()
95-
serverTcpPort?: string;
115+
SERVER_TCP_PORT?: string;
96116

97117
@Expose()
98118
@IsOptional()
99119
@IsPort()
100-
serverUdpPort?: string;
120+
SERVER_UDP_PORT?: string;
101121

102122
// This var must be ip address or fqdn (it cannot be undefined)
103123
@Expose()
104124
@IsIpOrFQDN()
105-
serverAddress?: string;
125+
SERVER_ADDRESS?: string;
106126

107127
// This var must be a byte length between 2 and 64. It can be undefined as it as a default value.
108128
@Expose()
109129
@Default("PACKET_END")
110130
@IsByteLength(2, 64)
111-
magicValue!: string;
131+
MAGIC_VALUE!: string;
112132
}
113133
```
114134

packages/config/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from "./exports";
22
export * from "./default";
3+
export * from "./transformers";
34
export * from "./validators";
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Transform } from "class-transformer";
2+
3+
const transformStringToBoolean = ({ value }: { value: string }) => {
4+
if (value === "true") return true;
5+
if (value === "false") return false;
6+
return undefined;
7+
};
8+
9+
export const TransformToBoolean = () => Transform(transformStringToBoolean);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { TransformToBoolean } from "./boolean.transformer";
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
export interface IApplicationOptions {
22
tickRate: number;
3-
environment: Record<string, unknown>;
43
}

0 commit comments

Comments
 (0)