@ogcio/o11y-validator 0.2.1 → 0.2.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/lib/rules.mjs +47 -3
- package/package.json +1 -1
package/lib/rules.mjs
CHANGED
|
@@ -7,15 +7,18 @@
|
|
|
7
7
|
// NOTE: if the rules directory is relocated in a future release, update
|
|
8
8
|
// RULES_SUBPATH to match.
|
|
9
9
|
|
|
10
|
+
import { execFileSync } from "node:child_process";
|
|
10
11
|
import { existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
11
12
|
import { dirname, join } from "node:path";
|
|
12
13
|
import { fileURLToPath } from "node:url";
|
|
13
14
|
|
|
14
15
|
const pkgRoot = join(dirname(fileURLToPath(import.meta.url)), "..");
|
|
16
|
+
let cachedToken;
|
|
15
17
|
|
|
16
18
|
export const pkg = JSON.parse(readFileSync(join(pkgRoot, "package.json"), "utf8"));
|
|
17
19
|
export const RULES_SUBPATH = "o11y-validator/rules";
|
|
18
|
-
|
|
20
|
+
// release-please tags as "<name>@v<version>".
|
|
21
|
+
export const RULES_REF = `${pkg.name}@v${pkg.version}`;
|
|
19
22
|
|
|
20
23
|
export function rulesCacheDir() {
|
|
21
24
|
// Cache inside the installed package, alongside the binary, scoped to the version.
|
|
@@ -39,10 +42,51 @@ function ghRepo() {
|
|
|
39
42
|
return `${match[1]}/${match[2]}`;
|
|
40
43
|
}
|
|
41
44
|
|
|
45
|
+
function tokenFromGhCli() {
|
|
46
|
+
try {
|
|
47
|
+
return execFileSync("gh", ["auth", "token"], {
|
|
48
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
49
|
+
encoding: "utf8",
|
|
50
|
+
}).trim();
|
|
51
|
+
} catch {
|
|
52
|
+
return "";
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function tokenFromGitCredential() {
|
|
57
|
+
try {
|
|
58
|
+
// Ask git's configured credential helper github.com credential.
|
|
59
|
+
// GIT_TERMINAL_PROMPT=0 makes it fail instead of prompting interactively when nothing is stored.
|
|
60
|
+
const out = execFileSync("git", ["credential", "fill"], {
|
|
61
|
+
input: "protocol=https\nhost=github.com\n\n",
|
|
62
|
+
stdio: ["pipe", "pipe", "ignore"],
|
|
63
|
+
encoding: "utf8",
|
|
64
|
+
env: { ...process.env, GIT_TERMINAL_PROMPT: "0" },
|
|
65
|
+
});
|
|
66
|
+
return out.match(/^password=(.+)$/m)?.[1].trim() || "";
|
|
67
|
+
} catch {
|
|
68
|
+
return "";
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function resolveToken() {
|
|
73
|
+
if (cachedToken !== undefined) return cachedToken;
|
|
74
|
+
// Prefer explicit env tokens, then fall back to the user's existing GitHub
|
|
75
|
+
// auth (gh CLI, then git credential helper) so the package works as a drop-in
|
|
76
|
+
// without requiring a token at invocation time.
|
|
77
|
+
cachedToken =
|
|
78
|
+
process.env.GITHUB_TOKEN ||
|
|
79
|
+
process.env.GH_TOKEN ||
|
|
80
|
+
process.env.NODE_AUTH_TOKEN ||
|
|
81
|
+
tokenFromGhCli() ||
|
|
82
|
+
tokenFromGitCredential() ||
|
|
83
|
+
"";
|
|
84
|
+
return cachedToken;
|
|
85
|
+
}
|
|
86
|
+
|
|
42
87
|
function ghHeaders(accept = "application/vnd.github+json") {
|
|
43
88
|
const headers = { "user-agent": "o11y-validator-cli", accept };
|
|
44
|
-
|
|
45
|
-
const token = process.env.GITHUB_TOKEN || process.env.NODE_AUTH_TOKEN;
|
|
89
|
+
const token = resolveToken();
|
|
46
90
|
if (token) headers.authorization = `Bearer ${token}`;
|
|
47
91
|
return headers;
|
|
48
92
|
}
|