@primitive.ai/prim 0.1.0-alpha.61 → 0.1.0-alpha.62

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.
@@ -1,166 +0,0 @@
1
- // src/lib/activation.ts
2
- import { execFileSync as execFileSync2 } from "child_process";
3
- import { existsSync, readFileSync } from "fs";
4
- import { homedir } from "os";
5
- import { join } from "path";
6
-
7
- // src/lib/git.ts
8
- import { execFileSync } from "child_process";
9
- import { lstatSync, realpathSync } from "fs";
10
- import { basename, dirname, isAbsolute, relative, resolve, sep } from "path";
11
- function gitToplevel(cwd) {
12
- try {
13
- return execFileSync("git", ["rev-parse", "--show-toplevel"], {
14
- cwd,
15
- encoding: "utf-8",
16
- stdio: ["ignore", "pipe", "ignore"]
17
- }).trim();
18
- } catch {
19
- return null;
20
- }
21
- }
22
- function gitValue(cwd, args) {
23
- try {
24
- const value = execFileSync("git", args, {
25
- cwd,
26
- encoding: "utf-8",
27
- stdio: ["ignore", "pipe", "ignore"]
28
- }).trim();
29
- return value || null;
30
- } catch {
31
- return null;
32
- }
33
- }
34
- function githubRepositoryFullName(cwd) {
35
- const remote = gitValue(cwd, ["config", "--get", "remote.origin.url"]);
36
- if (!remote || [...remote].some((char) => char < " " || char === "\x7F")) return null;
37
- const match = /^(?:git@github\.com:([^?#]+)|(?:https|ssh):\/\/(?:[^/@]+@)?github\.com(?::\d+)?\/([^?#]+))$/i.exec(
38
- remote
39
- );
40
- const path = match?.[1] ?? match?.[2];
41
- if (!path) return null;
42
- const parts = path.replace(/^\/+|\/+$/g, "").replace(/\.git$/i, "").split("/");
43
- if (parts.length !== 2 || parts.some((part) => !part || part !== part.trim())) return null;
44
- return `${parts[0]}/${parts[1]}`;
45
- }
46
- function canonicalGitRoot(cwd) {
47
- try {
48
- const top = gitToplevel(cwd);
49
- return top ? realpathSync.native(top) : null;
50
- } catch {
51
- return null;
52
- }
53
- }
54
- function canonicalRepositoryPath(filePath, cwd, repositoryRoot = canonicalGitRoot(cwd)) {
55
- if (!repositoryRoot || !filePath || [...filePath].some((char) => char < " " || char === "\x7F" || "~*?[]{}$`\\".includes(char)))
56
- return null;
57
- const absolute = resolve(cwd, filePath);
58
- let target;
59
- try {
60
- const stat = lstatSync(absolute);
61
- if (!stat.isFile() || stat.isSymbolicLink()) return null;
62
- target = realpathSync.native(absolute);
63
- } catch (error) {
64
- if (!(error instanceof Error) || error.code !== "ENOENT") {
65
- return null;
66
- }
67
- try {
68
- const parent = realpathSync.native(dirname(absolute));
69
- if (!lstatSync(parent).isDirectory()) return null;
70
- target = resolve(parent, basename(absolute));
71
- } catch {
72
- return null;
73
- }
74
- }
75
- const rel = relative(repositoryRoot, target);
76
- if (!rel || isAbsolute(rel) || rel === ".." || rel.startsWith(`..${sep}`)) {
77
- return null;
78
- }
79
- return sep === "/" ? rel : rel.split(sep).join("/");
80
- }
81
-
82
- // src/lib/activation.ts
83
- var PRIM_ACTIVE_KEY = "prim.active";
84
- var PRIM_REPO_SYNC_ID_KEY = "prim.repoSyncId";
85
- var PROJECT_INSTALL_FILES = [".claude/settings.json", ".codex/hooks.json"];
86
- var PRIM_HOOK_BINS = ["prim-hook", "prim-pre-tool-use", "prim-post-tool-use"];
87
- function repoActiveFlag(cwd) {
88
- try {
89
- const value = execFileSync2("git", ["config", "--get", PRIM_ACTIVE_KEY], {
90
- cwd,
91
- encoding: "utf-8",
92
- stdio: ["ignore", "pipe", "ignore"]
93
- }).trim();
94
- if (value === "true") return "true";
95
- if (value === "false") return "false";
96
- return void 0;
97
- } catch {
98
- return void 0;
99
- }
100
- }
101
- function isRepoActive(cwd) {
102
- return repoActiveFlag(cwd) === "true";
103
- }
104
- function setRepoActive(cwd, active) {
105
- execFileSync2("git", ["config", "--local", PRIM_ACTIVE_KEY, active ? "true" : "false"], {
106
- cwd,
107
- stdio: ["ignore", "ignore", "pipe"]
108
- });
109
- }
110
- function repoSyncId(cwd) {
111
- try {
112
- return execFileSync2("git", ["config", "--local", "--get", PRIM_REPO_SYNC_ID_KEY], {
113
- cwd,
114
- encoding: "utf-8",
115
- stdio: ["ignore", "pipe", "ignore"]
116
- }).trim() || void 0;
117
- } catch {
118
- return void 0;
119
- }
120
- }
121
- function setRepoSyncId(cwd, value) {
122
- if (!value || value.includes("\0") || value.includes("\n")) {
123
- throw new Error("invalid repository binding returned by server");
124
- }
125
- execFileSync2("git", ["config", "--local", PRIM_REPO_SYNC_ID_KEY, value], {
126
- cwd,
127
- stdio: ["ignore", "ignore", "pipe"]
128
- });
129
- }
130
- function hasProjectPrimInstall(cwd) {
131
- const root = gitToplevel(cwd);
132
- if (root === null || root === homedir()) return false;
133
- for (const rel of PROJECT_INSTALL_FILES) {
134
- const path = join(root, rel);
135
- if (existsSync(path)) {
136
- try {
137
- const content = readFileSync(path, "utf-8");
138
- if (PRIM_HOOK_BINS.some((bin) => content.includes(bin))) return true;
139
- } catch {
140
- }
141
- }
142
- }
143
- return false;
144
- }
145
- function isRepoActiveForCapture(cwd) {
146
- const flag = repoActiveFlag(cwd);
147
- if (flag === "true") return true;
148
- if (flag === "false") return false;
149
- return hasProjectPrimInstall(cwd);
150
- }
151
- function decisionIngestionStatus(cwd) {
152
- return isRepoActiveForCapture(cwd) ? "enabled" : "disabled";
153
- }
154
-
155
- export {
156
- gitToplevel,
157
- githubRepositoryFullName,
158
- canonicalGitRoot,
159
- canonicalRepositoryPath,
160
- isRepoActive,
161
- setRepoActive,
162
- repoSyncId,
163
- setRepoSyncId,
164
- isRepoActiveForCapture,
165
- decisionIngestionStatus
166
- };