Skip to content

Commit

Permalink
sh/shenv, auto-construct file list for env
Browse files Browse the repository at this point in the history
  • Loading branch information
fergusean committed Mar 20, 2024
1 parent d49acb1 commit 3e264fe
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 69 deletions.
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@signal24/config",
"version": "1.2.0",
"version": "1.3.0",
"description": "Runtime configuration encryption helpers",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand All @@ -22,9 +22,9 @@
},
"devDependencies": {
"@types/jest": "^29.5.12",
"@types/node": "^20.11.24",
"@typescript-eslint/eslint-plugin": "^7.1.1",
"@typescript-eslint/parser": "^7.1.1",
"@types/node": "^20.11.30",
"@typescript-eslint/eslint-plugin": "^7.3.1",
"@typescript-eslint/parser": "^7.3.1",
"buildkite-test-collector": "^1.7.0",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
Expand All @@ -36,8 +36,8 @@
"ts-jest": "^29.1.2",
"ts-node": "^10.9.2",
"tslib": "^2.6.2",
"type-fest": "^4.11.1",
"typescript": "^5.3.3"
"type-fest": "^4.13.0",
"typescript": "^5.4.2"
},
"engines": {
"node": ">=16.0.0"
Expand Down
44 changes: 44 additions & 0 deletions src/cli.program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,28 @@ import { ConfigData } from './types';

export const program = new Command();

program
.command('sh [files...]')
.description('Output export statements to set variables from the specified .env files')
.option('-k, --key <key>', 'The decryption key (defaults to the value of the CONFIG_DECRYPTION_KEY environment variable)')
.action((files, options) => {
files = files.length ? files : ['.env'];
files = verifyFiles(files);

const key = options.key ?? process.env.CONFIG_DECRYPTION_KEY;
exportFiles(files, key);
});

program
.command('shenv [environment]')
.description('Output export statements to set variables from the specified environment')
.option('-k, --key <key>', 'The decryption key (defaults to the value of the CONFIG_DECRYPTION_KEY environment variable)')
.action((env, options) => {
const files = ['.env', '.env.local', `.env.${env}`, `.env.${env}.local`];
const key = options.key ?? process.env.CONFIG_DECRYPTION_KEY;
exportFiles(files, key);
});

program
.command('encrypt [files...]')
.description('Encrypts the specified .env files')
Expand Down Expand Up @@ -87,3 +109,25 @@ function transformFile(path: string, transform: (data: ConfigData) => ConfigData
const updatedContent = loadAndTransformContent(path, transform);
writeFileSync(path, updatedContent);
}

function exportFiles(files: string[], key: string) {
const result: ConfigData = {};
const decryptor = new Decryptor(key);

for (const file of files) {
if (existsSync(file)) {
loadAndTransformContent(file, data => {
for (const [key, value] of Object.entries(data)) {
result[key] = decryptor.decryptValueIfEncrypted(value);
}
return data;
});
}
}

for (const [key, value] of Object.entries(result)) {
if (!process.env[key]) {
console.log(`export ${key}="${value}"`);
}
}
}
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ export function decryptConfig(decryptor: Decryptor, data: ConfigData): ConfigDat

export function loadConfig<T extends ConfigData>(options?: LoadOptions): T {
options = { ...DefaultLoadOptions, ...options };

if (!options.file) {
const envFiles = options.env ? [`.env.${options.env}`, `.env.${options.env}.local`] : [];
options.file = ['.env', '.env.local', ...envFiles];
}

const files = Array.isArray(options.file) ? options.file : ([options.file] as string[]);

const decryptor = new Decryptor(options.key);
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface DecryptOptions {

export interface LoadOptions extends DecryptOptions {
file?: string | string[];
env?: string;
}

export interface EncryptOptions {
Expand All @@ -14,7 +15,6 @@ export interface EncryptOptions {
}

export const DefaultLoadOptions: Partial<LoadOptions> = {
file: '.env',
key: process.env.CONFIG_DECRYPTION_KEY
};

Expand Down
124 changes: 62 additions & 62 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -985,10 +985,10 @@
resolved "https://registry.yarnpkg.com/@types/node/-/node-18.15.11.tgz#b3b790f09cb1696cffcec605de025b088fa4225f"
integrity sha512-E5Kwq2n4SbMzQOn6wnmBjuK9ouqlURrcZDVfbo9ftDDTFt3nk7ZKK4GMOzoYgnpQJKcxwQw+lGaBvvlMo0qN/Q==

"@types/node@^20.11.24":
version "20.11.24"
resolved "https://registry.yarnpkg.com/@types/node/-/node-20.11.24.tgz#cc207511104694e84e9fb17f9a0c4c42d4517792"
integrity sha512-Kza43ewS3xoLgCEpQrsT+xRo/EJej1y0kVYGiLFE1NEODXGzTfwiC6tXTLMQskn1X4/Rjlh0MQUvx9W+L9long==
"@types/node@^20.11.30":
version "20.11.30"
resolved "https://registry.yarnpkg.com/@types/node/-/node-20.11.30.tgz#9c33467fc23167a347e73834f788f4b9f399d66f"
integrity sha512-dHM6ZxwlmuZaRmUPfv1p+KrdD1Dci04FbdEm/9wEMouFqxYoFl5aMkt0VMAUtYRQDyYvD41WJLukhq/ha3YuTw==
dependencies:
undici-types "~5.26.4"

Expand All @@ -1014,90 +1014,90 @@
dependencies:
"@types/yargs-parser" "*"

"@typescript-eslint/eslint-plugin@^7.1.1":
version "7.1.1"
resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-7.1.1.tgz#dd71fc5c7ecec745ca26ece506d84d203a205c0e"
integrity sha512-zioDz623d0RHNhvx0eesUmGfIjzrk18nSBC8xewepKXbBvN/7c1qImV7Hg8TI1URTxKax7/zxfxj3Uph8Chcuw==
"@typescript-eslint/eslint-plugin@^7.3.1":
version "7.3.1"
resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-7.3.1.tgz#0d8f38a6c8a1802139e62184ee7a68ed024f30a1"
integrity sha512-STEDMVQGww5lhCuNXVSQfbfuNII5E08QWkvAw5Qwf+bj2WT+JkG1uc+5/vXA3AOYMDHVOSpL+9rcbEUiHIm2dw==
dependencies:
"@eslint-community/regexpp" "^4.5.1"
"@typescript-eslint/scope-manager" "7.1.1"
"@typescript-eslint/type-utils" "7.1.1"
"@typescript-eslint/utils" "7.1.1"
"@typescript-eslint/visitor-keys" "7.1.1"
"@typescript-eslint/scope-manager" "7.3.1"
"@typescript-eslint/type-utils" "7.3.1"
"@typescript-eslint/utils" "7.3.1"
"@typescript-eslint/visitor-keys" "7.3.1"
debug "^4.3.4"
graphemer "^1.4.0"
ignore "^5.2.4"
natural-compare "^1.4.0"
semver "^7.5.4"
ts-api-utils "^1.0.1"

"@typescript-eslint/parser@^7.1.1":
version "7.1.1"
resolved "https://registry.yarnpkg.com/@typescript-eslint/parser/-/parser-7.1.1.tgz#6a9d0a5c9ccdf5dbd3cb8c949728c64e24e07d1f"
integrity sha512-ZWUFyL0z04R1nAEgr9e79YtV5LbafdOtN7yapNbn1ansMyaegl2D4bL7vHoJ4HPSc4CaLwuCVas8CVuneKzplQ==
"@typescript-eslint/parser@^7.3.1":
version "7.3.1"
resolved "https://registry.yarnpkg.com/@typescript-eslint/parser/-/parser-7.3.1.tgz#c4ba7dc2744318a5e4506596cbc3a0086255c526"
integrity sha512-Rq49+pq7viTRCH48XAbTA+wdLRrB/3sRq4Lpk0oGDm0VmnjBrAOVXH/Laalmwsv2VpekiEfVFwJYVk6/e8uvQw==
dependencies:
"@typescript-eslint/scope-manager" "7.1.1"
"@typescript-eslint/types" "7.1.1"
"@typescript-eslint/typescript-estree" "7.1.1"
"@typescript-eslint/visitor-keys" "7.1.1"
"@typescript-eslint/scope-manager" "7.3.1"
"@typescript-eslint/types" "7.3.1"
"@typescript-eslint/typescript-estree" "7.3.1"
"@typescript-eslint/visitor-keys" "7.3.1"
debug "^4.3.4"

"@typescript-eslint/scope-manager@7.1.1":
version "7.1.1"
resolved "https://registry.yarnpkg.com/@typescript-eslint/scope-manager/-/scope-manager-7.1.1.tgz#9e301803ff8e21a74f50c6f89a4baccad9a48f93"
integrity sha512-cirZpA8bJMRb4WZ+rO6+mnOJrGFDd38WoXCEI57+CYBqta8Yc8aJym2i7vyqLL1vVYljgw0X27axkUXz32T8TA==
"@typescript-eslint/scope-manager@7.3.1":
version "7.3.1"
resolved "https://registry.yarnpkg.com/@typescript-eslint/scope-manager/-/scope-manager-7.3.1.tgz#73fd0cb4211a7be23e49e5b6efec8820caa6ec36"
integrity sha512-fVS6fPxldsKY2nFvyT7IP78UO1/I2huG+AYu5AMjCT9wtl6JFiDnsv4uad4jQ0GTFzcUV5HShVeN96/17bTBag==
dependencies:
"@typescript-eslint/types" "7.1.1"
"@typescript-eslint/visitor-keys" "7.1.1"
"@typescript-eslint/types" "7.3.1"
"@typescript-eslint/visitor-keys" "7.3.1"

"@typescript-eslint/type-utils@7.1.1":
version "7.1.1"
resolved "https://registry.yarnpkg.com/@typescript-eslint/type-utils/-/type-utils-7.1.1.tgz#aee820d5bedd39b83c18585a526cc520ddb7a226"
integrity sha512-5r4RKze6XHEEhlZnJtR3GYeCh1IueUHdbrukV2KSlLXaTjuSfeVF8mZUVPLovidCuZfbVjfhi4c0DNSa/Rdg5g==
"@typescript-eslint/type-utils@7.3.1":
version "7.3.1"
resolved "https://registry.yarnpkg.com/@typescript-eslint/type-utils/-/type-utils-7.3.1.tgz#cbf90d3d7e788466aa8a5c0ab3f46103f098aa0d"
integrity sha512-iFhaysxFsMDQlzJn+vr3OrxN8NmdQkHks4WaqD4QBnt5hsq234wcYdyQ9uquzJJIDAj5W4wQne3yEsYA6OmXGw==
dependencies:
"@typescript-eslint/typescript-estree" "7.1.1"
"@typescript-eslint/utils" "7.1.1"
"@typescript-eslint/typescript-estree" "7.3.1"
"@typescript-eslint/utils" "7.3.1"
debug "^4.3.4"
ts-api-utils "^1.0.1"

"@typescript-eslint/types@7.1.1":
version "7.1.1"
resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-7.1.1.tgz#ca33ba7cf58224fb46a84fea62593c2c53cd795f"
integrity sha512-KhewzrlRMrgeKm1U9bh2z5aoL4s7K3tK5DwHDn8MHv0yQfWFz/0ZR6trrIHHa5CsF83j/GgHqzdbzCXJ3crx0Q==
"@typescript-eslint/types@7.3.1":
version "7.3.1"
resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-7.3.1.tgz#ae104de8efa4227a462c0874d856602c5994413c"
integrity sha512-2tUf3uWggBDl4S4183nivWQ2HqceOZh1U4hhu4p1tPiIJoRRXrab7Y+Y0p+dozYwZVvLPRI6r5wKe9kToF9FIw==

"@typescript-eslint/typescript-estree@7.1.1":
version "7.1.1"
resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-7.1.1.tgz#09c54af0151a1b05d0875c0fc7fe2ec7a2476ece"
integrity sha512-9ZOncVSfr+sMXVxxca2OJOPagRwT0u/UHikM2Rd6L/aB+kL/QAuTnsv6MeXtjzCJYb8PzrXarypSGIPx3Jemxw==
"@typescript-eslint/typescript-estree@7.3.1":
version "7.3.1"
resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-7.3.1.tgz#598848195fad34c7aa73f548bd00a4d4e5f5e2bb"
integrity sha512-tLpuqM46LVkduWP7JO7yVoWshpJuJzxDOPYIVWUUZbW+4dBpgGeUdl/fQkhuV0A8eGnphYw3pp8d2EnvPOfxmQ==
dependencies:
"@typescript-eslint/types" "7.1.1"
"@typescript-eslint/visitor-keys" "7.1.1"
"@typescript-eslint/types" "7.3.1"
"@typescript-eslint/visitor-keys" "7.3.1"
debug "^4.3.4"
globby "^11.1.0"
is-glob "^4.0.3"
minimatch "9.0.3"
semver "^7.5.4"
ts-api-utils "^1.0.1"

"@typescript-eslint/utils@7.1.1":
version "7.1.1"
resolved "https://registry.yarnpkg.com/@typescript-eslint/utils/-/utils-7.1.1.tgz#bdeeb789eee4af5d3fb5400a69566d4dbf97ff3b"
integrity sha512-thOXM89xA03xAE0lW7alstvnyoBUbBX38YtY+zAUcpRPcq9EIhXPuJ0YTv948MbzmKh6e1AUszn5cBFK49Umqg==
"@typescript-eslint/utils@7.3.1":
version "7.3.1"
resolved "https://registry.yarnpkg.com/@typescript-eslint/utils/-/utils-7.3.1.tgz#fc28fd508ccf89495012561b7c02a6fdad162460"
integrity sha512-jIERm/6bYQ9HkynYlNZvXpzmXWZGhMbrOvq3jJzOSOlKXsVjrrolzWBjDW6/TvT5Q3WqaN4EkmcfdQwi9tDjBQ==
dependencies:
"@eslint-community/eslint-utils" "^4.4.0"
"@types/json-schema" "^7.0.12"
"@types/semver" "^7.5.0"
"@typescript-eslint/scope-manager" "7.1.1"
"@typescript-eslint/types" "7.1.1"
"@typescript-eslint/typescript-estree" "7.1.1"
"@typescript-eslint/scope-manager" "7.3.1"
"@typescript-eslint/types" "7.3.1"
"@typescript-eslint/typescript-estree" "7.3.1"
semver "^7.5.4"

"@typescript-eslint/visitor-keys@7.1.1":
version "7.1.1"
resolved "https://registry.yarnpkg.com/@typescript-eslint/visitor-keys/-/visitor-keys-7.1.1.tgz#e6538a58c9b157f03bcbb29e3b6a92fe39a6ab0d"
integrity sha512-yTdHDQxY7cSoCcAtiBzVzxleJhkGB9NncSIyMYe2+OGON1ZsP9zOPws/Pqgopa65jvknOjlk/w7ulPlZ78PiLQ==
"@typescript-eslint/visitor-keys@7.3.1":
version "7.3.1"
resolved "https://registry.yarnpkg.com/@typescript-eslint/visitor-keys/-/visitor-keys-7.3.1.tgz#6ddef14a3ce2a79690f01176f5305c34d7b93d8c"
integrity sha512-9RMXwQF8knsZvfv9tdi+4D/j7dMG28X/wMJ8Jj6eOHyHWwDW4ngQJcqEczSsqIKKjFiLFr40Mnr7a5ulDD3vmw==
dependencies:
"@typescript-eslint/types" "7.1.1"
"@typescript-eslint/types" "7.3.1"
eslint-visitor-keys "^3.4.1"

"@ungap/structured-clone@^1.2.0":
Expand Down Expand Up @@ -3283,15 +3283,15 @@ type-fest@^0.21.3:
resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-0.21.3.tgz#d260a24b0198436e133fa26a524a6d65fa3b2e37"
integrity sha512-t0rzBq87m3fVcduHDUFhKmyyX+9eo6WQjZvf51Ea/M0Q7+T374Jp1aUiyUl0GKxp8M/OETVHSDvmkyPgvX+X2w==

type-fest@^4.11.1:
version "4.11.1"
resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-4.11.1.tgz#7de27117459b926cb56922a4fd565423041e06d3"
integrity sha512-MFMf6VkEVZAETidGGSYW2B1MjXbGX+sWIywn2QPEaJ3j08V+MwVRHMXtf2noB8ENJaD0LIun9wh5Z6OPNf1QzQ==
type-fest@^4.13.0:
version "4.13.0"
resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-4.13.0.tgz#b55e877e3d811bb5560a212daded70443c9000c2"
integrity sha512-nKO1N9IFeTec3jnNe/3nZlX+RzwZsvT3c4akWC3IlhYGQbRSPFMBe87vmoaymS3hW2l/rs+4ptDDTxzcbqAcmA==

typescript@^5.3.3:
version "5.3.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.3.3.tgz#b3ce6ba258e72e6305ba66f5c9b452aaee3ffe37"
integrity sha512-pXWcraxM0uxAS+tN0AG/BF2TyqmHO014Z070UsJ+pFvYuRSq8KH8DmWpnbXe0pEPDHXZV3FcAbJkijJ5oNEnWw==
typescript@^5.4.2:
version "5.4.2"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.4.2.tgz#0ae9cebcfae970718474fe0da2c090cad6577372"
integrity sha512-+2/g0Fds1ERlP6JsakQQDXjZdZMM+rqpamFZJEKh4kwTIn3iDkgKtby0CeNd5ATNZ4Ry1ax15TMx0W2V+miizQ==

undici-types@~5.26.4:
version "5.26.5"
Expand Down

0 comments on commit 3e264fe

Please sign in to comment.