@jutaru-dev/envsonar 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +188 -176
- package/bin/envsonar.js +2 -0
- package/package.json +57 -46
- package/src/check.js +157 -146
- package/src/cli.js +303 -298
- package/src/parse.js +99 -98
- package/src/scan.js +272 -33
- package/types/check.d.ts +66 -0
- package/types/cli.d.ts +34 -0
- package/types/index.d.ts +4 -0
- package/types/parse.d.ts +26 -0
- package/types/profiles.d.ts +15 -0
- package/types/scan.d.ts +156 -0
package/src/parse.js
CHANGED
|
@@ -1,98 +1,99 @@
|
|
|
1
|
-
// @ts-check
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @typedef {{ value: string, line: number, endLine: number }} Entry
|
|
5
|
-
* @typedef {{ line: number, message: string }} ParseIssue
|
|
6
|
-
* @typedef {{
|
|
7
|
-
* entries: Map<string, Entry>,
|
|
8
|
-
* duplicates: { key: string, lines: number[] }[],
|
|
9
|
-
* errors: ParseIssue[],
|
|
10
|
-
* }} ParsedEnv
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
const KEY = /^[A-Za-z_][A-Za-z0-9_.-]*$/;
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Parses dotenv text. Supports comments, `export KEY=...`, single/double/backtick quotes,
|
|
17
|
-
* multi-line quoted values and inline `#` comments after unquoted values.
|
|
18
|
-
* Never throws: malformed lines are reported in `errors`.
|
|
19
|
-
*
|
|
20
|
-
* @param {string} text
|
|
21
|
-
* @returns {ParsedEnv}
|
|
22
|
-
*/
|
|
23
|
-
export function parseEnv(text) {
|
|
24
|
-
const lines = text.replace(/^\uFEFF/, "").split(/\r?\n/);
|
|
25
|
-
/** @type {Map<string, Entry>} */
|
|
26
|
-
const entries = new Map();
|
|
27
|
-
/** @type {Map<string, number[]>} */
|
|
28
|
-
const seen = new Map();
|
|
29
|
-
/** @type {ParseIssue[]} */
|
|
30
|
-
const errors = [];
|
|
31
|
-
|
|
32
|
-
for (let i = 0; i < lines.length; i++) {
|
|
33
|
-
const lineNo = i + 1;
|
|
34
|
-
const raw = lines[i].trim();
|
|
35
|
-
if (raw === "" || raw.startsWith("#")) continue;
|
|
36
|
-
|
|
37
|
-
const body = raw.startsWith("export ") ? raw.slice(7).trimStart() : raw;
|
|
38
|
-
const eq = body.indexOf("=");
|
|
39
|
-
if (eq === -1) {
|
|
40
|
-
errors.push({ line: lineNo, message: "expected KEY=VALUE" });
|
|
41
|
-
continue;
|
|
42
|
-
}
|
|
43
|
-
const key = body.slice(0, eq).trim();
|
|
44
|
-
if (!KEY.test(key)) {
|
|
45
|
-
errors.push({ line: lineNo, message: "invalid key name" });
|
|
46
|
-
continue;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
let rest = body.slice(eq + 1).trimStart();
|
|
50
|
-
let value;
|
|
51
|
-
const quote = rest[0];
|
|
52
|
-
if (quote === '"' || quote === "'" || quote === "`") {
|
|
53
|
-
// a quoted value may continue over the following lines until its closing quote
|
|
54
|
-
let collected = rest.slice(1);
|
|
55
|
-
let close = findClosing(collected, quote);
|
|
56
|
-
let j = i;
|
|
57
|
-
while (close === -1 && j + 1 < lines.length) {
|
|
58
|
-
j++;
|
|
59
|
-
collected += "\n" + lines[j];
|
|
60
|
-
close = findClosing(collected, quote);
|
|
61
|
-
}
|
|
62
|
-
if (close === -1) {
|
|
63
|
-
errors.push({ line: lineNo, message: "unterminated quoted value" });
|
|
64
|
-
i = j;
|
|
65
|
-
continue;
|
|
66
|
-
}
|
|
67
|
-
value = collected.slice(0, close);
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
* @param {string}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @typedef {{ value: string, line: number, endLine: number }} Entry
|
|
5
|
+
* @typedef {{ line: number, message: string }} ParseIssue
|
|
6
|
+
* @typedef {{
|
|
7
|
+
* entries: Map<string, Entry>,
|
|
8
|
+
* duplicates: { key: string, lines: number[] }[],
|
|
9
|
+
* errors: ParseIssue[],
|
|
10
|
+
* }} ParsedEnv
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
const KEY = /^[A-Za-z_][A-Za-z0-9_.-]*$/;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Parses dotenv text. Supports comments, `export KEY=...`, single/double/backtick quotes,
|
|
17
|
+
* multi-line quoted values and inline `#` comments after unquoted values.
|
|
18
|
+
* Never throws: malformed lines are reported in `errors`.
|
|
19
|
+
*
|
|
20
|
+
* @param {string} text
|
|
21
|
+
* @returns {ParsedEnv}
|
|
22
|
+
*/
|
|
23
|
+
export function parseEnv(text) {
|
|
24
|
+
const lines = text.replace(/^\uFEFF/, "").split(/\r?\n/);
|
|
25
|
+
/** @type {Map<string, Entry>} */
|
|
26
|
+
const entries = new Map();
|
|
27
|
+
/** @type {Map<string, number[]>} */
|
|
28
|
+
const seen = new Map();
|
|
29
|
+
/** @type {ParseIssue[]} */
|
|
30
|
+
const errors = [];
|
|
31
|
+
|
|
32
|
+
for (let i = 0; i < lines.length; i++) {
|
|
33
|
+
const lineNo = i + 1;
|
|
34
|
+
const raw = lines[i].trim();
|
|
35
|
+
if (raw === "" || raw.startsWith("#")) continue;
|
|
36
|
+
|
|
37
|
+
const body = raw.startsWith("export ") ? raw.slice(7).trimStart() : raw;
|
|
38
|
+
const eq = body.indexOf("=");
|
|
39
|
+
if (eq === -1) {
|
|
40
|
+
errors.push({ line: lineNo, message: "expected KEY=VALUE" });
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
const key = body.slice(0, eq).trim();
|
|
44
|
+
if (!KEY.test(key)) {
|
|
45
|
+
errors.push({ line: lineNo, message: "invalid key name" });
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
let rest = body.slice(eq + 1).trimStart();
|
|
50
|
+
let value;
|
|
51
|
+
const quote = rest[0];
|
|
52
|
+
if (quote === '"' || quote === "'" || quote === "`") {
|
|
53
|
+
// a quoted value may continue over the following lines until its closing quote
|
|
54
|
+
let collected = rest.slice(1);
|
|
55
|
+
let close = findClosing(collected, quote);
|
|
56
|
+
let j = i;
|
|
57
|
+
while (close === -1 && j + 1 < lines.length) {
|
|
58
|
+
j++;
|
|
59
|
+
collected += "\n" + lines[j];
|
|
60
|
+
close = findClosing(collected, quote);
|
|
61
|
+
}
|
|
62
|
+
if (close === -1) {
|
|
63
|
+
errors.push({ line: lineNo, message: "unterminated quoted value" });
|
|
64
|
+
i = j;
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
value = collected.slice(0, close);
|
|
68
|
+
// one pass, so `\\n` stays a backslash followed by "n" instead of turning into a newline
|
|
69
|
+
if (quote === '"') value = value.replace(/\\([nr"\\])/g, (_, c) => (c === "n" ? "\n" : c === "r" ? "\r" : c));
|
|
70
|
+
i = j;
|
|
71
|
+
} else {
|
|
72
|
+
const hash = rest.startsWith("#") ? 0 : rest.search(/\s#/);
|
|
73
|
+
value = (hash === -1 ? rest : rest.slice(0, hash)).trim();
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const prev = seen.get(key);
|
|
77
|
+
if (prev) prev.push(lineNo);
|
|
78
|
+
else seen.set(key, [lineNo]);
|
|
79
|
+
entries.set(key, { value, line: lineNo, endLine: i + 1 });
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const duplicates = [...seen].filter(([, l]) => l.length > 1).map(([key, l]) => ({ key, lines: l }));
|
|
83
|
+
return { entries, duplicates, errors };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* @param {string} s
|
|
88
|
+
* @param {string} quote
|
|
89
|
+
*/
|
|
90
|
+
function findClosing(s, quote) {
|
|
91
|
+
for (let k = 0; k < s.length; k++) {
|
|
92
|
+
if (s[k] === "\\" && quote === '"') {
|
|
93
|
+
k++;
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
if (s[k] === quote) return k;
|
|
97
|
+
}
|
|
98
|
+
return -1;
|
|
99
|
+
}
|