envilder 0.3.0 → 0.3.2

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.
@@ -1 +0,0 @@
1
- {"version":3,"file":"cliRunner.d.ts","sourceRoot":"","sources":["../../src/cli/cliRunner.ts"],"names":[],"mappings":";AAKA;;;;;;GAMG;AACH,wBAAsB,SAAS,kBAmB9B"}
@@ -1,41 +0,0 @@
1
- #!/usr/bin/env node
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- import { Command } from 'commander';
12
- import { run } from '../index.js';
13
- /**
14
- * Parses CLI arguments and runs the environment file generator.
15
- *
16
- * Expects `--map` and `--envfile` options to be provided, with an optional `--profile` for AWS CLI profile selection. Invokes the main process to generate a `.env` file from AWS SSM parameters based on the provided mapping.
17
- *
18
- * @throws {Error} If either `--map` or `--envfile` arguments are missing.
19
- */
20
- export function cliRunner() {
21
- return __awaiter(this, void 0, void 0, function* () {
22
- const program = new Command();
23
- program
24
- .name('envilder')
25
- .description('A CLI tool to generate .env files from AWS SSM parameters')
26
- .version('0.1.0')
27
- .requiredOption('--map <path>', 'Path to the JSON file with environment variable mapping')
28
- .requiredOption('--envfile <path>', 'Path to the .env file to be generated')
29
- .option('--profile <name>', 'AWS CLI profile to use');
30
- yield program.parseAsync(process.argv);
31
- const options = program.opts();
32
- if (!options.map || !options.envfile) {
33
- throw new Error('Missing required arguments: --map and --envfile');
34
- }
35
- yield run(options.map, options.envfile, options.profile);
36
- });
37
- }
38
- cliRunner().catch((error) => {
39
- console.error('🚨 Uh-oh! Looks like Mario fell into the wrong pipe! 🍄💥');
40
- });
41
- //# sourceMappingURL=cliRunner.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"cliRunner.js","sourceRoot":"","sources":["../../src/cli/cliRunner.ts"],"names":[],"mappings":";;;;;;;;;;AAEA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,GAAG,EAAE,MAAM,aAAa,CAAC;AAElC;;;;;;GAMG;AACH,MAAM,UAAgB,SAAS;;QAC7B,MAAM,OAAO,GAAG,IAAI,OAAO,EAAE,CAAC;QAE9B,OAAO;aACJ,IAAI,CAAC,UAAU,CAAC;aAChB,WAAW,CAAC,2DAA2D,CAAC;aACxE,OAAO,CAAC,OAAO,CAAC;aAChB,cAAc,CAAC,cAAc,EAAE,yDAAyD,CAAC;aACzF,cAAc,CAAC,kBAAkB,EAAE,uCAAuC,CAAC;aAC3E,MAAM,CAAC,kBAAkB,EAAE,wBAAwB,CAAC,CAAC;QAExD,MAAM,OAAO,CAAC,UAAU,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;QACvC,MAAM,OAAO,GAAG,OAAO,CAAC,IAAI,EAAE,CAAC;QAE/B,IAAI,CAAC,OAAO,CAAC,GAAG,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE,CAAC;YACrC,MAAM,IAAI,KAAK,CAAC,iDAAiD,CAAC,CAAC;QACrE,CAAC;QAED,MAAM,GAAG,CAAC,OAAO,CAAC,GAAG,EAAE,OAAO,CAAC,OAAO,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC;IAC3D,CAAC;CAAA;AAED,SAAS,EAAE,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;IAC1B,OAAO,CAAC,KAAK,CAAC,2DAA2D,CAAC,CAAC;AAC7E,CAAC,CAAC,CAAC"}
package/lib/index.d.ts DELETED
@@ -1,11 +0,0 @@
1
- /**
2
- * Orchestrates the process of fetching environment variable values from AWS SSM Parameter Store and writing them to a local environment file.
3
- *
4
- * Loads a parameter mapping from a JSON file, retrieves existing environment variables, fetches updated values from SSM (optionally using a specified AWS profile), merges them, and writes the result to the specified environment file.
5
- *
6
- * @param mapPath - Path to the JSON file mapping environment variable names to SSM parameter names.
7
- * @param envFilePath - Path to the local environment file to read and update.
8
- * @param profile - Optional AWS profile name to use for credentials.
9
- */
10
- export declare function run(mapPath: string, envFilePath: string, profile?: string): Promise<void>;
11
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAKA;;;;;;;;GAQG;AACH,wBAAsB,GAAG,CAAC,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,iBAY/E"}
package/lib/index.js DELETED
@@ -1,116 +0,0 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
- import * as fs from 'node:fs';
11
- import { GetParameterCommand, SSM } from '@aws-sdk/client-ssm';
12
- import { fromIni } from '@aws-sdk/credential-providers';
13
- import * as dotenv from 'dotenv';
14
- /**
15
- * Orchestrates the process of fetching environment variable values from AWS SSM Parameter Store and writing them to a local environment file.
16
- *
17
- * Loads a parameter mapping from a JSON file, retrieves existing environment variables, fetches updated values from SSM (optionally using a specified AWS profile), merges them, and writes the result to the specified environment file.
18
- *
19
- * @param mapPath - Path to the JSON file mapping environment variable names to SSM parameter names.
20
- * @param envFilePath - Path to the local environment file to read and update.
21
- * @param profile - Optional AWS profile name to use for credentials.
22
- */
23
- export function run(mapPath, envFilePath, profile) {
24
- return __awaiter(this, void 0, void 0, function* () {
25
- const defaultAwsConfig = {};
26
- const ssmClientConfig = profile ? { credentials: fromIni({ profile }) } : defaultAwsConfig;
27
- const ssm = new SSM(ssmClientConfig);
28
- const paramMap = loadParamMap(mapPath);
29
- const existingEnvVariables = loadExistingEnvVariables(envFilePath);
30
- const updatedEnvVariables = yield fetchAndUpdateEnvVariables(paramMap, existingEnvVariables, ssm);
31
- writeEnvFile(envFilePath, updatedEnvVariables);
32
- console.log(`Environment File generated at '${envFilePath}'`);
33
- });
34
- }
35
- function loadParamMap(mapPath) {
36
- const content = fs.readFileSync(mapPath, 'utf-8');
37
- try {
38
- return JSON.parse(content);
39
- }
40
- catch (error) {
41
- console.error(`Error parsing JSON from ${mapPath}`);
42
- throw new Error(`Invalid JSON in parameter map file: ${mapPath}`);
43
- }
44
- }
45
- function loadExistingEnvVariables(envFilePath) {
46
- const envVariables = {};
47
- if (!fs.existsSync(envFilePath))
48
- return envVariables;
49
- const existingEnvContent = fs.readFileSync(envFilePath, 'utf-8');
50
- const parsedEnv = dotenv.parse(existingEnvContent);
51
- Object.assign(envVariables, parsedEnv);
52
- return envVariables;
53
- }
54
- /**
55
- * Fetches parameter values from AWS SSM for each environment variable in the map and updates the existing environment variables record.
56
- *
57
- * For each mapping, retrieves the corresponding SSM parameter value and updates the environment variable if found. Logs masked values and warnings for missing parameters. Throws an error if any parameters fail to fetch.
58
- *
59
- * @param paramMap - Mapping of environment variable names to SSM parameter names.
60
- * @param existingEnvVariables - Current environment variables to be updated.
61
- * @param ssm - AWS SSM client instance used for fetching parameters.
62
- * @returns The updated environment variables record.
63
- *
64
- * @throws {Error} If any SSM parameters cannot be fetched.
65
- */
66
- function fetchAndUpdateEnvVariables(paramMap, existingEnvVariables, ssm) {
67
- return __awaiter(this, void 0, void 0, function* () {
68
- const errors = [];
69
- for (const [envVar, ssmName] of Object.entries(paramMap)) {
70
- try {
71
- const value = yield fetchSSMParameter(ssmName, ssm);
72
- if (value) {
73
- existingEnvVariables[envVar] = value;
74
- console.log(`${envVar}=${value.length > 3 ? '*'.repeat(value.length - 3) + value.slice(-3) : '*'.repeat(value.length)}`);
75
- }
76
- else {
77
- console.error(`Warning: No value found for: '${ssmName}'`);
78
- }
79
- }
80
- catch (error) {
81
- console.error(`Error fetching parameter: '${ssmName}'`);
82
- errors.push(`ParameterNotFound: ${ssmName}`);
83
- }
84
- }
85
- if (errors.length > 0) {
86
- throw new Error(`Some parameters could not be fetched:\n${errors.join('\n')}`);
87
- }
88
- return existingEnvVariables;
89
- });
90
- }
91
- /**
92
- * Retrieves the value of a parameter from AWS SSM Parameter Store with decryption enabled.
93
- *
94
- * @param ssmName - The name of the SSM parameter to retrieve.
95
- * @returns The decrypted parameter value if found, or undefined if the parameter does not exist.
96
- */
97
- function fetchSSMParameter(ssmName, ssm) {
98
- return __awaiter(this, void 0, void 0, function* () {
99
- const command = new GetParameterCommand({
100
- Name: ssmName,
101
- WithDecryption: true,
102
- });
103
- const { Parameter } = yield ssm.send(command);
104
- return Parameter === null || Parameter === void 0 ? void 0 : Parameter.Value;
105
- });
106
- }
107
- function writeEnvFile(envFilePath, envVariables) {
108
- const envContent = Object.entries(envVariables)
109
- .map(([key, value]) => {
110
- const escapedValue = value.replace(/(\n|\r|\n\r)/g, '\\n').replace(/"/g, '\\"');
111
- return `${key}=${escapedValue}`;
112
- })
113
- .join('\n');
114
- fs.writeFileSync(envFilePath, envContent);
115
- }
116
- //# sourceMappingURL=index.js.map
package/lib/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,KAAK,EAAE,MAAM,SAAS,CAAC;AAC9B,OAAO,EAAE,mBAAmB,EAAE,GAAG,EAAE,MAAM,qBAAqB,CAAC;AAC/D,OAAO,EAA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