-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (53 loc) · 1.33 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const {
GetSecretValueCommand,
SecretsManagerClient,
} = require("@aws-sdk/client-secrets-manager");
const path = require('path');
const fs = require('fs');
const { env: {
region = 'us-east-1',
secretName: SecretId = 'secrets2env/test',
accessKeyId,
secretAccessKey,
}} = process;
// Create a Secrets Manager client
const client = new SecretsManagerClient({
region,
credentials: {
accessKeyId, // Replace with your actual access key ID
secretAccessKey, // Replace with your actual secret access key
}
});
const retrieve = async () => {
const data = await client.send(
new GetSecretValueCommand({
SecretId,
}),
);
let dotEnvContent = '';
return(JSON.parse(data.SecretString));
}
const config = async () => {
const secrets = await retrieve();
Object.entries(secrets).forEach(([attribute, value]) => {
process.env[attribute] = value;
});
}
const createFile = async (args) => {
let file = '.env';
if (args && args.file) {
file = args.file;
}
let dotEnvContent = '';
const secrets = await retrieve();
Object.entries(secrets).forEach(([attribute, value]) => {
dotEnvContent += `${attribute}=${value}\n`;
});
const writePath = __dirname.split('node_modules/')[0]
fs.writeFileSync(`${writePath}/${file}`, dotEnvContent);
}
module.exports = {
retrieve,
config,
createFile,
}