-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathEnvUtil.js
51 lines (44 loc) · 1.21 KB
/
EnvUtil.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
const fs = require('fs');
const path = require('path');
const { S3 } = require('aws-sdk');
class EnvUtil {
constructor(bucket, key, directory = '.', file = '.env') {
this.bucket = bucket;
this.key = key;
this.directory = directory;
this.file = file;
this.s3 = new S3();
this.writeToFile = this.writeToFile.bind(this);
}
getEnvVariables() {
const params = {
Bucket: this.bucket,
Key: this.key,
};
return new Promise((resolve, reject) => {
this.s3.getObject(params, (err, data) => {
if (err) {
reject(new Error(`There was an error retrieving ${params.Bucket}/${params.key}: ${err}`));
} else {
resolve(data.Body.toString().trim());
}
});
});
}
/* eslint-disable class-methods-use-this */
writeToFile(envVariables) {
if (this.directory !== '.') {
const dir = this.directory;
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
}
const filePath = path.join(this.directory, this.file);
fs.writeFile(filePath, envVariables, (err) => {
if (err) {
throw new Error(`There was an error when writing to .env: ${err}`);
}
});
}
}
module.exports = EnvUtil;