diff --git a/dist/index.d.ts b/dist/index.d.ts deleted file mode 100644 index 5e663bf..0000000 --- a/dist/index.d.ts +++ /dev/null @@ -1,4 +0,0 @@ -import { Decryptor } from './crypto'; -import { ConfigData, LoadOptions } from './types'; -export declare function decryptConfig(decryptor: Decryptor, data: ConfigData): ConfigData; -export declare function loadConfig(options?: LoadOptions): T; diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index 6c96ec4..0000000 --- a/dist/index.js +++ /dev/null @@ -1,35 +0,0 @@ -'use strict'; -Object.defineProperty(exports, '__esModule', { value: true }); -exports.loadConfig = exports.decryptConfig = void 0; -const dotenv_1 = require('dotenv'); -const fs_1 = require('fs'); -const crypto_1 = require('./crypto'); -const reader_1 = require('./reader'); -const types_1 = require('./types'); -function decryptConfig(decryptor, data) { - const decrypted = {}; - for (const [key, value] of Object.entries(data)) { - decrypted[key] = decryptor.decryptValueIfEncrypted(value); - } - return decrypted; -} -exports.decryptConfig = decryptConfig; -function loadConfig(options) { - options = { ...types_1.DefaultLoadOptions, ...options }; - const files = Array.isArray(options.file) ? options.file : [options.file]; - if (!options.key) { - throw new Error('Decryption key is not set'); - } - const decryptor = new crypto_1.Decryptor(options.key); - const config = {}; - for (const file of files) { - if ((0, fs_1.existsSync)(file)) { - const decryptedContent = (0, reader_1.loadAndTransformContent)(file, data => decryptConfig(decryptor, data)); - const fileConfig = (0, dotenv_1.parse)(decryptedContent); - Object.assign(config, fileConfig); - } - } - Object.assign(config, process.env); - return config; -} -exports.loadConfig = loadConfig; diff --git a/package.json b/package.json index a53f97c..7f4d43f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@signal24/config", - "version": "1.4.0", + "version": "1.5.0", "description": "Runtime configuration encryption helpers", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/src/cli.program.ts b/src/cli.program.ts index e8cbedf..6e62b67 100644 --- a/src/cli.program.ts +++ b/src/cli.program.ts @@ -1,8 +1,8 @@ import { Command } from 'commander'; -import { existsSync, writeFileSync } from 'fs'; +import { writeFileSync } from 'fs'; import { Decryptor, Encryptor } from './crypto'; -import { generateConfigKeyPair } from './helpers'; +import { fileExists, generateConfigKeyPair } from './helpers'; import { keyMatches, loadAndTransformContent } from './reader'; import { ConfigData } from './types'; @@ -97,7 +97,7 @@ program function verifyFiles(files: string[]) { return files.filter(file => { - if (!existsSync(file)) { + if (!fileExists(file)) { process.stderr.write(`'${file}' does not exist\n`); return false; } @@ -115,7 +115,7 @@ function exportFiles(files: string[], key: string) { const decryptor = new Decryptor(key); for (const file of files) { - if (existsSync(file)) { + if (fileExists(file)) { loadAndTransformContent(file, data => { for (const [key, value] of Object.entries(data)) { result[key] = decryptor.decryptValueIfEncrypted(value); diff --git a/src/helpers.ts b/src/helpers.ts index a49bb08..9081b80 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -1,4 +1,5 @@ import { generateKeyPairSync } from 'crypto'; +import { existsSync } from 'fs'; export function generateConfigKeyPair() { const { privateKey, publicKey } = generateKeyPairSync('rsa', { @@ -18,3 +19,14 @@ export function generateConfigKeyPair() { publicKey: publicKey.toString('base64').replace(/=+$/, '') }; } + +export function getPath(path: string) { + if (process.env.CONFIG_PATH) { + return `${process.env.CONFIG_PATH}/${path}`; + } + return path; +} + +export function fileExists(path: string) { + return existsSync(getPath(path)); +} diff --git a/src/index.ts b/src/index.ts index a8d0cee..ab6351a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,7 @@ import { parse } from 'dotenv'; -import { existsSync } from 'fs'; import { Decryptor } from './crypto'; +import { fileExists } from './helpers'; import { loadAndTransformContent } from './reader'; import { ConfigData, DefaultLoadOptions, LoadOptions } from './types'; @@ -27,7 +27,7 @@ export function loadConfig(options?: LoadOptions): T { const config: ConfigData = {}; for (const file of files) { - if (existsSync(file)) { + if (fileExists(file)) { const decryptedContent = loadAndTransformContent(file, data => decryptConfig(decryptor, data)); const fileConfig = parse(decryptedContent); Object.assign(config, fileConfig); diff --git a/src/reader.ts b/src/reader.ts index 54dc298..0ad068d 100644 --- a/src/reader.ts +++ b/src/reader.ts @@ -1,12 +1,8 @@ import { readFileSync } from 'fs'; -import { parse } from 'path'; +import { getPath } from './helpers'; import { ConfigData } from './types'; -export function readConfigFile>(path: string): T { - return parse(readFileSync(path, 'utf8')) as any; -} - type MatchType = string | RegExp; export function keyMatches(key: string, match: MatchType | MatchType[]): boolean { if (Array.isArray(match)) { @@ -20,7 +16,7 @@ export function keyMatches(key: string, match: MatchType | MatchType[]): boolean } export function loadAndTransformContent(path: string, transform: (data: ConfigData) => ConfigData) { - const content = readFileSync(path, 'utf8').replace(/\r\n/, '\n'); + const content = readFileSync(getPath(path), 'utf8').replace(/\r\n/, '\n'); return transformContent(content, transform); }