envinos
Version:
Sync your local environment variables from external source shared among developers
46 lines (45 loc) • 1.98 kB
JavaScript
import { EOL } from 'node:os';
import fs from 'fs-extra';
import { GetSecretValueCommand } from '@aws-sdk/client-secrets-manager';
import dotenv from 'dotenv';
import { diff } from 'json-diff';
export const generateEnvFile = async (clientConfiguration, secretsManagerClient, secretIndex) => {
const secret = clientConfiguration.secrets[secretIndex];
const getSecretValueCommand = new GetSecretValueCommand({ SecretId: secret.key });
await fs.ensureFile(secret.filePath);
const [dotEnvFileContent, getSecretValueResponse] = await Promise.all([
fs.readFile(secret.filePath, 'utf8'),
secretsManagerClient.send(getSecretValueCommand),
]);
const secretValue = getSecretValueResponse.SecretString;
if (!secretValue) {
return;
}
const parsedSecretValue = JSON.parse(secretValue);
const parsedDotEnvFileContent = dotenv.parse(dotEnvFileContent);
const envDifference = diff(parsedDotEnvFileContent, parsedSecretValue);
if (!envDifference) {
return;
}
for (const diffKey in envDifference) {
if (diffKey.endsWith('__deleted')) {
const envName = diffKey.replace('__deleted', '');
delete parsedDotEnvFileContent[envName];
}
else if (diffKey.endsWith('__added')) {
const envName = diffKey.replace('__added', '');
const newEnvValue = envDifference[diffKey];
parsedDotEnvFileContent[envName] = newEnvValue;
}
else {
const newEnvValue = envDifference[diffKey]['__new'];
if (clientConfiguration.skipKeyword === undefined || newEnvValue !== clientConfiguration.skipKeyword) {
parsedDotEnvFileContent[diffKey] = newEnvValue;
}
}
}
const newEnvFileContent = Object.entries(parsedDotEnvFileContent)
.map((envEntry) => envEntry.join('='))
.join(EOL) + EOL;
await fs.outputFile(secret.filePath, newEnvFileContent, 'utf8');
};