Skip to content

Commit

Permalink
refactor: Apply review comments generated by coderabbitai ^_^
Browse files Browse the repository at this point in the history
  • Loading branch information
macalbert committed Oct 16, 2024
1 parent f6bf230 commit 4b18ffc
Show file tree
Hide file tree
Showing 5 changed files with 386 additions and 400 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"@secretlint/secretlint-rule-preset-recommend": "^8.2.4",
"@types/node": "^22.5.5",
"commander": "^12.1.0",
"dotenv": "^16.4.5",
"picocolors": "^1.1.0"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion src/cli/cliRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ export async function cliRunner() {
}

cliRunner().catch((error) => {
console.error('Error in CLI Runner:', error);
console.error('🚨 Uh-oh! Looks like Mario fell into the wrong pipe! 🍄💥');
});
31 changes: 19 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as fs from 'node:fs';
import { GetParameterCommand, SSM } from '@aws-sdk/client-ssm';
import * as dotenv from 'dotenv';

const ssm = new SSM({});

Expand All @@ -10,26 +11,27 @@ export async function run(mapPath: string, envFilePath: string) {
const updatedEnvVariables = await fetchAndUpdateEnvVariables(paramMap, existingEnvVariables);

writeEnvFile(envFilePath, updatedEnvVariables);
console.log(`.env file generated at ${envFilePath}`);
console.log(`Environment File generated at '${envFilePath}'`);
}

function loadParamMap(mapPath: string): Record<string, string> {
const content = fs.readFileSync(mapPath, 'utf-8');
return JSON.parse(content);
try {
return JSON.parse(content);
} catch (error) {
console.error(`Error parsing JSON from ${mapPath}`);
throw new Error(`Invalid JSON in parameter map file: ${mapPath}`);
}
}

function loadExistingEnvVariables(envFilePath: string): Record<string, string> {
const envVariables: Record<string, string> = {};

if (!fs.existsSync(envFilePath)) return envVariables;

const existingEnvContent = fs.readFileSync(envFilePath, 'utf-8');
const lines = existingEnvContent.split('\n');
for (const line of lines) {
const [key, value] = line.split('=');
if (key && value) {
envVariables[key] = value;
}
}
const parsedEnv = dotenv.parse(existingEnvContent);
Object.assign(envVariables, parsedEnv);

return envVariables;
}
Expand All @@ -39,6 +41,7 @@ async function fetchAndUpdateEnvVariables(
existingEnvVariables: Record<string, string>,
): Promise<Record<string, string>> {
console.log('Fetching parameters...');
const errors: string[] = [];

for (const [envVar, ssmName] of Object.entries(paramMap)) {
try {
Expand All @@ -47,14 +50,18 @@ async function fetchAndUpdateEnvVariables(
existingEnvVariables[envVar] = value;
console.log(`${envVar}=${value}`);
} else {
console.error(`Warning: No value found for ${ssmName}`);
console.error(`Warning: No value found for: '${ssmName}'`);
}
} catch (error) {
console.error(`Error fetching parameter ${ssmName}: ${error}`);
throw new Error(`ParameterNotFound: ${ssmName}`);
console.error(`Error fetching parameter: '${ssmName}'`);
errors.push(`ParameterNotFound: ${ssmName}`);
}
}

if (errors.length > 0) {
throw new Error(`Some parameters could not be fetched:\n${errors.join('\n')}`);
}

return existingEnvVariables;
}

Expand Down
8 changes: 2 additions & 6 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,7 @@ describe('Envilder CLI', () => {
const mockMapPath = './tests/param_map.json';
const mockEnvFilePath = './tests/.env.test';

const existingEnvContent = `
EXISTING_VAR=existingValue
`;
const existingEnvContent = 'EXISTING_VAR=existingValue';
fs.writeFileSync(mockEnvFilePath, existingEnvContent);
const paramMapContent = {
NEXT_PUBLIC_CREDENTIAL_EMAIL: '/path/to/ssm/email',
Expand All @@ -105,9 +103,7 @@ describe('Envilder CLI', () => {
// Arrange
const mockMapPath = './tests/param_map.json';
const mockEnvFilePath = './tests/.env.test';
const existingEnvContent = `
[email protected]
`;
const existingEnvContent = '[email protected]';
fs.writeFileSync(mockEnvFilePath, existingEnvContent);
const paramMapContent = {
NEXT_PUBLIC_CREDENTIAL_EMAIL: '/path/to/ssm/email',
Expand Down
Loading

0 comments on commit 4b18ffc

Please sign in to comment.