@shubhammaji1/envparity 0.0.0-stage → 0.1.1
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 +21 -2
- package/dist/analyze.js +55 -0
- package/dist/cli.js +116 -0
- package/dist/env.js +53 -0
- package/dist/project.js +154 -0
- package/dist/report.js +132 -0
- package/package.json +27 -6
package/README.md
CHANGED
|
@@ -1,3 +1,22 @@
|
|
|
1
|
-
#
|
|
1
|
+
# EnvParity
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Offline CLI checks for missing, empty, invalid, and undocumented environment
|
|
4
|
+
variables. Reports include key names without printing dotenv values.
|
|
5
|
+
|
|
6
|
+
Requires Node.js 22.15 or later. Git enables repository-aware discovery.
|
|
7
|
+
|
|
8
|
+
```sh
|
|
9
|
+
npm install -g @shubhammaji1/envparity@0.1.1
|
|
10
|
+
envparity --version
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
In your project, add a required key to .env.example, such as PORT=, then run:
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
envparity init
|
|
17
|
+
envparity doctor
|
|
18
|
+
envparity validate --json
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Run init once per project. If PORT is missing, set it in your local .env and
|
|
22
|
+
rerun the check. Use envparity --help for commands and exit codes.
|
package/dist/analyze.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.scan = scan;
|
|
7
|
+
const typescript_1 = __importDefault(require("typescript"));
|
|
8
|
+
const project_1 = require("./project");
|
|
9
|
+
function scan(project) {
|
|
10
|
+
const references = [];
|
|
11
|
+
const skipped = [];
|
|
12
|
+
for (const file of (0, project_1.sourceFiles)(project).sort()) {
|
|
13
|
+
let text;
|
|
14
|
+
try {
|
|
15
|
+
text = (0, project_1.readLocal)(project, file);
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
skipped.push(file);
|
|
19
|
+
continue;
|
|
20
|
+
}
|
|
21
|
+
if (text === undefined || text.includes("\0"))
|
|
22
|
+
continue;
|
|
23
|
+
const source = typescript_1.default.createSourceFile(file, text, typescript_1.default.ScriptTarget.Latest, true);
|
|
24
|
+
function track(node, key) {
|
|
25
|
+
const line = source.getLineAndCharacterOfPosition(node.getStart(source)).line + 1;
|
|
26
|
+
references.push({ key: key && /^[A-Za-z_][A-Za-z0-9_.-]*$/.test(key) ? key : undefined, file, line });
|
|
27
|
+
}
|
|
28
|
+
function isEnv(node) {
|
|
29
|
+
if (!typescript_1.default.isPropertyAccessExpression(node) || node.name.text !== "env")
|
|
30
|
+
return false;
|
|
31
|
+
const owner = node.expression;
|
|
32
|
+
return (typescript_1.default.isIdentifier(owner) && owner.text === "process") ||
|
|
33
|
+
(typescript_1.default.isMetaProperty(owner) && owner.keywordToken === typescript_1.default.SyntaxKind.ImportKeyword && owner.name.text === "meta");
|
|
34
|
+
}
|
|
35
|
+
function visit(node) {
|
|
36
|
+
if (typescript_1.default.isPropertyAccessExpression(node) && isEnv(node.expression)) {
|
|
37
|
+
track(node, node.name.text);
|
|
38
|
+
}
|
|
39
|
+
else if (typescript_1.default.isElementAccessExpression(node) && isEnv(node.expression)) {
|
|
40
|
+
track(node, typescript_1.default.isStringLiteralLike(node.argumentExpression) ? node.argumentExpression.text : undefined);
|
|
41
|
+
}
|
|
42
|
+
else if (typescript_1.default.isVariableDeclaration(node) && node.initializer && isEnv(node.initializer) &&
|
|
43
|
+
typescript_1.default.isObjectBindingPattern(node.name)) {
|
|
44
|
+
for (const element of node.name.elements) {
|
|
45
|
+
const name = element.propertyName ?? element.name;
|
|
46
|
+
track(element, !element.dotDotDotToken && (typescript_1.default.isIdentifier(name) || typescript_1.default.isStringLiteralLike(name)) ?
|
|
47
|
+
name.text : undefined);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
typescript_1.default.forEachChild(node, visit);
|
|
51
|
+
}
|
|
52
|
+
visit(source);
|
|
53
|
+
}
|
|
54
|
+
return { references, skipped };
|
|
55
|
+
}
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
const node_fs_1 = require("node:fs");
|
|
5
|
+
const node_path_1 = require("node:path");
|
|
6
|
+
const node_util_1 = require("node:util");
|
|
7
|
+
const yaml_1 = require("yaml");
|
|
8
|
+
const project_1 = require("./project");
|
|
9
|
+
const report_1 = require("./report");
|
|
10
|
+
const help = `EnvParity — Git-aware environment checks
|
|
11
|
+
|
|
12
|
+
Usage: envparity <command> [--json] [--mode <environment>]
|
|
13
|
+
|
|
14
|
+
init Create envparity.yml in the current project
|
|
15
|
+
doctor Check environment files, schema and source references
|
|
16
|
+
diff List configuration and source findings
|
|
17
|
+
validate Validate environment files against the schema
|
|
18
|
+
scan List source analysis findings
|
|
19
|
+
status Show environment health and findings
|
|
20
|
+
--help Print this help
|
|
21
|
+
--version Print the package version
|
|
22
|
+
|
|
23
|
+
Exit codes: 0 success, 1 policy errors, 2 usage or operational failure
|
|
24
|
+
`;
|
|
25
|
+
function output(value) {
|
|
26
|
+
console.log(JSON.stringify(value, null, 2));
|
|
27
|
+
}
|
|
28
|
+
function main() {
|
|
29
|
+
const arguments_ = process.argv.slice(2);
|
|
30
|
+
let json = arguments_.includes("--json");
|
|
31
|
+
try {
|
|
32
|
+
const { positionals, values } = (0, node_util_1.parseArgs)({
|
|
33
|
+
args: arguments_,
|
|
34
|
+
allowPositionals: true,
|
|
35
|
+
options: {
|
|
36
|
+
json: { type: "boolean" },
|
|
37
|
+
mode: { type: "string", default: "development" },
|
|
38
|
+
help: { type: "boolean", short: "h" },
|
|
39
|
+
version: { type: "boolean", short: "v" },
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
json = !!values.json;
|
|
43
|
+
if (values.version) {
|
|
44
|
+
const { version } = require("../package.json");
|
|
45
|
+
if (json)
|
|
46
|
+
output({ version });
|
|
47
|
+
else
|
|
48
|
+
console.log(version);
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
if (values.help || !positionals.length) {
|
|
52
|
+
if (json)
|
|
53
|
+
output({ help });
|
|
54
|
+
else
|
|
55
|
+
console.log(help);
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
const [command] = positionals;
|
|
59
|
+
if (positionals.length !== 1 || !["init", "doctor", "diff", "validate", "scan", "status"].includes(command)) {
|
|
60
|
+
throw new Error("Unknown command or unexpected arguments. Run envparity --help.");
|
|
61
|
+
}
|
|
62
|
+
const mode = values.mode ?? "development";
|
|
63
|
+
if (!/^[A-Za-z0-9][A-Za-z0-9_-]*$/.test(mode))
|
|
64
|
+
throw new Error("Invalid environment mode.");
|
|
65
|
+
const cwd = (0, node_fs_1.realpathSync)(process.cwd());
|
|
66
|
+
if (command === "init") {
|
|
67
|
+
const { project } = (0, project_1.discover)(cwd);
|
|
68
|
+
const { manifest } = (0, project_1.config)(project);
|
|
69
|
+
const path = (0, node_path_1.join)(project, project_1.configName);
|
|
70
|
+
try {
|
|
71
|
+
(0, node_fs_1.writeFileSync)(path, (0, yaml_1.stringify)({ version: 1, files: { schema: manifest }, variables: {} }), { flag: "wx" });
|
|
72
|
+
}
|
|
73
|
+
catch (error) {
|
|
74
|
+
if (error.code !== "EEXIST")
|
|
75
|
+
throw error;
|
|
76
|
+
throw new Error("envparity.yml already exists.");
|
|
77
|
+
}
|
|
78
|
+
if (json)
|
|
79
|
+
output({ created: project_1.configName });
|
|
80
|
+
else
|
|
81
|
+
console.log("Created envparity.yml. Run envparity doctor to check the project.");
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
const result = (0, report_1.report)(cwd, mode, command !== "validate");
|
|
85
|
+
if (command === "scan")
|
|
86
|
+
result.findings = result.findings.filter((finding) => finding.source === "source");
|
|
87
|
+
const errors = result.findings.filter((finding) => finding.severity === "error").length;
|
|
88
|
+
if (json) {
|
|
89
|
+
output({ ...result, errors });
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
console.log(`EnvParity | ${result.framework} | ${result.mode} | ${result.branch ?? "no active branch"}`);
|
|
93
|
+
if (command !== "scan")
|
|
94
|
+
console.log(`${result.valid} valid | ${result.files.length} environment files`);
|
|
95
|
+
for (const finding of result.findings) {
|
|
96
|
+
const location = finding.line ? `${finding.file}:${finding.line}` : finding.file;
|
|
97
|
+
console.log(`${finding.severity.toUpperCase()} ${finding.type} ${finding.key ?? ""} (${location}) — ${finding.resolution}`);
|
|
98
|
+
}
|
|
99
|
+
console.log(`${errors} errors, ${result.findings.length - errors} warnings`);
|
|
100
|
+
}
|
|
101
|
+
if (errors)
|
|
102
|
+
process.exitCode = 1;
|
|
103
|
+
}
|
|
104
|
+
catch (error) {
|
|
105
|
+
const code = error.code;
|
|
106
|
+
const message = code?.startsWith("ERR_PARSE_ARGS") ? "Invalid arguments. Run envparity --help." :
|
|
107
|
+
code ? "Unable to access project files or run the command." :
|
|
108
|
+
error instanceof Error && error.constructor === Error ? error.message : "Unable to complete the environment check.";
|
|
109
|
+
if (json)
|
|
110
|
+
output({ error: message });
|
|
111
|
+
else
|
|
112
|
+
console.error(`EnvParity: ${message}`);
|
|
113
|
+
process.exitCode = 2;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
main();
|
package/dist/env.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.readDotenv = readDotenv;
|
|
4
|
+
const node_util_1 = require("node:util");
|
|
5
|
+
function readDotenv(text) {
|
|
6
|
+
const input = text.replace(/^\uFEFF/, "").replace(/\r\n/g, "\n");
|
|
7
|
+
const declarations = [];
|
|
8
|
+
const malformed = [];
|
|
9
|
+
const values = new Map();
|
|
10
|
+
const lines = input.split("\n");
|
|
11
|
+
for (let index = 0; index < lines.length; index++) {
|
|
12
|
+
const line = index + 1;
|
|
13
|
+
const raw = lines[index].trimStart();
|
|
14
|
+
if (!raw || raw.startsWith("#"))
|
|
15
|
+
continue;
|
|
16
|
+
const assignment = /^(?:export\s+)?([A-Za-z_][A-Za-z0-9_.-]*)[ \t]*=[ \t]*/.exec(raw);
|
|
17
|
+
if (!assignment) {
|
|
18
|
+
malformed.push(line);
|
|
19
|
+
continue;
|
|
20
|
+
}
|
|
21
|
+
const key = assignment[1];
|
|
22
|
+
const rest = raw.slice(assignment[0].length);
|
|
23
|
+
let statement = raw;
|
|
24
|
+
const quote = rest[0];
|
|
25
|
+
if (quote === "'" || quote === '"' || quote === "`") {
|
|
26
|
+
let fragment = rest.slice(1);
|
|
27
|
+
while (true) {
|
|
28
|
+
const end = fragment.indexOf(quote);
|
|
29
|
+
if (end >= 0) {
|
|
30
|
+
const tail = fragment.slice(end + 1).trimStart();
|
|
31
|
+
if (tail && !tail.startsWith("#"))
|
|
32
|
+
malformed.push(line);
|
|
33
|
+
break;
|
|
34
|
+
}
|
|
35
|
+
if (index + 1 >= lines.length) {
|
|
36
|
+
malformed.push(line);
|
|
37
|
+
break;
|
|
38
|
+
}
|
|
39
|
+
fragment = lines[++index];
|
|
40
|
+
statement += "\n" + fragment;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
const parsed = (0, node_util_1.parseEnv)(statement);
|
|
44
|
+
const value = Object.hasOwn(parsed, key) ? parsed[key] : undefined;
|
|
45
|
+
if (value === undefined) {
|
|
46
|
+
malformed.push(line);
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
declarations.push({ key, line });
|
|
50
|
+
values.set(key, value);
|
|
51
|
+
}
|
|
52
|
+
return { declarations, malformed: [...new Set(malformed)], values };
|
|
53
|
+
}
|
package/dist/project.js
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.sourcePattern = exports.ruleTypes = exports.configName = void 0;
|
|
4
|
+
exports.git = git;
|
|
5
|
+
exports.discover = discover;
|
|
6
|
+
exports.inside = inside;
|
|
7
|
+
exports.readLocal = readLocal;
|
|
8
|
+
exports.config = config;
|
|
9
|
+
exports.sourceFiles = sourceFiles;
|
|
10
|
+
const node_child_process_1 = require("node:child_process");
|
|
11
|
+
const node_fs_1 = require("node:fs");
|
|
12
|
+
const node_path_1 = require("node:path");
|
|
13
|
+
const yaml_1 = require("yaml");
|
|
14
|
+
exports.configName = "envparity.yml";
|
|
15
|
+
function git(cwd, ...args) {
|
|
16
|
+
const result = (0, node_child_process_1.spawnSync)("git", args, {
|
|
17
|
+
cwd, encoding: "utf8", timeout: 5000, maxBuffer: 16 * 1024 * 1024,
|
|
18
|
+
windowsHide: true,
|
|
19
|
+
});
|
|
20
|
+
return result.status === 0 ? result.stdout : undefined;
|
|
21
|
+
}
|
|
22
|
+
function discover(cwd) {
|
|
23
|
+
const gitRoot = git(cwd, "rev-parse", "--show-toplevel")?.replace(/\r?\n$/, "");
|
|
24
|
+
const candidate = gitRoot ? (0, node_fs_1.realpathSync)(gitRoot) : cwd;
|
|
25
|
+
const suffix = (0, node_path_1.relative)(candidate, cwd);
|
|
26
|
+
const inRepository = !!gitRoot && !(0, node_path_1.isAbsolute)(suffix) && suffix !== ".." && !suffix.startsWith(".." + node_path_1.sep);
|
|
27
|
+
const root = inRepository ? candidate : cwd;
|
|
28
|
+
let project = cwd;
|
|
29
|
+
while ((0, node_path_1.relative)(root, project) && !readLocal(project, exports.configName) && !readLocal(project, "package.json")) {
|
|
30
|
+
const parent = (0, node_path_1.dirname)(project);
|
|
31
|
+
if (parent === project)
|
|
32
|
+
break;
|
|
33
|
+
project = parent;
|
|
34
|
+
}
|
|
35
|
+
let dependencies = {};
|
|
36
|
+
try {
|
|
37
|
+
const manifest = JSON.parse(readLocal(project, "package.json") ?? "{}");
|
|
38
|
+
dependencies = { ...manifest.dependencies, ...manifest.devDependencies };
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
// Never include manifest contents in a diagnostic.
|
|
42
|
+
}
|
|
43
|
+
const framework = ["next", "vite", "fastify", "express"].find((name) => name in dependencies) ?? "node";
|
|
44
|
+
return {
|
|
45
|
+
root, project, framework,
|
|
46
|
+
branch: inRepository ? git(root, "symbolic-ref", "--quiet", "--short", "HEAD")?.trim() : undefined,
|
|
47
|
+
commit: inRepository ? git(root, "rev-parse", "HEAD")?.trim() : undefined,
|
|
48
|
+
worktree: inRepository ? root : undefined,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
function inside(root, name) {
|
|
52
|
+
const path = (0, node_path_1.resolve)(root, name);
|
|
53
|
+
const suffix = (0, node_path_1.relative)(root, path);
|
|
54
|
+
if (!suffix || suffix === ".." || suffix.startsWith(".." + node_path_1.sep) || (0, node_path_1.isAbsolute)(suffix) || (0, node_path_1.isAbsolute)(name)) {
|
|
55
|
+
throw new Error("Configured paths must stay inside the project.");
|
|
56
|
+
}
|
|
57
|
+
return path;
|
|
58
|
+
}
|
|
59
|
+
function readLocal(root, name, maxBytes = 2 * 1024 * 1024) {
|
|
60
|
+
const path = inside(root, name);
|
|
61
|
+
try {
|
|
62
|
+
const stat = (0, node_fs_1.lstatSync)(path);
|
|
63
|
+
if (stat.isSymbolicLink() || (0, node_path_1.relative)(path, (0, node_fs_1.realpathSync)(path)) || !stat.isFile()) {
|
|
64
|
+
throw new Error("Expected a regular project file with no symlink components.");
|
|
65
|
+
}
|
|
66
|
+
if (stat.size > maxBytes)
|
|
67
|
+
throw new Error("Project file exceeds the supported size.");
|
|
68
|
+
return (0, node_fs_1.readFileSync)(path, "utf8");
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
if (error.code === "ENOENT")
|
|
72
|
+
return undefined;
|
|
73
|
+
throw error;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
exports.ruleTypes = ["string", "boolean", "number", "integer", "url", "email", "json", "enum"];
|
|
77
|
+
function record(value) {
|
|
78
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
79
|
+
throw new Error("Expected a mapping in envparity.yml.");
|
|
80
|
+
}
|
|
81
|
+
return value;
|
|
82
|
+
}
|
|
83
|
+
function config(project) {
|
|
84
|
+
const text = readLocal(project, exports.configName);
|
|
85
|
+
let data = { version: 1 };
|
|
86
|
+
if (text !== undefined) {
|
|
87
|
+
try {
|
|
88
|
+
data = record((0, yaml_1.parse)(text, { maxAliasCount: 0, prettyErrors: false }));
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
throw new Error("envparity.yml contains invalid YAML or an unsupported mapping.");
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
if (data.version !== 1)
|
|
95
|
+
throw new Error("envparity.yml must use version 1.");
|
|
96
|
+
const files = record(data.files ?? {});
|
|
97
|
+
const manifest = files.schema ?? (readLocal(project, ".env.example") !== undefined ? ".env.example" :
|
|
98
|
+
readLocal(project, ".env.template") !== undefined ? ".env.template" : ".env.example");
|
|
99
|
+
if (typeof manifest !== "string" || (files.local !== undefined && typeof files.local !== "string")) {
|
|
100
|
+
throw new Error("files.schema and files.local must be project-relative paths.");
|
|
101
|
+
}
|
|
102
|
+
inside(project, manifest);
|
|
103
|
+
if (typeof files.local === "string")
|
|
104
|
+
inside(project, files.local);
|
|
105
|
+
const rules = new Map();
|
|
106
|
+
for (const [key, value] of Object.entries(record(data.variables ?? {}))) {
|
|
107
|
+
if (!/^[A-Za-z_][A-Za-z0-9_.-]*$/.test(key))
|
|
108
|
+
throw new Error("Invalid variable name in schema.");
|
|
109
|
+
const rule = record(value);
|
|
110
|
+
for (const [field, setting] of Object.entries(rule)) {
|
|
111
|
+
const valid = (["required", "sensitive"].includes(field) && typeof setting === "boolean") ||
|
|
112
|
+
(field === "type" && exports.ruleTypes.some((type) => type === setting)) ||
|
|
113
|
+
(["min", "max"].includes(field) && typeof setting === "number" && Number.isFinite(setting)) ||
|
|
114
|
+
(["minLength", "maxLength"].includes(field) && typeof setting === "number" && Number.isInteger(setting) && setting >= 0) ||
|
|
115
|
+
(field === "values" && Array.isArray(setting) && setting.every((entry) => typeof entry === "string"));
|
|
116
|
+
if (!valid)
|
|
117
|
+
throw new Error("Invalid or unsupported variable rule in envparity.yml.");
|
|
118
|
+
}
|
|
119
|
+
const typed = rule;
|
|
120
|
+
if ((typed.type === "enum" && !typed.values?.length) ||
|
|
121
|
+
(typed.min !== undefined && typed.max !== undefined && typed.min > typed.max) ||
|
|
122
|
+
(typed.minLength !== undefined && typed.maxLength !== undefined && typed.minLength > typed.maxLength)) {
|
|
123
|
+
throw new Error("Invalid variable rule constraints in envparity.yml.");
|
|
124
|
+
}
|
|
125
|
+
rules.set(key, typed);
|
|
126
|
+
}
|
|
127
|
+
const validation = record(data.validation ?? {});
|
|
128
|
+
for (const [category, severity] of Object.entries(validation)) {
|
|
129
|
+
if (!["missing", "empty", "invalid", "obsolete", "undeclared", "duplicate"].includes(category) ||
|
|
130
|
+
(severity !== "error" && severity !== "warning"))
|
|
131
|
+
throw new Error("Invalid validation policy.");
|
|
132
|
+
}
|
|
133
|
+
return { manifest, local: files.local, rules, validation };
|
|
134
|
+
}
|
|
135
|
+
exports.sourcePattern = /\.(?:[cm]?[jt]s|[jt]sx)$/i;
|
|
136
|
+
const ignoredDirectories = new Set([".git", "node_modules", ".next", "dist", "build", "coverage", ".turbo", ".vercel"]);
|
|
137
|
+
function sourceFiles(project) {
|
|
138
|
+
const tracked = git(project, "ls-files", "-z", "--cached", "--others", "--exclude-standard", "--", ".");
|
|
139
|
+
if (tracked !== undefined) {
|
|
140
|
+
return [...new Set(tracked.split("\0"))].filter((name) => exports.sourcePattern.test(name) && !name.split("/").some((part) => ignoredDirectories.has(part)));
|
|
141
|
+
}
|
|
142
|
+
const files = [];
|
|
143
|
+
function walk(folder) {
|
|
144
|
+
for (const entry of (0, node_fs_1.readdirSync)(folder, { withFileTypes: true })) {
|
|
145
|
+
const path = (0, node_path_1.join)(folder, entry.name);
|
|
146
|
+
if (entry.isDirectory() && !ignoredDirectories.has(entry.name))
|
|
147
|
+
walk(path);
|
|
148
|
+
if (entry.isFile() && exports.sourcePattern.test(entry.name))
|
|
149
|
+
files.push((0, node_path_1.relative)(project, path));
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
walk(project);
|
|
153
|
+
return files;
|
|
154
|
+
}
|
package/dist/report.js
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.report = report;
|
|
4
|
+
const analyze_1 = require("./analyze");
|
|
5
|
+
const env_1 = require("./env");
|
|
6
|
+
const project_1 = require("./project");
|
|
7
|
+
function layers(framework, mode) {
|
|
8
|
+
if (framework === "next") {
|
|
9
|
+
return [".env", `.env.${mode}`, ...(mode === "test" ? [] : [".env.local"]), `.env.${mode}.local`];
|
|
10
|
+
}
|
|
11
|
+
if (framework === "vite")
|
|
12
|
+
return [".env", ".env.local", `.env.${mode}`, `.env.${mode}.local`];
|
|
13
|
+
return [".env", `.env.${mode}`, ".env.local", `.env.${mode}.local`];
|
|
14
|
+
}
|
|
15
|
+
function matches(value, rule) {
|
|
16
|
+
if ((rule.minLength !== undefined && value.length < rule.minLength) ||
|
|
17
|
+
(rule.maxLength !== undefined && value.length > rule.maxLength))
|
|
18
|
+
return false;
|
|
19
|
+
if (rule.type === "number" || rule.type === "integer" || rule.min !== undefined || rule.max !== undefined) {
|
|
20
|
+
const number = Number(value);
|
|
21
|
+
if (!/^[+-]?(?:\d+\.?\d*|\.\d+)(?:e[+-]?\d+)?$/i.test(value) || !Number.isFinite(number))
|
|
22
|
+
return false;
|
|
23
|
+
if (rule.type === "integer" && !Number.isInteger(number))
|
|
24
|
+
return false;
|
|
25
|
+
if (rule.min !== undefined && number < rule.min)
|
|
26
|
+
return false;
|
|
27
|
+
if (rule.max !== undefined && number > rule.max)
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
switch (rule.type) {
|
|
31
|
+
case "boolean": return /^(true|false)$/i.test(value);
|
|
32
|
+
case "url": return URL.canParse(value);
|
|
33
|
+
case "email": return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value);
|
|
34
|
+
case "enum": return rule.values?.includes(value) ?? false;
|
|
35
|
+
case "json":
|
|
36
|
+
try {
|
|
37
|
+
JSON.parse(value);
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
default: return true;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
function report(cwd, mode, includeSource) {
|
|
47
|
+
const context = (0, project_1.discover)(cwd);
|
|
48
|
+
const specification = (0, project_1.config)(context.project);
|
|
49
|
+
const findings = [];
|
|
50
|
+
function add(type, file, resolution, key, line, severity = "error", source = "configuration") {
|
|
51
|
+
const policy = specification.validation[type.toLowerCase()];
|
|
52
|
+
findings.push({
|
|
53
|
+
type, file, resolution, key, line, source,
|
|
54
|
+
severity: policy === "error" || policy === "warning" ? policy : severity,
|
|
55
|
+
automaticRemediation: false,
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
const rules = new Map();
|
|
59
|
+
const effective = new Map();
|
|
60
|
+
const candidates = specification.local ? [specification.local] : layers(context.framework, mode);
|
|
61
|
+
const loaded = [];
|
|
62
|
+
function load(file) {
|
|
63
|
+
const text = (0, project_1.readLocal)(context.project, file);
|
|
64
|
+
if (text === undefined)
|
|
65
|
+
return undefined;
|
|
66
|
+
const parsed = (0, env_1.readDotenv)(text);
|
|
67
|
+
const seen = new Set();
|
|
68
|
+
for (const line of parsed.malformed)
|
|
69
|
+
add("MALFORMED", file, "Correct the dotenv declaration.", undefined, line);
|
|
70
|
+
for (const declaration of parsed.declarations) {
|
|
71
|
+
if (seen.has(declaration.key)) {
|
|
72
|
+
add("DUPLICATE", file, "Keep one declaration in this file.", declaration.key, declaration.line, "warning");
|
|
73
|
+
}
|
|
74
|
+
seen.add(declaration.key);
|
|
75
|
+
}
|
|
76
|
+
return parsed;
|
|
77
|
+
}
|
|
78
|
+
const manifest = load(specification.manifest);
|
|
79
|
+
for (const { key } of manifest?.declarations ?? [])
|
|
80
|
+
rules.set(key, {});
|
|
81
|
+
for (const [key, rule] of specification.rules)
|
|
82
|
+
rules.set(key, rule);
|
|
83
|
+
if (!manifest && !rules.size)
|
|
84
|
+
add("UNKNOWN", specification.manifest, "Create a key-only manifest or configure variable rules.", undefined, undefined, "warning");
|
|
85
|
+
for (const file of candidates) {
|
|
86
|
+
const parsed = load(file);
|
|
87
|
+
if (!parsed)
|
|
88
|
+
continue;
|
|
89
|
+
loaded.push(file);
|
|
90
|
+
for (const { key, line } of parsed.declarations) {
|
|
91
|
+
const value = parsed.values.get(key);
|
|
92
|
+
if (value !== undefined)
|
|
93
|
+
effective.set(key, { value, file, line });
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
let valid = 0;
|
|
97
|
+
for (const [key, rule] of rules) {
|
|
98
|
+
const actual = effective.get(key);
|
|
99
|
+
if (!actual) {
|
|
100
|
+
if (rule.required !== false)
|
|
101
|
+
add("MISSING", candidates.at(-1), "Set this key in the active local environment.", key);
|
|
102
|
+
}
|
|
103
|
+
else if (!actual.value.trim() && rule.required !== false) {
|
|
104
|
+
add("EMPTY", actual.file, "Supply a non-empty local value.", key, actual.line);
|
|
105
|
+
}
|
|
106
|
+
else if (/\$\{[^}]+\}|\$[A-Za-z_][A-Za-z0-9_]*/.test(actual.value)) {
|
|
107
|
+
add("UNKNOWN", actual.file, "Verify the value after framework-specific expansion.", key, actual.line, "warning");
|
|
108
|
+
}
|
|
109
|
+
else if (actual.value && !matches(actual.value, rule)) {
|
|
110
|
+
add("INVALID", actual.file, "Set a value that satisfies the schema constraints.", key, actual.line);
|
|
111
|
+
}
|
|
112
|
+
else
|
|
113
|
+
valid++;
|
|
114
|
+
}
|
|
115
|
+
if (includeSource) {
|
|
116
|
+
const analysis = (0, analyze_1.scan)(context.project);
|
|
117
|
+
for (const file of analysis.skipped) {
|
|
118
|
+
add("UNKNOWN", file, "Review the skipped source file manually.", undefined, undefined, "warning", "source");
|
|
119
|
+
}
|
|
120
|
+
for (const { key, file, line } of analysis.references) {
|
|
121
|
+
if (!key)
|
|
122
|
+
add("UNKNOWN", file, "Document the keys used by this dynamic reference.", key, line, "warning", "source");
|
|
123
|
+
else if (!rules.has(key))
|
|
124
|
+
add("UNDECLARED", file, "Declare this key in the schema if the application requires it.", key, line, "warning", "source");
|
|
125
|
+
if (key && /^(NEXT_PUBLIC_|VITE_|PUBLIC_)/.test(key) &&
|
|
126
|
+
(rules.get(key)?.sensitive || /SECRET|PRIVATE_KEY|PASSWORD|TOKEN/i.test(key))) {
|
|
127
|
+
add("PUBLIC_EXPOSURE", file, "Review whether this variable can be exposed to browser code.", key, line, "warning", "source");
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return { ...context, mode, manifest: specification.manifest, files: loaded, valid, findings };
|
|
132
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,27 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@shubhammaji1/envparity",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@shubhammaji1/envparity",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Offline environment configuration checks for Git repositories",
|
|
5
|
+
"engines": {
|
|
6
|
+
"node": ">=22.15.0"
|
|
7
|
+
},
|
|
8
|
+
"bin": {
|
|
9
|
+
"envparity": "./dist/cli.js"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"dist"
|
|
13
|
+
],
|
|
14
|
+
"scripts": {
|
|
15
|
+
"build": "tsc",
|
|
16
|
+
"typecheck": "tsc --noEmit",
|
|
17
|
+
"lint": "tsc --noEmit --noUnusedLocals --noUnusedParameters",
|
|
18
|
+
"test": "node --test"
|
|
19
|
+
},
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"typescript": "5.9.3",
|
|
22
|
+
"yaml": "2.8.0"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@types/node": "22.18.11"
|
|
26
|
+
}
|
|
27
|
+
}
|