Skip to content

Commit

Permalink
CONFIG_PATH, remove dist
Browse files Browse the repository at this point in the history
  • Loading branch information
fergusean committed Mar 20, 2024
1 parent 517e6c5 commit d649e58
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 52 deletions.
4 changes: 0 additions & 4 deletions dist/index.d.ts

This file was deleted.

35 changes: 0 additions & 35 deletions dist/index.js

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
8 changes: 4 additions & 4 deletions src/cli.program.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { generateKeyPairSync } from 'crypto';
import { existsSync } from 'fs';

export function generateConfigKeyPair() {
const { privateKey, publicKey } = generateKeyPairSync('rsa', {
Expand All @@ -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));
}
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -27,7 +27,7 @@ export function loadConfig<T extends ConfigData>(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);
Expand Down
8 changes: 2 additions & 6 deletions src/reader.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import { readFileSync } from 'fs';
import { parse } from 'path';

import { getPath } from './helpers';
import { ConfigData } from './types';

export function readConfigFile<T extends Record<string, string>>(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)) {
Expand All @@ -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);
}

Expand Down

0 comments on commit d649e58

Please sign in to comment.