@sprout-idws/sprout-config-loader 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.
@@ -0,0 +1,9 @@
1
+ import { DynamicModule } from '@nestjs/common';
2
+ export interface ConfigLoaderModuleOptions {
3
+ envFilePath?: string;
4
+ isGlobal?: boolean;
5
+ }
6
+ export declare class ConfigLoaderModule {
7
+ static forRoot(options?: ConfigLoaderModuleOptions): DynamicModule;
8
+ }
9
+ //# sourceMappingURL=config-loader.module.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config-loader.module.d.ts","sourceRoot":"","sources":["../src/config-loader.module.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAkB,MAAM,gBAAgB,CAAC;AAI/D,MAAM,WAAW,yBAAyB;IACxC,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB;AAED,qBAEa,kBAAkB;IAC7B,MAAM,CAAC,OAAO,CAAC,OAAO,GAAE,yBAA8B,GAAG,aAAa;CAevE"}
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var ConfigLoaderModule_1;
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ exports.ConfigLoaderModule = void 0;
11
+ const common_1 = require("@nestjs/common");
12
+ const config_1 = require("@nestjs/config");
13
+ const config_loader_service_1 = require("./config-loader.service");
14
+ let ConfigLoaderModule = ConfigLoaderModule_1 = class ConfigLoaderModule {
15
+ static forRoot(options = {}) {
16
+ const { envFilePath = '.env', isGlobal = true } = options;
17
+ return {
18
+ module: ConfigLoaderModule_1,
19
+ imports: [
20
+ config_1.ConfigModule.forRoot({
21
+ isGlobal,
22
+ envFilePath,
23
+ load: [() => config_loader_service_1.ConfigLoaderService.loadConfigFiles()],
24
+ }),
25
+ ],
26
+ exports: [config_1.ConfigModule],
27
+ };
28
+ }
29
+ };
30
+ exports.ConfigLoaderModule = ConfigLoaderModule;
31
+ exports.ConfigLoaderModule = ConfigLoaderModule = ConfigLoaderModule_1 = __decorate([
32
+ (0, common_1.Global)(),
33
+ (0, common_1.Module)({})
34
+ ], ConfigLoaderModule);
35
+ //# sourceMappingURL=config-loader.module.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config-loader.module.js","sourceRoot":"","sources":["../src/config-loader.module.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,2CAA+D;AAC/D,2CAA8C;AAC9C,mEAA8D;AASvD,IAAM,kBAAkB,0BAAxB,MAAM,kBAAkB;IAC7B,MAAM,CAAC,OAAO,CAAC,UAAqC,EAAE;QACpD,MAAM,EAAE,WAAW,GAAG,MAAM,EAAE,QAAQ,GAAG,IAAI,EAAE,GAAG,OAAO,CAAC;QAE1D,OAAO;YACL,MAAM,EAAE,oBAAkB;YAC1B,OAAO,EAAE;gBACP,qBAAY,CAAC,OAAO,CAAC;oBACnB,QAAQ;oBACR,WAAW;oBACX,IAAI,EAAE,CAAC,GAAG,EAAE,CAAC,2CAAmB,CAAC,eAAe,EAAE,CAAC;iBACpD,CAAC;aACH;YACD,OAAO,EAAE,CAAC,qBAAY,CAAC;SACxB,CAAC;IACJ,CAAC;CACF,CAAA;AAhBY,gDAAkB;6BAAlB,kBAAkB;IAF9B,IAAA,eAAM,GAAE;IACR,IAAA,eAAM,EAAC,EAAE,CAAC;GACE,kBAAkB,CAgB9B"}
@@ -0,0 +1,8 @@
1
+ import { Logger } from '@nestjs/common';
2
+ export declare class ConfigLoaderService {
3
+ static logger: Logger;
4
+ static loadConfigFiles(): Promise<Record<string, unknown>>;
5
+ private static getKeyFromFileName;
6
+ private static loadFilesFromDirectory;
7
+ }
8
+ //# sourceMappingURL=config-loader.service.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config-loader.service.d.ts","sourceRoot":"","sources":["../src/config-loader.service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAOxC,qBAAa,mBAAmB;IAC9B,MAAM,CAAC,MAAM,SAAwC;WAExC,eAAe,IAAI,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAYhE,OAAO,CAAC,MAAM,CAAC,kBAAkB;mBAIZ,sBAAsB;CA6C5C"}
@@ -0,0 +1,90 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.ConfigLoaderService = void 0;
37
+ const common_1 = require("@nestjs/common");
38
+ const fs = __importStar(require("fs"));
39
+ const path = __importStar(require("path"));
40
+ const CONFIGS_BASE_PATH = process.env.CONFIGS_BASE_PATH || '/run/configs';
41
+ const SECRETS_BASE_PATH = process.env.SECRETS_BASE_PATH || '/run/secrets';
42
+ class ConfigLoaderService {
43
+ static async loadConfigFiles() {
44
+ const loadedConfig = await ConfigLoaderService.loadFilesFromDirectory(CONFIGS_BASE_PATH, 'config');
45
+ const loadedSecrets = await ConfigLoaderService.loadFilesFromDirectory(SECRETS_BASE_PATH, 'secret');
46
+ return { ...loadedConfig, ...loadedSecrets };
47
+ }
48
+ static getKeyFromFileName(fileName) {
49
+ return fileName.replace(/[^a-zA-Z0-9]/g, '_').toUpperCase();
50
+ }
51
+ static async loadFilesFromDirectory(dirPath, type) {
52
+ if (!fs.existsSync(dirPath)) {
53
+ this.logger.warn(`${type} directory not found: ${dirPath}`);
54
+ return {};
55
+ }
56
+ try {
57
+ const loadedConfig = {};
58
+ const files = fs.readdirSync(dirPath, { withFileTypes: true });
59
+ const fileCount = files.filter((file) => file.isFile()).length;
60
+ if (fileCount === 0) {
61
+ this.logger.log(`No ${type} files found in ${dirPath}`);
62
+ return {};
63
+ }
64
+ for (const file of files) {
65
+ if (file.isFile()) {
66
+ const filePath = path.join(dirPath, file.name);
67
+ const key = ConfigLoaderService.getKeyFromFileName(file.name);
68
+ const content = fs.readFileSync(filePath, 'utf-8').trim();
69
+ if (!process.env[key]) {
70
+ process.env[key] = content;
71
+ loadedConfig[key] = content;
72
+ this.logger.debug(`Loaded ${type}: ${key}`);
73
+ }
74
+ else {
75
+ this.logger.warn(`Environment variable ${key} already exists, skipping override from ${file.name}`);
76
+ }
77
+ }
78
+ }
79
+ this.logger.log(`Loaded ${fileCount} ${type} file(s) from ${dirPath}`);
80
+ return loadedConfig;
81
+ }
82
+ catch (error) {
83
+ this.logger.error(`Error loading ${type} files: ${error}`);
84
+ return {};
85
+ }
86
+ }
87
+ }
88
+ exports.ConfigLoaderService = ConfigLoaderService;
89
+ ConfigLoaderService.logger = new common_1.Logger(ConfigLoaderService.name);
90
+ //# sourceMappingURL=config-loader.service.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config-loader.service.js","sourceRoot":"","sources":["../src/config-loader.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,2CAAwC;AACxC,uCAAyB;AACzB,2CAA6B;AAE7B,MAAM,iBAAiB,GAAG,OAAO,CAAC,GAAG,CAAC,iBAAiB,IAAI,cAAc,CAAC;AAC1E,MAAM,iBAAiB,GAAG,OAAO,CAAC,GAAG,CAAC,iBAAiB,IAAI,cAAc,CAAC;AAE1E,MAAa,mBAAmB;IAG9B,MAAM,CAAC,KAAK,CAAC,eAAe;QAC1B,MAAM,YAAY,GAAG,MAAM,mBAAmB,CAAC,sBAAsB,CACnE,iBAAiB,EACjB,QAAQ,CACT,CAAC;QACF,MAAM,aAAa,GAAG,MAAM,mBAAmB,CAAC,sBAAsB,CACpE,iBAAiB,EACjB,QAAQ,CACT,CAAC;QACF,OAAO,EAAE,GAAG,YAAY,EAAE,GAAG,aAAa,EAAE,CAAC;IAC/C,CAAC;IAEO,MAAM,CAAC,kBAAkB,CAAC,QAAgB;QAChD,OAAO,QAAQ,CAAC,OAAO,CAAC,eAAe,EAAE,GAAG,CAAC,CAAC,WAAW,EAAE,CAAC;IAC9D,CAAC;IAEO,MAAM,CAAC,KAAK,CAAC,sBAAsB,CACzC,OAAe,EACf,IAAyB;QAEzB,IAAI,CAAC,EAAE,CAAC,UAAU,CAAC,OAAO,CAAC,EAAE,CAAC;YAC5B,IAAI,CAAC,M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AAI,CAAC,MAAM,CAAC,IAAI,CACd,wBAAwB,GAAG,2CAA2C,IAAI,CAAC,IAAI,EAAE,CAClF,CAAC;oBACJ,CAAC;gBACH,CAAC;YACH,CAAC;YAED,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,UAAU,SAAS,IAAI,IAAI,iBAAiB,OAAO,EAAE,CAAC,CAAC;YAEvE,OAAO,YAAY,CAAC;QACtB,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,iBAAiB,IAAI,WAAW,KAAK,EAAE,CAAC,CAAC;YAC3D,OAAO,EAAE,CAAC;QACZ,CAAC;IACH,CAAC;;AA/DH,kDAgEC;AA/DQ,0BAAM,GAAG,IAAI,eAAM,CAAC,mBAAmB,CAAC,IAAI,CAAC,CAAC"}
@@ -0,0 +1,3 @@
1
+ export { ConfigLoaderService } from './config-loader.service';
2
+ export { ConfigLoaderModule, type ConfigLoaderModuleOptions } from './config-loader.module';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,yBAAyB,CAAC;AAC9D,OAAO,EAAE,kBAAkB,EAAE,KAAK,yBAAyB,EAAE,MAAM,wBAAwB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ConfigLoaderModule = exports.ConfigLoaderService = void 0;
4
+ var config_loader_service_1 = require("./config-loader.service");
5
+ Object.defineProperty(exports, "ConfigLoaderService", { enumerable: true, get: function () { return config_loader_service_1.ConfigLoaderService; } });
6
+ var config_loader_module_1 = require("./config-loader.module");
7
+ Object.defineProperty(exports, "ConfigLoaderModule", { enumerable: true, get: function () { return config_loader_module_1.ConfigLoaderModule; } });
8
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,iEAA8D;AAArD,4HAAA,mBAAmB,OAAA;AAC5B,+DAA4F;AAAnF,0HAAA,kBAAkB,OAAA"}
package/package.json ADDED
@@ -0,0 +1,55 @@
1
+ {
2
+ "name": "@sprout-idws/sprout-config-loader",
3
+ "version": "1.0.0",
4
+ "description": "Reusable NestJS config loader: loads config and secret files from directories into process.env",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "scripts": {
8
+ "build": "tsc",
9
+ "dev": "tsc --watch",
10
+ "clean": "rm -rf dist tsconfig.tsbuildinfo",
11
+ "prepublishOnly": "npm run clean && npm run build",
12
+ "lint": "biome lint .",
13
+ "lint:fix": "biome lint --write .",
14
+ "format": "biome format --write .",
15
+ "format:check": "biome format .",
16
+ "check": "biome check .",
17
+ "check:fix": "biome check --write .",
18
+ "publish:npm": "npm publish"
19
+ },
20
+ "peerDependencies": {
21
+ "@nestjs/common": ">=10.0.0",
22
+ "@nestjs/config": ">=3.0.0"
23
+ },
24
+ "devDependencies": {
25
+ "@biomejs/biome": "^2.4.4",
26
+ "@sprout-idws/typescript-config": "^1.0.0",
27
+ "@nestjs/common": "^11.1.12",
28
+ "@nestjs/config": "^4.0.2",
29
+ "typescript": "^5.9.3"
30
+ },
31
+ "files": [
32
+ "dist/**/*"
33
+ ],
34
+ "keywords": [
35
+ "nestjs",
36
+ "config",
37
+ "config-loader",
38
+ "secrets",
39
+ "env"
40
+ ],
41
+ "author": "Sprout",
42
+ "license": "MIT",
43
+ "publishConfig": {
44
+ "access": "public"
45
+ },
46
+ "repository": {
47
+ "type": "git",
48
+ "url": "https://github.com/sprout-libs/sprout-typescript-backend.git",
49
+ "directory": "packages/sprout-config-loader"
50
+ },
51
+ "homepage": "https://github.com/sprout-libs/sprout-typescript-backend#readme",
52
+ "bugs": {
53
+ "url": "https://github.com/sprout-libs/sprout-typescript-backend/issues"
54
+ }
55
+ }