ts-game-environment 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/dist/cfg/config.d.ts +17 -0
- package/dist/cfg/config.js +68 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/package.json +42 -4
package/README.md
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
#
|
|
1
|
+
# ts-game-environment
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Typescript Game Server Environment Utils
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { SerializableKey, SerializableValue } from 'ts-game-common';
|
|
2
|
+
export declare const ENV_TIMEZONE = "time_zone", ENV_PWD = "pwd";
|
|
3
|
+
declare class Config {
|
|
4
|
+
private static _instance;
|
|
5
|
+
private _conf;
|
|
6
|
+
private constructor();
|
|
7
|
+
private _init_base;
|
|
8
|
+
private _init;
|
|
9
|
+
static get instance(): Config;
|
|
10
|
+
private merge;
|
|
11
|
+
private update;
|
|
12
|
+
getConf(key: SerializableKey, def?: SerializableValue): SerializableValue;
|
|
13
|
+
getSubConf(key: SerializableKey, objKey: string, def?: SerializableValue): SerializableValue;
|
|
14
|
+
getAllConfig(): SerializableValue;
|
|
15
|
+
}
|
|
16
|
+
export declare const Configuration: Config;
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import * as fs from 'fs';
|
|
2
|
+
import * as path from 'path';
|
|
3
|
+
import { isSerializableObject, mergeSerializableValue } from 'ts-game-common';
|
|
4
|
+
const __KEYS__ = ['SHELL', 'PWD', 'HOME', 'LANG', 'TERM', 'USER'];
|
|
5
|
+
export const ENV_TIMEZONE = 'time_zone', ENV_PWD = 'pwd';
|
|
6
|
+
class Config {
|
|
7
|
+
constructor() {
|
|
8
|
+
this._conf = {};
|
|
9
|
+
this._init();
|
|
10
|
+
this._init_base();
|
|
11
|
+
}
|
|
12
|
+
_init_base() {
|
|
13
|
+
if (!(ENV_PWD in this._conf)) {
|
|
14
|
+
this.update(ENV_PWD, process.cwd());
|
|
15
|
+
}
|
|
16
|
+
const workDir = this.getConf(ENV_PWD);
|
|
17
|
+
const def = path.join(workDir, 'conf', 'default.json');
|
|
18
|
+
if (fs.existsSync(def)) {
|
|
19
|
+
this.merge(JSON.parse(fs.readFileSync(def, 'utf-8')));
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
_init() {
|
|
23
|
+
__KEYS__.forEach((it) => {
|
|
24
|
+
if (it in process.env)
|
|
25
|
+
this.update(it, process.env[it]);
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
static get instance() {
|
|
29
|
+
if (!Config._instance)
|
|
30
|
+
Config._instance = new Config();
|
|
31
|
+
return Config._instance;
|
|
32
|
+
}
|
|
33
|
+
merge(vals) {
|
|
34
|
+
mergeSerializableValue(this._conf, vals);
|
|
35
|
+
return this;
|
|
36
|
+
}
|
|
37
|
+
update(key, val) {
|
|
38
|
+
const cur = this._conf[key];
|
|
39
|
+
if (isSerializableObject(cur) && isSerializableObject(val)) {
|
|
40
|
+
mergeSerializableValue(cur, val);
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
this._conf[key] = val;
|
|
44
|
+
}
|
|
45
|
+
return this;
|
|
46
|
+
}
|
|
47
|
+
getConf(key, def = '') {
|
|
48
|
+
return key in this._conf ? this._conf[key] : def;
|
|
49
|
+
}
|
|
50
|
+
getSubConf(key, objKey, def = '') {
|
|
51
|
+
if (!objKey)
|
|
52
|
+
return this.getConf(key, def);
|
|
53
|
+
const parts = objKey.split('.').filter((s) => s !== '');
|
|
54
|
+
let cur = this._conf;
|
|
55
|
+
for (const p of parts) {
|
|
56
|
+
if (cur && typeof cur === 'object' && p in cur)
|
|
57
|
+
cur = cur[p];
|
|
58
|
+
else
|
|
59
|
+
return def;
|
|
60
|
+
}
|
|
61
|
+
return key in cur ? cur[key] : def;
|
|
62
|
+
}
|
|
63
|
+
getAllConfig() {
|
|
64
|
+
return this._conf;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
Config._instance = null;
|
|
68
|
+
export const Configuration = Config.instance;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { Configuration } from './cfg/config';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { Configuration } from './cfg/config';
|
package/package.json
CHANGED
|
@@ -1,6 +1,44 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ts-game-environment",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "environment config utils for game server",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"test": "vitest",
|
|
10
|
+
"build": "tsc",
|
|
11
|
+
"dev": "tsc --watch",
|
|
12
|
+
"demo": "tsx demo/index.ts",
|
|
13
|
+
"format": "prettier --write \"src/**/*.ts\"",
|
|
14
|
+
"prepare": "npm run build",
|
|
15
|
+
"prepublishOnly": "npm run test",
|
|
16
|
+
"preversion": "npm run format",
|
|
17
|
+
"version": "git add -A .",
|
|
18
|
+
"postversion": "git push"
|
|
19
|
+
},
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "https://github.com/baozilaji/ts-game-environment.git"
|
|
23
|
+
},
|
|
24
|
+
"keywords": [
|
|
25
|
+
"typescript",
|
|
26
|
+
"game server",
|
|
27
|
+
"game environment"
|
|
28
|
+
],
|
|
29
|
+
"files": [
|
|
30
|
+
"dist/**/*"
|
|
31
|
+
],
|
|
32
|
+
"author": "baozilaji@126.com",
|
|
33
|
+
"license": "ISC",
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"ts-game-common": "^1.0.2"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@types/node": "^26.6.6",
|
|
39
|
+
"prettier": "^3.9.10",
|
|
40
|
+
"tsx": "^4.23.15",
|
|
41
|
+
"typescript": "^7.0.2",
|
|
42
|
+
"vitest": "^5.1.0"
|
|
43
|
+
}
|
|
44
|
+
}
|