@furalure/configuration-manager 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/build/configurationManager.d.ts +9 -0
- package/build/configurationManager.d.ts.map +1 -0
- package/build/configurationManager.js +34 -0
- package/build/configurationManager.js.map +1 -0
- package/build/index.d.ts +2 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +2 -0
- package/build/index.js.map +1 -0
- package/package.json +24 -0
- package/src/configurationManager.ts +40 -0
- package/src/index.ts +1 -0
- package/tsconfig.json +39 -0
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { FastifyInstance } from 'fastify';
|
|
2
|
+
export declare class ConfigurationManager {
|
|
3
|
+
private folderPath;
|
|
4
|
+
private loadedRawConfig;
|
|
5
|
+
constructor(app: FastifyInstance);
|
|
6
|
+
loadConfigFile(fileName: string): void;
|
|
7
|
+
getRawConfig<ConfigFormat>(fileName: string): ConfigFormat;
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=configurationManager.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"configurationManager.d.ts","sourceRoot":"","sources":["../src/configurationManager.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAI/C,qBAAa,oBAAoB;IAC7B,OAAO,CAAC,UAAU,CAAsC;IACxD,OAAO,CAAC,eAAe,CAAqC;gBAEhD,GAAG,EAAE,eAAe;IAkBzB,cAAc,CAAC,QAAQ,EAAE,MAAM;IAQ/B,YAAY,CAAC,YAAY,EAAE,QAAQ,EAAE,MAAM,GAGd,YAAY;CAEnD"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
export class ConfigurationManager {
|
|
4
|
+
constructor(app) {
|
|
5
|
+
this.folderPath = path.join(process.cwd(), 'config');
|
|
6
|
+
this.loadedRawConfig = {};
|
|
7
|
+
const folderContent = fs.readdirSync(this.folderPath);
|
|
8
|
+
for (const index in folderContent) {
|
|
9
|
+
const configFile = folderContent[index];
|
|
10
|
+
if (configFile == undefined)
|
|
11
|
+
continue;
|
|
12
|
+
if (!configFile.endsWith(".json")) {
|
|
13
|
+
if (!configFile.endsWith(".example"))
|
|
14
|
+
app.log.warn(`ConfigurationManager - File "${configFile}" is not a ".json" file. Configuration only support ".json".`);
|
|
15
|
+
continue;
|
|
16
|
+
}
|
|
17
|
+
this.loadConfigFile(configFile.replace(".json", ""));
|
|
18
|
+
app.log.debug(`ConfigurationManager - Configuration loaded ${configFile}`);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
loadConfigFile(fileName) {
|
|
22
|
+
const configPath = path.join(this.folderPath, `${fileName}.json`);
|
|
23
|
+
const rawConfig = fs.readFileSync(configPath, 'utf-8');
|
|
24
|
+
// Replace ${VAR} with process.env.VAR
|
|
25
|
+
this.loadedRawConfig[fileName] = rawConfig.replace(/\$\{([a-zA-Z0-9_]+)\}/g, (_, key) => process.env[key] || "");
|
|
26
|
+
}
|
|
27
|
+
getRawConfig(fileName) {
|
|
28
|
+
const rawConfig = this.loadedRawConfig[fileName];
|
|
29
|
+
if (!rawConfig)
|
|
30
|
+
throw new Error(`Configuration ${fileName} not found`);
|
|
31
|
+
return JSON.parse(rawConfig);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=configurationManager.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"configurationManager.js","sourceRoot":"","sources":["../src/configurationManager.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB,MAAM,OAAO,oBAAoB;IAI7B,YAAY,GAAoB;QAHxB,eAAU,GAAG,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,QAAQ,CAAC,CAAC;QAChD,oB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AK,CAAC,iBAAiB,QAAQ,YAAY,CAAC,CAAC;QACvE,OAAO,IAAI,CAAC,KAAK,CAAC,SAAS,CAAiB,CAAC;IACjD,CAAC;CACJ"}
|
package/build/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":""}
|
package/build/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAA"}
|
package/package.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@furalure/configuration-manager",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"main": "index.js",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"build": "tsc -p tsconfig.json",
|
|
8
|
+
"start": "node ./index.js"
|
|
9
|
+
},
|
|
10
|
+
"keywords": [],
|
|
11
|
+
"author": "Furalure Events",
|
|
12
|
+
"license": "Apache-2.0",
|
|
13
|
+
"description": "",
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"fastify": "^5.6.2",
|
|
16
|
+
"tsx": "^4.20.6"
|
|
17
|
+
},
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"@types/node": "^24.10.1",
|
|
20
|
+
"concurrently": "^9.2.1",
|
|
21
|
+
"nodemon": "^3.1.11",
|
|
22
|
+
"typescript": "^5.9.3"
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { FastifyInstance } from 'fastify';
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
|
|
5
|
+
export class ConfigurationManager {
|
|
6
|
+
private folderPath = path.join(process.cwd(), 'config');
|
|
7
|
+
private loadedRawConfig: { [fileName: string]: string } = {}
|
|
8
|
+
|
|
9
|
+
constructor(app: FastifyInstance) {
|
|
10
|
+
const folderContent = fs.readdirSync(this.folderPath)
|
|
11
|
+
|
|
12
|
+
for (const index in folderContent) {
|
|
13
|
+
const configFile = folderContent[index]
|
|
14
|
+
|
|
15
|
+
if (configFile == undefined) continue
|
|
16
|
+
if (!configFile.endsWith(".json")) {
|
|
17
|
+
if (!configFile.endsWith(".example"))
|
|
18
|
+
app.log.warn(`ConfigurationManager - File "${configFile}" is not a ".json" file. Configuration only support ".json".`)
|
|
19
|
+
continue
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
this.loadConfigFile(configFile.replace(".json", ""))
|
|
23
|
+
app.log.debug(`ConfigurationManager - Configuration loaded ${configFile}`)
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
public loadConfigFile(fileName: string) {
|
|
28
|
+
const configPath = path.join(this.folderPath, `${fileName}.json`);
|
|
29
|
+
const rawConfig = fs.readFileSync(configPath, 'utf-8');
|
|
30
|
+
|
|
31
|
+
// Replace ${VAR} with process.env.VAR
|
|
32
|
+
this.loadedRawConfig[fileName] = rawConfig.replace(/\$\{([a-zA-Z0-9_]+)\}/g, (_, key) => process.env[key] || "");
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
public getRawConfig<ConfigFormat>(fileName: string) {
|
|
36
|
+
const rawConfig = this.loadedRawConfig[fileName];
|
|
37
|
+
if (!rawConfig) throw new Error(`Configuration ${fileName} not found`);
|
|
38
|
+
return JSON.parse(rawConfig) as ConfigFormat;
|
|
39
|
+
}
|
|
40
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import { ConfigurationManager } from "./configurationManager.js"
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
// Visit https://aka.ms/tsconfig to read more about this file
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
// File Layout
|
|
5
|
+
// "rootDir": "./src",
|
|
6
|
+
"outDir": "./build",
|
|
7
|
+
// Environment Settings
|
|
8
|
+
// See also https://aka.ms/tsconfig/module
|
|
9
|
+
"module": "node20",
|
|
10
|
+
"target": "es2017",
|
|
11
|
+
"types": [],
|
|
12
|
+
// For nodejs:
|
|
13
|
+
// "lib": ["esnext"],
|
|
14
|
+
// "types": ["node"],
|
|
15
|
+
// and npm install -D @types/node
|
|
16
|
+
// Other Outputs
|
|
17
|
+
"sourceMap": true,
|
|
18
|
+
"declaration": true,
|
|
19
|
+
"declarationMap": true,
|
|
20
|
+
// Stricter Typechecking Options
|
|
21
|
+
"noUncheckedIndexedAccess": true,
|
|
22
|
+
"exactOptionalPropertyTypes": true,
|
|
23
|
+
// Style Options
|
|
24
|
+
// "noImplicitReturns": true,
|
|
25
|
+
// "noImplicitOverride": true,
|
|
26
|
+
// "noUnusedLocals": true,
|
|
27
|
+
// "noUnusedParameters": true,
|
|
28
|
+
// "noFallthroughCasesInSwitch": true,
|
|
29
|
+
// "noPropertyAccessFromIndexSignature": true,
|
|
30
|
+
// Recommended Options
|
|
31
|
+
"strict": true,
|
|
32
|
+
"jsx": "react-jsx",
|
|
33
|
+
"verbatimModuleSyntax": true,
|
|
34
|
+
"isolatedModules": true,
|
|
35
|
+
"noUncheckedSideEffectImports": true,
|
|
36
|
+
"moduleDetection": "force",
|
|
37
|
+
"skipLibCheck": true,
|
|
38
|
+
}
|
|
39
|
+
}
|