Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

env-spec

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

env-spec - npm Package Compare versions

Comparing version 0.3.0 to 0.4.0

10

package.json
{
"name": "env-spec",
"version": "0.3.0",
"version": "0.4.0",
"description": "modify env files to HTML",

@@ -12,2 +12,3 @@ "main": "index.js",

"jest": "^23.4.1",
"jsdoc": "^3.5.5",
"prettier": "^1.13.7",

@@ -19,8 +20,9 @@ "prettier-check": "^2.0.0",

"test": "jest",
"lint": "prettier-check src/*.js",
"format": "prettier --write src/*.js",
"lint": "prettier-check {demo,src}/*.{js,css}",
"format": "prettier --write {demo,src}/*.{js,css}",
"watch": "watchify src/main.js --verbose --standalone envSpec -o dist/envspec.js",
"start": "browser-sync start --server demo --server dist --watch --files 'dist/*' 'demo/*' --port $PORT"
"start": "browser-sync start --server demo --server dist --watch --files 'dist/*' 'demo/*' --port $PORT",
"docs": "jsdoc src/main.js -d docs"
},
"dependencies": {}
}

166

src/main.js

@@ -1,4 +0,10 @@

//function that checks if each env entry is valid
//environmental variable of entry cannot start with a digit and must only contain alphanumeric characters or an underscore
//type of entry must be valid
/** @method checkValidationOfValues
* @summary Checks if each env entry of .env file is valid.
* @description Environmental variable name of entry cannot start with a digit and must only contain alphanumeric characters or an underscore.
* Type of entry must be valid or null.
* Restricted choices and default values of entry have specific syntax or are null.
* Uses parsing function {@link parseVarFromType} to get entries.
* @param {string} envSpecString the whole .env file as a string
* @returns {Array|null} The array of all valid entries or null if something was invalid.
*/
const checkValidationOfValues = envSpecString => {

@@ -28,24 +34,46 @@ const validTypes = [

//element.choices will contain the given options or null
//element.defaultValue is the default Value if there was a given one
envSpecEntries = envSpecLines.map(element => {
if (
//in case environmental variable is valid and entry has a valid type
element.name.match(alphanumericThatDoesNotStartWithDigit) &&
validTypes.includes(element.type)
) {
return element;
}
//in case environmental variable is valid and entry has restricted choices (indicated by "[]" ,we should split them
else if (
element.name.match(alphanumericThatDoesNotStartWithDigit) &&
element.type.match(genericForCheckingRestrChoicesSyntax)
) {
element.choices = element.type
.match(genericForCheckingRestrChoicesSyntax)[1]
.split(",");
element.type = null;
return element;
}
//in case of a syntax error in any cases of the above
else {
if (element.defaultValue === "") {
//this would happen in case input is "DATA: number = " or "DATA :[4,2] = "
checkValidation = false;
} else {
if (
//in case environmental variable is valid and entry has a valid type
element.name.match(alphanumericThatDoesNotStartWithDigit) &&
validTypes.includes(element.type) &&
checkValidation === true
) {
return element;
}
//in case environmental variable is valid and entry has restricted choices (indicated by "[]" ,we should split them
else if (
element.name.match(alphanumericThatDoesNotStartWithDigit) &&
element.type.match(genericForCheckingRestrChoicesSyntax) &&
checkValidation === true
) {
element.choices = element.type
.match(genericForCheckingRestrChoicesSyntax)[1]
.split(",");
element.choices = element.choices.map(choice => {
if (choice.trim() === "") {
//check for wrong syntax "DATA: [1, ]"
checkValidation = false;
}
return choice.trim(); //trim valid choices
});
element.type = null;
//if there is a default value, check if it is valid
if (
element.defaultValue &&
!element.choices.includes(element.defaultValue)
) {
checkValidation = false;
}
return element;
}
//in case of a syntax error in any cases of the above
else {
checkValidation = false;
}
}

@@ -62,15 +90,38 @@ });

//class that creates entries
/**
* Class representing an Entry
* @class
*/
class Entry {
constructor(envName, envType, envChoices, envDefaultVal) {
this.name = envName;
this.type = envType;
this.choices = envChoices;
this.defaultValue = envDefaultVal;
/**
* @param {string} name environmental variable e.g. DATABASE_URL
*@param {string|null} type type of variable e.g. url
*@param {Array|null} choices given restricted choices e.g. [data,info,1]
*@param {string|null} defaultValue given default value e.g. data
*@param {string} comment given comment using the # symbol
*/
constructor(name, type, choices, defaultValue, comment) {
this.name = name;
this.type = type;
this.choices = choices;
this.defaultValue = defaultValue;
this.comment = comment;
}
}
//function that separates the variables from their types and returns them as an array
/** @function parseVarFromType
* @summary Separates the variables' name from their types, their restricted choices and their default values and returns them as an array of {@link Entry} objects.
* @desc Parses each line according to existing symbols such as : or = and create an entry object for each line.If a feature do not exist,its field will be null.
* @param {Array} envSpecAsArray array with the slpitted lines of the .env file based on the \n symbol
* @returns {Array} An array containing entry objects.
*/
const parseVarFromType = envSpecAsArray => {
return (envSpecAsArrayParsed = envSpecAsArray.map(element => {
const commentRegexp = /#(.+)$/;
return envSpecAsArray.filter(line => !line.startsWith("#")).map(element => {
//in case of existing comment ignore what's after the "#" symbol
const commentMatch = element.match(commentRegexp);
const comment = commentMatch ? commentMatch[1].trim() : null;
element = commentMatch ? element.split("#")[0] : element;
//in case of typed variable or given restricted choices

@@ -86,16 +137,27 @@ if (element.includes(":")) {

null,
element[1][1].trim()
element[1][1].trim(),
comment
);
}
//else if there is just a type
return new Entry(element[0].trim(), element[1].trim(), null, null);
return new Entry(
element[0].trim(),
element[1].trim(),
null,
null,
comment
);
}
//in case of untyped variable , give default type
else {
return new Entry(element.trim(), "text", null, null);
return new Entry(element.trim(), "text", null, null, comment);
}
}));
});
};
//function that creates the render label for the output outputHTML
/** @function renderLabelForEntry
* @desc Creates the render for the {@link outputHTML} function.
* @param {string} name the given environmental variable name
* @returns {string}
*/
const renderLabelForEntry = name => {

@@ -105,4 +167,8 @@ return (labelToPrint = `<label for="env_spec_${name.toLowerCase()}">${name}</label>\n`);

//function that returns the HTML code as a string
//envSpecEntriesArray is an array containing entries with their variable name and its type or restricted choices
/** @function outputHTML
* @summary Renders an {@link Entry} object array to HTML code as a string.
* @desc Checks which attributes of the object are null and which they are not , so the proper message is rendered.
* @param {Array|null} envSpecEntriesArray is an array containing entry objects
* @returns {string}
*/
const outputHTML = envSpecEntriesArray => {

@@ -123,3 +189,2 @@ //create HTML format if there is not a syntax error

toPrint += ` />\n`;
return toPrint;
} else if (element.choices) {

@@ -130,8 +195,19 @@ //if element is value with restricted choices

//print text for every option
toPrint =
toPrint + ` <option value="${choice}">${choice}</option>\n`;
if (choice === element.defaultValue) {
//in case defaultValue was given
toPrint =
toPrint +
` <option value="${choice}" selected>${choice}</option>\n`;
} else {
toPrint =
toPrint + ` <option value="${choice}">${choice}</option>\n`;
}
}
toPrint += `</select>\n`;
return toPrint;
}
//in case of existing comment add it to toPrint value
if (element.comment) {
toPrint = toPrint + `<small>${element.comment}</small>\n`;
}
return toPrint;
});

@@ -145,3 +221,7 @@ //return as string value

//function for final output
/** @function envSpecToHTML
* @desc gives final output using {@link outputHTML} and {@link checkValidationOfValues}
* @param {string} envSpec the .env file given in string format
* @returns {string} HTML code
*/
const envSpecToHTML = envSpec => {

@@ -148,0 +228,0 @@ return outputHTML(checkValidationOfValues(envSpec));

@@ -26,3 +26,14 @@ const envSpecToHTML = require("./main.js");

test("Valid input with default value", () => {
test("Valid input with default value for type", () => {
const testEnv = "DEBUG: [0, 1]=1";
expect(envSpecToHTML(testEnv)).toEqual(
`<label for="env_spec_debug">DEBUG</label>\n` +
`<select id="env_spec_debug" name="debug">\n` +
` <option value="0">0</option>\n` +
` <option value="1" selected>1</option>\n` +
`</select>\n`
);
});
test("Valid input with default value for restricted choices", () => {
const testEnv = "DATABASE_URL: email = test@mail.com";

@@ -35,2 +46,20 @@ expect(envSpecToHTML(testEnv)).toEqual(

test("Valid input with comment,middle of line", () => {
const testEnv = "DATABASE_URL: email = test@mail.com #comment";
expect(envSpecToHTML(testEnv)).toEqual(
`<label for="env_spec_database_url">DATABASE_URL</label>\n` +
`<input id="env_spec_database_url" name="database_url" type="email" value="test@mail.com" />\n` +
`<small>comment</small>\n`
);
});
test("Valid input with comment, start of line", () => {
const testEnv =
"#DATABASE_URL: email = test@mail.com\nDATABASE_URL: email = test@mail.com";
expect(envSpecToHTML(testEnv)).toEqual(
`<label for="env_spec_database_url">DATABASE_URL</label>\n` +
`<input id="env_spec_database_url" name="database_url" type="email" value="test@mail.com" />\n`
);
});
test("Invalid environmental variable : starts with number", () => {

@@ -76,5 +105,20 @@ const testEnv = "1DATABASE_URL\nADMIN_EMAIL:email";

test("Wrong Syntax for default value", () => {
test("Wrong Syntax for restricted choices2", () => {
const testEnv = "DEBUG: [0, ]";
expect(envSpecToHTML(testEnv)).toEqual("Error:Wrong Syntax");
});
test("Wrong Syntax for default value for type", () => {
const testEnv = "DATABASE_URL\nADMIN_EMAIL:email\nDATA: text dasdsa";
expect(envSpecToHTML(testEnv)).toEqual("Error:Wrong Syntax");
});
test("Wrong Syntax for default value for type", () => {
const testEnv = "DEBUG: [0, 1]=4";
expect(envSpecToHTML(testEnv)).toEqual("Error:Wrong Syntax");
});
test("Wrong Syntax for default value,missing value", () => {
const testEnv = "DEBUG: [0, 1]=";
expect(envSpecToHTML(testEnv)).toEqual("Error:Wrong Syntax");
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc