Skip to content
This repository has been archived by the owner on Jul 9, 2023. It is now read-only.

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
RobuxTRex committed Dec 18, 2022
1 parent 952e264 commit cd79f7e
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
docs/.next
docs/node_modules
test/node_modules
14 changes: 14 additions & 0 deletions package/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# lNV

lNV is a light-weight .eNV file read/write package.
lNV isn't very feature-rich compared to dotENV, but for small projects like Discord bots, all you need is a read function, it's wasted space.
This package also targets to become as easy-to-use as possible!

The documentation will be available soon, at:
https://lnv.robuxtrex.co.uk

## Early Access

As this is my first package, I would like to clarify that some versions may/will not work. Use at your own risk.
Though, you can always help me on GitHub issues :D
https://github.com/RobuxTRex/lnv/issues
21 changes: 21 additions & 0 deletions package/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "lnv",
"version": "1.0.0-4",
"description": "lNV can read and write to ENV files with little-to-no performance cost!",
"main": "src/index.js",
"scripts": {
"private": "npm publish",
"public": "npm publish --access public",
"start": "node ./src/entry.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/RobuxTRex/lnv.git"
},
"author": "sulphurdev",
"license": "MIT",
"bugs": {
"url": "https://github.com/RobuxTRex/lnv/issues"
},
"homepage": "https://lnv.robuxtrex.co.uk/link/npm"
}
28 changes: 28 additions & 0 deletions package/src/func/load.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const root = process.cwd()

const fs = require('fs');
const path = require('path');
const { parseBuffer } = require('../parse');

module.exports = function loadEnv(target) {

if (!target) return null;

const envFilePath = path.join(root, '.env');
const bufferEnv = fs.readFileSync(envFilePath);
const envObject = parseBuffer(bufferEnv);

let found = Object.entries(envObject)

if(envObject == null) {
return null;
} else {
for (const [key, value] of Object.entries(envObject)) {
if (key == target) {
return value;
} else {
null;
}
}
}
}
4 changes: 4 additions & 0 deletions package/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const loadEnv = require('./func/load')

exports.get = loadEnv
exports.read = loadEnv
42 changes: 42 additions & 0 deletions package/src/parse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const NEWLINES_MATCH = /\r\n|\n|\r/
const NEWLINE = '\n'
const RE_INI_KEY_VAL = /^\s*([\w.-]+)\s*=\s*(.*)?\s*$/
const RE_NEWLINES = /\\n/g

const parseBuffer = (src) => {
const obj = {};
src.toString().split(NEWLINES_MATCH).forEach((line, idx) => {
// matching "KEY" and "VAL" in "KEY=VAL"
const keyValueArr = line.match(RE_INI_KEY_VAL);
// matched?
if(keyValueArr != null){
const key = keyValueArr[1];

// default undefined or missing values to empty string

let val = (keyValueArr[2] || '');
const end = val.length -1;
const isDoubleQuoted = val[0] === '"' && val[end] === '"';
const isSingleQuoted = val[0] === "'" && val[end] === "'";

// if single or double quoted, remove quotes
if(isSingleQuoted || isDoubleQuoted) {
val = val.substring(1, end);

// if double quoted, expand newlines
if(isDoubleQuoted){
val = val.replace(RE_NEWLINES, NEWLINE);
}
} else {
// remove surrounding whitespace
val = val.trim();
}
obj[key] = val;
}
});
return obj;
}

module.exports = {
parseBuffer
}
5 changes: 3 additions & 2 deletions test/.env
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
USER=xyz
ID=123xyz
USER=SulphurDev
PASSWORD=123xyz
EMAIL=robuxtrex@gmail.com
2 changes: 1 addition & 1 deletion test/main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const lnv = require('lnv')

console.log(lnv.get('USER'))
console.log(lnv.get('e'))

1 comment on commit cd79f7e

@vercel
Copy link

@vercel vercel bot commented on cd79f7e Dec 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

lnv – ./

lnv.vercel.app
lnv-git-main-robuxtrex.vercel.app
lnv-robuxtrex.vercel.app

Please sign in to comment.