From 8161720535a69a020162947669365a2f40b523aa Mon Sep 17 00:00:00 2001 From: Aleksei Zarubin <12220926+alexzarbn@users.noreply.github.com> Date: Sun, 11 Sep 2022 13:30:04 +0500 Subject: [PATCH] feat: setting comments and adding non-existing entries --- README.md | 26 ++++++++++++++++++++++++++ src/dotEnv.ts | 20 ++++++++++++++++++-- src/dotEnvEntry.ts | 5 ++--- tests/dotEnv.test.ts | 25 +++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 5 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..f160570 --- /dev/null +++ b/README.md @@ -0,0 +1,26 @@ +# .env File Entries Manager + +This package allows you to easily manage `.env` file entries in a type-safe manner. + +## Usage + +```ts + +let dotEnv = new DotEnv(`EXAMPLE_NUMBER_FIELD=123`); + +// retrieving +const exampleNumberEntry = dotEnv.get('EXAMPLE_NUMBER_FIELD'); + +console.log(exampleNumberEntry.key); // string ("EXAMPLE_NUMBER_FIELD") +console.log(exampleNumberEntry.value); // number (123) +console.log(exampleNumberEntry.comment); // undefined + +// setting values and comments +dotEnv.set('EXAMPLE_NUMBER_FIELD', 456); +dotEnv.setComment('EXAMPLE_NUMBER_FIELD', 'Example comment'); + +dotEnv.set('NEW_FIELD', 'test'); + +// formatting entries to string +console.log(dotEnv.toString()); // "EXAMPLE_NUMBER_FIELD=456 # Example comment\nNEWFIELD=test" +``` diff --git a/src/dotEnv.ts b/src/dotEnv.ts index a873d55..ab8c39a 100644 --- a/src/dotEnv.ts +++ b/src/dotEnv.ts @@ -30,14 +30,30 @@ export class DotEnv { return this.entries.find((entry) => entry.key === key) as DotEnvEntry; } - public set(key: string, value: DotEnvEntryValueType): DotEnvEntry { + public set(key: string, value: T, comment?: string): DotEnvEntry { + let entryIndex = this.entries.findIndex((entry) => entry.key === key); + + if (entryIndex < 0) { + const entry = new DotEnvEntry(key, value, comment); + + this.entries.push(entry); + + return entry; + } else { + this.entries[entryIndex].value = value; + + return this.entries[entryIndex] as DotEnvEntry; + } + } + + public setComment(key: string, comment: string | undefined): DotEnvEntry { let entryIndex = this.entries.findIndex((entry) => entry.key === key); if (entryIndex < 0) { throw new Error(`Unable to find entry with the given key: ${key}`); } - this.entries[entryIndex].value = value; + this.entries[entryIndex].comment = comment; return this.entries[entryIndex] as DotEnvEntry; } diff --git a/src/dotEnvEntry.ts b/src/dotEnvEntry.ts index a32db1e..9bc1381 100644 --- a/src/dotEnvEntry.ts +++ b/src/dotEnvEntry.ts @@ -1,9 +1,8 @@ import {DotEnvEntryValueType} from "./dotEnvEntryValueType"; export class DotEnvEntry { - public key?: string; - public value?: T; - public comment?: string; + constructor(public key?: string, public value?: T, public comment?: string) { + } public toString(): string { let formattedEntry = ''; diff --git a/tests/dotEnv.test.ts b/tests/dotEnv.test.ts index 80b5895..8ace0c2 100644 --- a/tests/dotEnv.test.ts +++ b/tests/dotEnv.test.ts @@ -89,6 +89,31 @@ describe('DotEnv tests', () => { expect(updatedEntry?.value).toBe('updated'); }); + test('setting value and comment of a non-existing entry', () => { + const dotEnv = new DotEnv(`keyA=a`); + + const addedEntry = dotEnv.set('NON_EXISTING', 'test-value', 'test-comment'); + + const entry = dotEnv.get('NON_EXISTING'); + + expect(entry?.value).toBe('test-value'); + expect(addedEntry?.value).toBe('test-value'); + + expect(entry?.comment).toBe('test-comment'); + expect(addedEntry?.comment).toBe('test-comment'); + }); + + test('setting entry comment', () => { + const dotEnv = new DotEnv(`keyA=a`); + + const updatedEntry = dotEnv.setComment('keyA', 'example comment'); + + const entry = dotEnv.get('keyA'); + + expect(entry?.comment).toBe('example comment'); + expect(updatedEntry?.comment).toBe('example comment'); + }); + test('removing an entry', () => { const dotEnv = new DotEnv(`keyA=a`);