-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #188 from mocherfaoui/import-export-secrets
add ability to import/export secrets with comments
- Loading branch information
Showing
7 changed files
with
2,482 additions
and
2,305 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
const LINE = | ||
/(?:^|^)\s*(?:export\s+)?([\w.-]+)(?:\s*=\s*?|:\s+?)(\s*'(?:\\'|[^'])*'|\s*"(?:\\"|[^"])*"|\s*`(?:\\`|[^`])*`|[^#\r\n]+)?\s*(?:#.*)?(?:$|$)/gm; | ||
|
||
/** | ||
* Return text that is the buffer parsed | ||
* @param {ArrayBuffer} src - source buffer | ||
* @returns {String} text - text of buffer | ||
*/ | ||
export function parseDotEnv(src: ArrayBuffer) { | ||
const object: { | ||
[key: string]: { value: string; comments: string[] }; | ||
} = {}; | ||
|
||
// Convert buffer to string | ||
let lines = src.toString(); | ||
|
||
// Convert line breaks to same format | ||
lines = lines.replace(/\r\n?/gm, '\n'); | ||
|
||
let comments: string[] = []; | ||
|
||
lines | ||
.split('\n') | ||
.map((line) => { | ||
// collect comments of each env variable | ||
if (line.startsWith('#')) { | ||
comments.push(line.replace('#', '').trim()); | ||
} else if (line) { | ||
let match; | ||
let item: [string, string, string[]] | [] = []; | ||
|
||
while ((match = LINE.exec(line)) !== null) { | ||
const key = match[1]; | ||
|
||
// Default undefined or null to empty string | ||
let value = match[2] || ''; | ||
|
||
// Remove whitespace | ||
value = value.trim(); | ||
|
||
// Check if double quoted | ||
const maybeQuote = value[0]; | ||
|
||
// Remove surrounding quotes | ||
value = value.replace(/^(['"`])([\s\S]*)\1$/gm, '$2'); | ||
|
||
// Expand newlines if double quoted | ||
if (maybeQuote === '"') { | ||
value = value.replace(/\\n/g, '\n'); | ||
value = value.replace(/\\r/g, '\r'); | ||
} | ||
item = [key, value, comments]; | ||
} | ||
comments = []; | ||
return item; | ||
} | ||
return []; | ||
}) | ||
.filter((line) => line.length > 1) | ||
.forEach((line) => { | ||
const [key, value, comments] = line; | ||
object[key as string] = { value, comments }; | ||
}); | ||
|
||
return object; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.