@shubhammaji1/envparity 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 ADDED
@@ -0,0 +1,28 @@
1
+ # EnvParity
2
+
3
+ Check a Git project's environment files against its declared requirements.
4
+ Reports show key names and keep environment values out of the output.
5
+
6
+ Requires Node.js 22.15 or newer and Git for repository hooks.
7
+
8
+ ```sh
9
+ npm install -g @shubhammaji1/envparity@0.2.0
10
+ envparity doctor
11
+ envparity hook install
12
+ ```
13
+
14
+ The opt-in Git guard checks the repository root after branch checkouts, merges,
15
+ and rebases. It warns about missing, empty, and invalid development configuration
16
+ without changing dotenv files or blocking Git.
17
+
18
+ Use `envparity hook status` and `envparity hook uninstall` to manage the guard.
19
+ Existing hooks are preserved. To integrate an existing shell hook or custom
20
+ `core.hooksPath`, add the appropriate check to its successful path, for example:
21
+
22
+ ```sh
23
+ envparity hook check post-checkout "$@"
24
+ ```
25
+
26
+ This example is for `post-checkout`. Replace the event with `post-merge` or
27
+ `post-rewrite` in the corresponding hook. Reinstall managed hooks if the Node
28
+ or CLI installation path moves.
package/dist/cli.js CHANGED
@@ -5,6 +5,7 @@ const node_fs_1 = require("node:fs");
5
5
  const node_path_1 = require("node:path");
6
6
  const node_util_1 = require("node:util");
7
7
  const yaml_1 = require("yaml");
8
+ const hooks_1 = require("./hooks");
8
9
  const project_1 = require("./project");
9
10
  const report_1 = require("./report");
10
11
  const help = `EnvParity — Git-aware environment checks
@@ -17,6 +18,7 @@ Usage: envparity <command> [--json] [--mode <environment>]
17
18
  validate Validate environment files against the schema
18
19
  scan List source analysis findings
19
20
  status Show environment health and findings
21
+ hook Install, inspect, or uninstall Git guards
20
22
  --help Print this help
21
23
  --version Print the package version
22
24
 
@@ -56,6 +58,28 @@ function main() {
56
58
  return;
57
59
  }
58
60
  const [command] = positionals;
61
+ if (command === "hook") {
62
+ const cwd = (0, node_fs_1.realpathSync)(process.cwd());
63
+ const [, action, event, ...args] = positionals;
64
+ if (action === "check") {
65
+ (0, hooks_1.checkHook)(cwd, event, args);
66
+ return;
67
+ }
68
+ if (!action || positionals.length !== 2) {
69
+ throw new Error("Use hook install, hook status, or hook uninstall.");
70
+ }
71
+ const result = (0, hooks_1.manageHooks)(cwd, action);
72
+ if (json)
73
+ output(result);
74
+ else {
75
+ for (const { event, state, needsRefresh } of result.hooks) {
76
+ console.log(`${event}: ${state}${needsRefresh ? " (run hook uninstall, then hook install to refresh)" : ""}`);
77
+ if (state === "existing")
78
+ console.log(` Existing hook kept. To add checks, call envparity hook check ${event} "$@" in that hook.`);
79
+ }
80
+ }
81
+ return;
82
+ }
59
83
  if (positionals.length !== 1 || !["init", "doctor", "diff", "validate", "scan", "status"].includes(command)) {
60
84
  throw new Error("Unknown command or unexpected arguments. Run envparity --help.");
61
85
  }
package/dist/hooks.js ADDED
@@ -0,0 +1,104 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.manageHooks = manageHooks;
4
+ exports.checkHook = checkHook;
5
+ const node_fs_1 = require("node:fs");
6
+ const node_path_1 = require("node:path");
7
+ const project_1 = require("./project");
8
+ const report_1 = require("./report");
9
+ const events = ["post-checkout", "post-merge", "post-rewrite"];
10
+ const actions = ["install", "status", "uninstall"];
11
+ function inspect(path) {
12
+ try {
13
+ return (0, node_fs_1.lstatSync)(path);
14
+ }
15
+ catch (error) {
16
+ if (error.code === "ENOENT")
17
+ return undefined;
18
+ throw error;
19
+ }
20
+ }
21
+ function gitLine(cwd, ...args) {
22
+ return (0, project_1.git)(cwd, ...args)?.replace(/\r?\n$/, "");
23
+ }
24
+ function shellQuote(path) {
25
+ const portable = process.platform === "win32" ? path.replaceAll("\\", "/") : path;
26
+ if (/[\r\n]/.test(portable))
27
+ throw new Error("Git hooks require CLI paths without line breaks.");
28
+ return "'" + portable.replaceAll("'", "'\"'\"'") + "'";
29
+ }
30
+ function script(event) {
31
+ const executable = shellQuote(process.execPath);
32
+ const cli = shellQuote((0, node_fs_1.realpathSync)(process.argv[1]));
33
+ return [
34
+ "#!/bin/sh",
35
+ "# EnvParity managed Git hook v1",
36
+ `${executable} ${cli} hook check ${event} "$@" || printf '%s\\n' 'EnvParity: hook check failed. Run envparity hook install to refresh it.' >&2`,
37
+ "exit 0",
38
+ "",
39
+ ].join("\n");
40
+ }
41
+ const managedScript = /^#!\/bin\/sh\n# EnvParity managed Git hook v1\n'(?:[^'\n]|'"'"')*' '(?:[^'\n]|'"'"')*' hook check (post-checkout|post-merge|post-rewrite) "\$@" \|\| printf '%s\\n' 'EnvParity: hook check failed\. Run envparity hook install to refresh it\.' >&2\nexit 0\n$/;
42
+ function manageHooks(cwd, action) {
43
+ if (!actions.includes(action))
44
+ throw new Error("Use hook install, hook status, or hook uninstall.");
45
+ if (gitLine(cwd, "rev-parse", "--is-inside-work-tree") !== "true") {
46
+ throw new Error("Run this command inside a Git worktree.");
47
+ }
48
+ const config = (0, project_1.git)(cwd, "config", "--get", "core.hooksPath");
49
+ if (config !== undefined) {
50
+ throw new Error("core.hooksPath is configured. Add `envparity hook check <event> \"$@\"` to your existing hook's successful path.");
51
+ }
52
+ const common = gitLine(cwd, "rev-parse", "--path-format=absolute", "--git-common-dir");
53
+ if (!common)
54
+ throw new Error("Unable to locate the Git metadata directory.");
55
+ const directory = (0, node_path_1.join)((0, node_fs_1.realpathSync)(common), "hooks");
56
+ const directoryInfo = inspect(directory);
57
+ if (directoryInfo && (!directoryInfo.isDirectory() || (0, node_path_1.relative)(directory, (0, node_fs_1.realpathSync)(directory)))) {
58
+ throw new Error("Expected a real Git hooks directory.");
59
+ }
60
+ if (action === "install" && !directoryInfo)
61
+ (0, node_fs_1.mkdirSync)(directory);
62
+ const hooks = events.map((event) => {
63
+ const path = (0, node_path_1.join)(directory, event);
64
+ const info = inspect(path);
65
+ const contents = info?.isFile() && info.size <= 8192 ? (0, node_fs_1.readFileSync)(path, "utf8") : undefined;
66
+ const expected = script(event);
67
+ const state = !info ? "absent" :
68
+ contents && managedScript.exec(contents)?.[1] === event ? "installed" : "existing";
69
+ if (action === "install" && state === "absent") {
70
+ (0, node_fs_1.writeFileSync)(path, expected, { mode: 0o755, flag: "wx" });
71
+ return { event, state: "installed", needsRefresh: false };
72
+ }
73
+ if (action === "uninstall" && state === "installed") {
74
+ (0, node_fs_1.unlinkSync)(path);
75
+ return { event, state: "absent", needsRefresh: false };
76
+ }
77
+ return { event, state, needsRefresh: state === "installed" && contents !== expected };
78
+ });
79
+ return { hooks };
80
+ }
81
+ function checkHook(cwd, event, args) {
82
+ if (!events.some((name) => name === event))
83
+ throw new Error("Unknown Git hook event.");
84
+ if ((event === "post-checkout" && args[2] !== "1") ||
85
+ (event === "post-rewrite" && args[0] !== "rebase"))
86
+ return;
87
+ try {
88
+ const { findings } = (0, report_1.report)(cwd, "development", false);
89
+ for (const { type, key } of findings) {
90
+ if (type === "MISSING" && key)
91
+ console.error(`EnvParity: Missing required key: ${key}`);
92
+ if (type === "EMPTY" && key)
93
+ console.error(`EnvParity: Empty required key: ${key}`);
94
+ if (type === "INVALID" && key)
95
+ console.error(`EnvParity: Invalid value for key: ${key}`);
96
+ }
97
+ if (findings.some(({ type }) => type === "MALFORMED")) {
98
+ console.error("EnvParity: Invalid dotenv declaration. Run envparity validate for details.");
99
+ }
100
+ }
101
+ catch {
102
+ console.error("EnvParity: Unable to validate the current environment. Run envparity validate for details.");
103
+ }
104
+ }
package/dist/report.js CHANGED
@@ -1,7 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.report = report;
4
- const analyze_1 = require("./analyze");
5
4
  const env_1 = require("./env");
6
5
  const project_1 = require("./project");
7
6
  function layers(framework, mode) {
@@ -113,7 +112,8 @@ function report(cwd, mode, includeSource) {
113
112
  valid++;
114
113
  }
115
114
  if (includeSource) {
116
- const analysis = (0, analyze_1.scan)(context.project);
115
+ const { scan } = require("./analyze");
116
+ const analysis = scan(context.project);
117
117
  for (const file of analysis.skipped) {
118
118
  add("UNKNOWN", file, "Review the skipped source file manually.", undefined, undefined, "warning", "source");
119
119
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shubhammaji1/envparity",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Offline environment configuration checks for Git repositories",
5
5
  "engines": { "node": ">=22.15.0" },
6
6
  "bin": { "envparity": "./dist/cli.js" },