Comparing version 0.0.7 to 0.0.8
{ | ||
"name": "editenv", | ||
"version": "0.0.7", | ||
"version": "0.0.8", | ||
"description": "EditEnv is a simple and efficient Node.js library for loading, parsing, editing, and saving .env files. Ideal for programmatically managing environment variables in your projects.", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -17,7 +17,9 @@ # EditEnv | ||
```js | ||
const EditEnv = require('editenv'); | ||
const { EditEnvLoad, EditEnvAsJson, EditEnvGet, EditEnvSet } = require('editenv'); | ||
const envEditor = new EditEnv('./.env'); | ||
envEditor.edit('NEW_KEY', 'new_value'); | ||
envEditor.save(); | ||
EditEnvLoad('./.env'); | ||
EditEnvAsJson('./.env'); | ||
EditEnvGet('./.env', 'EXISTING_KEY') | ||
EditEnvSet('./.env', 'EXISTING_KEY', 'new_value'); | ||
EditEnvSet('./.env', 'NEW_KEY', 'new_value'); | ||
``` |
@@ -9,2 +9,6 @@ const fs = require('fs'); | ||
get() { | ||
return this.originalContent; | ||
} | ||
load() { | ||
@@ -42,2 +46,28 @@ return this.parse(); | ||
module.exports = EditEnv; | ||
function EditEnvGet(filePath, key) { | ||
const envEditor = new EditEnv(filePath); | ||
return envEditor.load()[key]; | ||
} | ||
function EditEnvLoad(filePath) { | ||
const envEditor = new EditEnv(filePath); | ||
return envEditor.get(); | ||
} | ||
function EditEnvAsJson(filePath) { | ||
const envEditor = new EditEnv(filePath); | ||
return envEditor.parse(); | ||
} | ||
function EditEnvSet(filePath, key, value) { | ||
const envEditor = new EditEnv(filePath); | ||
envEditor.edit(key, value); | ||
envEditor.save(); | ||
} | ||
module.exports = { | ||
EditEnvLoad, | ||
EditEnvAsJson, | ||
EditEnvGet, | ||
EditEnvSet, | ||
} |
const fs = require('fs'); | ||
const EditEnv = require('../src/index'); | ||
const { EditEnvLoad, EditEnvAsJson, EditEnvSet } = require('../src/index'); | ||
@@ -9,8 +9,3 @@ // Cria um arquivo .env de teste | ||
describe('EditEnv', () => { | ||
let envEditor; | ||
beforeEach(() => { | ||
envEditor = new EditEnv(testEnvPath); | ||
}); | ||
afterEach(() => { | ||
@@ -21,3 +16,3 @@ fs.writeFileSync(testEnvPath, '#MAIN TEST\n\nEXISTING_KEY=existing_value\nANOTHER_KEY=another_value\n'); | ||
test('should load .env file', () => { | ||
const envConfig = envEditor.load(); | ||
const envConfig = EditEnvAsJson(testEnvPath); | ||
expect(envConfig).toEqual({ | ||
@@ -30,3 +25,3 @@ EXISTING_KEY: 'existing_value', | ||
test('should parse .env file', () => { | ||
const parsedConfig = envEditor.parse(); | ||
const parsedConfig = EditEnvAsJson(testEnvPath); | ||
expect(parsedConfig).toEqual({ | ||
@@ -39,5 +34,5 @@ EXISTING_KEY: 'existing_value', | ||
test('should edit .env file', () => { | ||
envEditor.edit('NEW_KEY', 'new_value'); | ||
envEditor.edit('EXISTING_KEY', 'updated_value'); | ||
const parsedConfig = envEditor.parse(); | ||
EditEnvSet(testEnvPath, 'NEW_KEY', 'new_value'); | ||
EditEnvSet(testEnvPath, 'EXISTING_KEY', 'updated_value'); | ||
const parsedConfig = EditEnvAsJson(testEnvPath); | ||
expect(parsedConfig).toEqual({ | ||
@@ -51,8 +46,6 @@ EXISTING_KEY: 'updated_value', | ||
test('should save .env file', () => { | ||
envEditor.edit('NEW_KEY', 'new_value'); | ||
envEditor.save(); | ||
const fileContent = fs.readFileSync(testEnvPath, 'utf-8'); | ||
EditEnvSet(testEnvPath, 'NEW_KEY', 'new_value'); | ||
const fileContent = EditEnvLoad(testEnvPath); | ||
expect(fileContent).toContain('NEW_KEY=new_value'); | ||
}); | ||
}); |
624650
117
25