@pgomes13/drift-guard 2.6.2

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/index.d.ts ADDED
@@ -0,0 +1,55 @@
1
+ export type Severity = "breaking" | "non-breaking" | "info";
2
+
3
+ export interface Change {
4
+ type: string;
5
+ severity: Severity;
6
+ path: string;
7
+ method: string;
8
+ location: string;
9
+ description: string;
10
+ before?: string;
11
+ after?: string;
12
+ }
13
+
14
+ export interface Summary {
15
+ total: number;
16
+ breaking: number;
17
+ non_breaking: number;
18
+ info: number;
19
+ }
20
+
21
+ export interface DiffResult {
22
+ base_file: string;
23
+ head_file: string;
24
+ changes: Change[];
25
+ summary: Summary;
26
+ }
27
+
28
+ export interface Hit {
29
+ file: string;
30
+ line_num: number;
31
+ line: string;
32
+ change_type: string;
33
+ change_path: string;
34
+ }
35
+
36
+ export interface ImpactOptions {
37
+ format?: "text" | "json" | "markdown";
38
+ }
39
+
40
+ /** Diff two OpenAPI 3.x schemas (YAML or JSON). */
41
+ export function compareOpenAPI(base: string, head: string): DiffResult;
42
+
43
+ /** Diff two GraphQL SDL schemas (.graphql or .gql). */
44
+ export function compareGraphQL(base: string, head: string): DiffResult;
45
+
46
+ /** Diff two Protobuf schemas (.proto). */
47
+ export function compareGRPC(base: string, head: string): DiffResult;
48
+
49
+ /**
50
+ * Scan a source directory for references to breaking changes.
51
+ * Returns Hit[] when format is "json" (default), otherwise a formatted string.
52
+ */
53
+ export function impact(diffResult: DiffResult, scanDir?: string, options?: ImpactOptions & { format: "json" }): Hit[];
54
+ export function impact(diffResult: DiffResult, scanDir?: string, options?: ImpactOptions & { format: "text" | "markdown" }): string;
55
+ export function impact(diffResult: DiffResult, scanDir?: string, options?: ImpactOptions): Hit[] | string;
package/index.js ADDED
@@ -0,0 +1,86 @@
1
+ "use strict";
2
+ // Programmatic API — wraps the drift-guard binary and returns parsed results.
3
+
4
+ const path = require("path");
5
+ const { spawnSync } = require("child_process");
6
+
7
+ const BIN = path.join(
8
+ __dirname,
9
+ "bin",
10
+ process.platform === "win32" ? "drift-guard.exe" : "drift-guard"
11
+ );
12
+
13
+ /**
14
+ * Run the drift-guard binary and return its stdout.
15
+ * @param {string[]} args
16
+ * @returns {string}
17
+ */
18
+ function run(args) {
19
+ const result = spawnSync(BIN, args, { encoding: "utf8" });
20
+ if (result.error) {
21
+ throw new Error(`drift-guard binary error: ${result.error.message}`);
22
+ }
23
+ if (result.status !== 0 && result.status !== 1) {
24
+ // exit 1 means breaking changes found (--fail-on-breaking), not a crash
25
+ throw new Error(`drift-guard exited with code ${result.status}: ${result.stderr}`);
26
+ }
27
+ return result.stdout;
28
+ }
29
+
30
+ /**
31
+ * Diff two OpenAPI 3.x schemas.
32
+ * @param {string} base Path to the base (old) schema file
33
+ * @param {string} head Path to the head (new) schema file
34
+ * @returns {DiffResult}
35
+ */
36
+ function compareOpenAPI(base, head) {
37
+ return JSON.parse(run(["openapi", "--base", base, "--head", head, "--format", "json"]));
38
+ }
39
+
40
+ /**
41
+ * Diff two GraphQL SDL schemas.
42
+ * @param {string} base
43
+ * @param {string} head
44
+ * @returns {DiffResult}
45
+ */
46
+ function compareGraphQL(base, head) {
47
+ return JSON.parse(run(["graphql", "--base", base, "--head", head, "--format", "json"]));
48
+ }
49
+
50
+ /**
51
+ * Diff two Protobuf schemas.
52
+ * @param {string} base
53
+ * @param {string} head
54
+ * @returns {DiffResult}
55
+ */
56
+ function compareGRPC(base, head) {
57
+ return JSON.parse(run(["grpc", "--base", base, "--head", head, "--format", "json"]));
58
+ }
59
+
60
+ /**
61
+ * Scan a directory for source references to breaking changes in a diff result.
62
+ * @param {DiffResult} diffResult
63
+ * @param {string} scanDir Directory to scan (default ".")
64
+ * @param {{ format?: "text"|"json"|"markdown" }} [options]
65
+ * @returns {Hit[]|string} Array of hits when format="json", otherwise formatted string
66
+ */
67
+ function impact(diffResult, scanDir = ".", options = {}) {
68
+ const { format = "json" } = options;
69
+ const fs = require("fs");
70
+ const os = require("os");
71
+
72
+ // Write the diff result to a temp file so we can pass --diff
73
+ const tmp = require("path").join(os.tmpdir(), `drift-guard-diff-${Date.now()}.json`);
74
+ try {
75
+ fs.writeFileSync(tmp, JSON.stringify(diffResult));
76
+ const out = run(["impact", "--diff", tmp, "--scan", scanDir, "--format", format]);
77
+ if (format === "json") {
78
+ return JSON.parse(out);
79
+ }
80
+ return out;
81
+ } finally {
82
+ fs.rmSync(tmp, { force: true });
83
+ }
84
+ }
85
+
86
+ module.exports = { compareOpenAPI, compareGraphQL, compareGRPC, impact };
package/install.js ADDED
@@ -0,0 +1,97 @@
1
+ #!/usr/bin/env node
2
+ // postinstall: downloads the drift-guard binary from GitHub Releases.
3
+ "use strict";
4
+
5
+ const https = require("https");
6
+ const fs = require("fs");
7
+ const os = require("os");
8
+ const path = require("path");
9
+ const { execSync } = require("child_process");
10
+
11
+ const VERSION = require("./package.json").version;
12
+ const REPO = "pgomes13/drift-guard-engine";
13
+ const BIN_DIR = path.join(__dirname, "bin");
14
+ const BIN_PATH = path.join(BIN_DIR, process.platform === "win32" ? "drift-guard.exe" : "drift-guard");
15
+
16
+ // Map Node.js platform/arch → goreleaser archive naming
17
+ function getPlatformInfo() {
18
+ const p = process.platform;
19
+ const a = process.arch;
20
+
21
+ const osMap = { linux: "linux", darwin: "darwin", win32: "windows" };
22
+ const archMap = { x64: "amd64", arm64: "arm64" };
23
+
24
+ const goos = osMap[p];
25
+ const goarch = archMap[a];
26
+
27
+ if (!goos || !goarch) {
28
+ throw new Error(`Unsupported platform: ${p}/${a}`);
29
+ }
30
+ if (goos === "windows" && goarch === "arm64") {
31
+ throw new Error("Windows arm64 is not supported");
32
+ }
33
+
34
+ const ext = goos === "windows" ? "zip" : "tar.gz";
35
+ const archive = `drift-guard_${VERSION}_${goos}_${goarch}.${ext}`;
36
+ const url = `https://github.com/${REPO}/releases/download/v${VERSION}/${archive}`;
37
+
38
+ return { url, archive, ext };
39
+ }
40
+
41
+ function download(url, dest) {
42
+ return new Promise((resolve, reject) => {
43
+ const file = fs.createWriteStream(dest);
44
+ function get(u) {
45
+ https.get(u, (res) => {
46
+ if (res.statusCode === 301 || res.statusCode === 302) {
47
+ return get(res.headers.location);
48
+ }
49
+ if (res.statusCode !== 200) {
50
+ reject(new Error(`Download failed: HTTP ${res.statusCode} for ${u}`));
51
+ return;
52
+ }
53
+ res.pipe(file);
54
+ file.on("finish", () => file.close(resolve));
55
+ }).on("error", reject);
56
+ }
57
+ get(url);
58
+ });
59
+ }
60
+
61
+ async function install() {
62
+ if (fs.existsSync(BIN_PATH)) {
63
+ return; // already installed (e.g. cached node_modules)
64
+ }
65
+
66
+ const { url, archive, ext } = getPlatformInfo();
67
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "drift-guard-"));
68
+ const archivePath = path.join(tmpDir, archive);
69
+
70
+ process.stdout.write(`Downloading drift-guard v${VERSION}...\n`);
71
+
72
+ try {
73
+ await download(url, archivePath);
74
+
75
+ fs.mkdirSync(BIN_DIR, { recursive: true });
76
+
77
+ if (ext === "tar.gz") {
78
+ execSync(`tar -xzf "${archivePath}" -C "${tmpDir}"`);
79
+ } else {
80
+ execSync(`unzip -o "${archivePath}" -d "${tmpDir}"`);
81
+ }
82
+
83
+ const extracted = path.join(tmpDir, process.platform === "win32" ? "drift-guard.exe" : "drift-guard");
84
+ fs.copyFileSync(extracted, BIN_PATH);
85
+ fs.chmodSync(BIN_PATH, 0o755);
86
+
87
+ process.stdout.write(`drift-guard installed to ${BIN_PATH}\n`);
88
+ } finally {
89
+ fs.rmSync(tmpDir, { recursive: true, force: true });
90
+ }
91
+ }
92
+
93
+ install().catch((err) => {
94
+ process.stderr.write(`drift-guard install failed: ${err.message}\n`);
95
+ process.stderr.write("You can install it manually: https://github.com/pgomes13/drift-guard-engine/releases\n");
96
+ // Do not exit(1) — allow npm install to succeed even if binary download fails.
97
+ });
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "@pgomes13/drift-guard",
3
+ "version": "2.6.2",
4
+ "description": "API schema diff engine — detect breaking changes in OpenAPI, GraphQL, and gRPC schemas",
5
+ "main": "index.js",
6
+ "types": "index.d.ts",
7
+ "bin": {
8
+ "drift-guard": "run.js"
9
+ },
10
+ "scripts": {
11
+ "postinstall": "node install.js"
12
+ },
13
+ "files": [
14
+ "index.js",
15
+ "index.d.ts",
16
+ "run.js",
17
+ "install.js",
18
+ "bin/"
19
+ ],
20
+ "keywords": [
21
+ "api",
22
+ "openapi",
23
+ "graphql",
24
+ "grpc",
25
+ "schema",
26
+ "diff",
27
+ "breaking-changes",
28
+ "drift"
29
+ ],
30
+ "author": "pgomes13",
31
+ "license": "MIT",
32
+ "repository": {
33
+ "type": "git",
34
+ "url": "https://github.com/pgomes13/drift-guard-engine"
35
+ },
36
+ "homepage": "https://github.com/pgomes13/drift-guard-engine",
37
+ "engines": {
38
+ "node": ">=16"
39
+ }
40
+ }
package/run.js ADDED
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env node
2
+ // Thin shim: passes all arguments to the drift-guard binary.
3
+ "use strict";
4
+
5
+ const path = require("path");
6
+ const { spawnSync } = require("child_process");
7
+
8
+ const bin = path.join(
9
+ __dirname,
10
+ "bin",
11
+ process.platform === "win32" ? "drift-guard.exe" : "drift-guard"
12
+ );
13
+
14
+ const result = spawnSync(bin, process.argv.slice(2), { stdio: "inherit" });
15
+
16
+ if (result.error) {
17
+ process.stderr.write(
18
+ `drift-guard: could not run binary: ${result.error.message}\n` +
19
+ `Make sure the package installed correctly or download manually:\n` +
20
+ `https://github.com/pgomes13/drift-guard-engine/releases\n`
21
+ );
22
+ process.exit(1);
23
+ }
24
+
25
+ process.exit(result.status ?? 0);