1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | Object.defineProperty(exports, "__esModule", { value: true });
|
11 | exports.EnvFile = void 0;
|
12 | const NewLine_1 = require("../Formats/NewLine");
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | class EnvFile {
|
18 | constructor(basePath) {
|
19 | this.basePath = basePath;
|
20 | this.envContents = new NewLine_1.NewLineFile(this.basePath, '.env');
|
21 | this.exampleEnvContents = new NewLine_1.NewLineFile(this.basePath, '.env.example');
|
22 | }
|
23 | |
24 |
|
25 |
|
26 | set(key, value) {
|
27 | const matchingLine = this.envContents.get().find((line) => line.startsWith(`${key}=`));
|
28 | const newText = `${key}=${value}`;
|
29 | if (matchingLine && newText !== matchingLine) {
|
30 | this.envContents.update(matchingLine, newText);
|
31 | return this;
|
32 | }
|
33 | this.envContents.add(newText);
|
34 | this.exampleEnvContents.add(newText);
|
35 | return this;
|
36 | }
|
37 | |
38 |
|
39 |
|
40 | get() {
|
41 | return this.envContents.get().reduce((result, line) => {
|
42 | const [key, value] = line.split('=');
|
43 | result[key.trim()] = value.trim();
|
44 | return result;
|
45 | }, {});
|
46 | }
|
47 | |
48 |
|
49 |
|
50 | exists() {
|
51 | return this.envContents.exists();
|
52 | }
|
53 | |
54 |
|
55 |
|
56 | unset(key) {
|
57 | const matchingLine = this.envContents.get().find((line) => line.startsWith(`${key}=`));
|
58 | if (matchingLine) {
|
59 | this.envContents.remove(matchingLine);
|
60 | }
|
61 | const exampleFileMatchingLine = this.exampleEnvContents.get().find((line) => {
|
62 | return line.startsWith(`${key}=`);
|
63 | });
|
64 | if (exampleFileMatchingLine) {
|
65 | this.exampleEnvContents.remove(exampleFileMatchingLine);
|
66 | }
|
67 | return this;
|
68 | }
|
69 | |
70 |
|
71 |
|
72 | commit() {
|
73 | this.envContents.commit();
|
74 | this.exampleEnvContents.commit();
|
75 | }
|
76 | |
77 |
|
78 |
|
79 | rollback() {
|
80 | this.envContents.rollback();
|
81 | this.exampleEnvContents.rollback();
|
82 | }
|
83 | }
|
84 | exports.EnvFile = EnvFile;
|