@intra-mart/accel 2.0.0 → 3.0.0-dev.202611201537

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.
Files changed (69) hide show
  1. package/README.md +9 -3
  2. package/assets/githooks/.githooks/pre-commit/pre-commit_en +71 -32
  3. package/assets/githooks/.githooks/pre-commit/pre-commit_ja +67 -33
  4. package/assets/githooks/.githooks/pre-commit/pre-commit_zh_CN +66 -32
  5. package/assets/runtime/ensure.mjs +27 -16
  6. package/dist/asset/deployer.d.ts +0 -2
  7. package/dist/asset/deployer.js +1 -6
  8. package/dist/asset/installer.d.ts +18 -2
  9. package/dist/asset/installer.js +94 -17
  10. package/dist/commands/attach-existing-files.d.ts +7 -0
  11. package/dist/commands/attach-existing-files.js +47 -1
  12. package/dist/commands/attach.d.ts +2 -0
  13. package/dist/commands/attach.js +20 -4
  14. package/dist/commands/deploy.d.ts +5 -1
  15. package/dist/commands/deploy.js +10 -3
  16. package/dist/commands/detach.d.ts +3 -1
  17. package/dist/commands/detach.js +16 -4
  18. package/dist/commands/init.d.ts +2 -0
  19. package/dist/commands/init.js +8 -3
  20. package/dist/commands/login.d.ts +8 -0
  21. package/dist/commands/login.js +13 -1
  22. package/dist/commands/mobile/attach.js +2 -3
  23. package/dist/commands/mobile/init.js +5 -2
  24. package/dist/commands/mobile/shared.d.ts +1 -1
  25. package/dist/commands/mobile/shared.js +5 -2
  26. package/dist/commands/switch.d.ts +2 -0
  27. package/dist/commands/switch.js +2 -2
  28. package/dist/commands/update.d.ts +2 -0
  29. package/dist/commands/update.js +1 -1
  30. package/dist/git/config-file.d.ts +10 -2
  31. package/dist/git/config-file.js +177 -94
  32. package/dist/git/config-scopes.d.ts +17 -0
  33. package/dist/git/config-scopes.js +33 -0
  34. package/dist/git/index-file.js +23 -4
  35. package/dist/git/read-capped.d.ts +1 -0
  36. package/dist/git/read-capped.js +6 -3
  37. package/dist/git/repo-discovery.d.ts +26 -0
  38. package/dist/git/repo-discovery.js +213 -0
  39. package/dist/git/untracked.d.ts +1 -0
  40. package/dist/git/untracked.js +23 -1
  41. package/dist/git/worktree-status.js +26 -21
  42. package/dist/i18n/en.js +72 -6
  43. package/dist/i18n/ja.js +72 -6
  44. package/dist/i18n/zh_CN.js +72 -6
  45. package/dist/interactive/credentials-prompts.d.ts +4 -1
  46. package/dist/interactive/credentials-prompts.js +2 -1
  47. package/dist/interactive/hook-confirm.d.ts +2 -0
  48. package/dist/interactive/hook-confirm.js +16 -0
  49. package/dist/lib.d.ts +1 -0
  50. package/dist/utils/command-exists.d.ts +5 -0
  51. package/dist/utils/command-exists.js +7 -6
  52. package/dist/utils/external-command.d.ts +0 -1
  53. package/dist/utils/external-command.js +1 -29
  54. package/dist/utils/githooks-remove.d.ts +27 -0
  55. package/dist/utils/githooks-remove.js +196 -0
  56. package/dist/utils/githooks-state.d.ts +80 -0
  57. package/dist/utils/githooks-state.js +364 -0
  58. package/dist/utils/githooks-sync.d.ts +11 -2
  59. package/dist/utils/githooks-sync.js +360 -68
  60. package/dist/utils/managed-marker.d.ts +1 -0
  61. package/dist/utils/managed-marker.js +1 -0
  62. package/dist/utils/mcp-generate.d.ts +5 -1
  63. package/dist/utils/mcp-generate.js +13 -4
  64. package/dist/utils/output-tail.d.ts +11 -0
  65. package/dist/utils/output-tail.js +29 -0
  66. package/dist/utils/runtime-ensure.js +10 -4
  67. package/package.json +1 -1
  68. package/dist/git/repo-layout.d.ts +0 -5
  69. package/dist/git/repo-layout.js +0 -44
@@ -0,0 +1,364 @@
1
+ import { randomBytes } from "node:crypto";
2
+ import { constants as fsConstants } from "node:fs";
3
+ import { access, chmod, lstat, open, readdir, realpath, rename, rmdir, stat, unlink, writeFile, } from "node:fs/promises";
4
+ import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
5
+ import { createFsAssetTree } from "../asset/asset-tree.js";
6
+ import { walkAssetDir } from "../asset/walker.js";
7
+ import { emptyEvalContext } from "../core/eval-context.js";
8
+ import { getAssetsGithooksRoot } from "../core/paths.js";
9
+ import { deleteConfigKey, isConfigNotEditableError, readConfigValue, } from "../git/config-file.js";
10
+ import { expandTilde, readScopedConfigValues, resolveUserConfigPaths, } from "../git/config-scopes.js";
11
+ import { readTextFileCapped } from "../git/read-capped.js";
12
+ import { comparablePath, isOutsideRelative, } from "../git/repo-discovery.js";
13
+ import { getMessage } from "../i18n/index.js";
14
+ import { MANAGED_MARKER } from "./managed-marker.js";
15
+ import { sanitizeDisplayText } from "./sanitize-text.js";
16
+ export const INTEGRATION_DIR = ".githooks";
17
+ export const INTEGRATION_SCRIPT = "pre-commit";
18
+ export const INTEGRATION_SCRIPT_ALT = "accel-pre-commit";
19
+ export const INTEGRATION_ATTRIBUTES = ".gitattributes";
20
+ export const integrationPaths = (projectDir) => {
21
+ const dir = join(projectDir, INTEGRATION_DIR);
22
+ return {
23
+ dir,
24
+ primary: join(dir, INTEGRATION_SCRIPT),
25
+ alt: join(dir, INTEGRATION_SCRIPT_ALT),
26
+ attributes: join(dir, INTEGRATION_ATTRIBUTES),
27
+ };
28
+ };
29
+ const LEGACY_HOOKS_PATH = ".githooks";
30
+ const MAX_HOOK_FILE_BYTES = 1024 * 1024;
31
+ const KNOWN_HOOK_NAMES = new Set([
32
+ "applypatch-msg",
33
+ "pre-applypatch",
34
+ "post-applypatch",
35
+ "pre-commit",
36
+ "pre-merge-commit",
37
+ "prepare-commit-msg",
38
+ "commit-msg",
39
+ "post-commit",
40
+ "pre-rebase",
41
+ "post-checkout",
42
+ "post-merge",
43
+ "pre-push",
44
+ "pre-receive",
45
+ "update",
46
+ "proc-receive",
47
+ "post-receive",
48
+ "post-update",
49
+ "reference-transaction",
50
+ "push-to-checkout",
51
+ "pre-auto-gc",
52
+ "post-rewrite",
53
+ "sendemail-validate",
54
+ "fsmonitor-watchman",
55
+ "p4-changelist",
56
+ "p4-prepare-changelist",
57
+ "p4-post-changelist",
58
+ "p4-pre-submit",
59
+ "post-index-change",
60
+ ]);
61
+ export const classifyFile = async (path) => {
62
+ const stats = await lstat(path).catch(() => null);
63
+ if (stats === null)
64
+ return "absent";
65
+ if (!stats.isFile())
66
+ return "foreign";
67
+ const content = await readTextFileCapped(path, MAX_HOOK_FILE_BYTES);
68
+ return content !== null && content.includes(MANAGED_MARKER)
69
+ ? "managed"
70
+ : "foreign";
71
+ };
72
+ export const classifyDir = async (path) => {
73
+ const stats = await lstat(path).catch(() => null);
74
+ if (stats === null)
75
+ return "absent";
76
+ return stats.isDirectory() ? "directory" : "other";
77
+ };
78
+ export const loadHookAssets = async (locale) => {
79
+ const tree = createFsAssetTree(getAssetsGithooksRoot());
80
+ const entries = await walkAssetDir(tree, emptyEvalContext({ locale }));
81
+ const read = async (relativePath) => {
82
+ const entry = entries.find((e) => e.relativePath === relativePath);
83
+ if (entry === undefined) {
84
+ throw new Error(`bundled hook asset not found: ${relativePath}`);
85
+ }
86
+ return (await tree.readFile(entry.sourceRelPath)).replace(/\r\n/g, "\n");
87
+ };
88
+ return {
89
+ script: await read(`${INTEGRATION_DIR}/pre-commit`),
90
+ gitattributes: await read(`${INTEGRATION_DIR}/${INTEGRATION_ATTRIBUTES}`),
91
+ };
92
+ };
93
+ export const writeFileAtomic = async (path, content, mode) => {
94
+ const current = await lstat(path).catch(() => null);
95
+ if (current?.isFile()) {
96
+ const existing = await readTextFileCapped(path, MAX_HOOK_FILE_BYTES);
97
+ if (existing === content) {
98
+ if (mode !== undefined && (current.mode & 0o777) !== mode) {
99
+ await chmod(path, mode);
100
+ }
101
+ return false;
102
+ }
103
+ }
104
+ const tmpPath = join(dirname(path), `.${basename(path)}.accel-${randomBytes(4).toString("hex")}.tmp`);
105
+ try {
106
+ await writeFile(tmpPath, content, { encoding: "utf-8", flag: "wx" });
107
+ if (mode !== undefined)
108
+ await chmod(tmpPath, mode);
109
+ await rename(tmpPath, path);
110
+ }
111
+ catch (err) {
112
+ await unlink(tmpPath).catch(() => { });
113
+ throw err;
114
+ }
115
+ return true;
116
+ };
117
+ export const removeIfManaged = async (path) => {
118
+ if ((await classifyFile(path)) !== "managed")
119
+ return false;
120
+ await unlink(path);
121
+ return true;
122
+ };
123
+ export const removeManagedFiles = async (paths, onError) => {
124
+ for (const path of paths) {
125
+ try {
126
+ await removeIfManaged(path);
127
+ }
128
+ catch (err) {
129
+ onError(err);
130
+ }
131
+ }
132
+ };
133
+ export const removeDirIfEmpty = async (dir) => {
134
+ const rest = await readdir(dir).catch(() => null);
135
+ if (rest === null || rest.length > 0)
136
+ return false;
137
+ return rmdir(dir).then(() => true, () => false);
138
+ };
139
+ export const unsetLocalHooksPath = async (configPath) => {
140
+ await deleteConfigKey(configPath, "core", "hooksPath");
141
+ return (await readConfigValue(configPath, "core", "hooksPath")) === undefined;
142
+ };
143
+ export const toUnsetFailure = (err) => isConfigNotEditableError(err)
144
+ ? { kind: "notEditable", reason: err.reason, configPath: err.configPath }
145
+ : { kind: "other", message: err instanceof Error ? err.message : String(err) };
146
+ const fixableByGit = (failure) => failure.kind === "remains" ||
147
+ (failure.kind === "notEditable" &&
148
+ (failure.reason === "keyOnHeaderLine" ||
149
+ failure.reason === "invalidEncoding" ||
150
+ failure.reason === "tooLarge"));
151
+ export const unsetFailureText = (failure, locale, displayRoot) => {
152
+ switch (failure.kind) {
153
+ case "notEditable":
154
+ return getMessage(`gitHooks.configNotEditable.${failure.reason}`, locale, {
155
+ path: sanitizeDisplayText(displayRelative(displayRoot, failure.configPath)),
156
+ });
157
+ case "remains":
158
+ return getMessage("gitHooks.hooksPathRemains", locale);
159
+ case "other":
160
+ return sanitizeDisplayText(failure.message);
161
+ }
162
+ };
163
+ export const isShellSafePath = (path) => !/["$`!%\u201c-\u201f\u0000-\u001f\u007f-\u009f]/.test(path) &&
164
+ !path.includes("\\\\") &&
165
+ !path.endsWith("\\");
166
+ export const remainingHooksPaths = (definitions) => ({
167
+ user: definitions.filter((d) => d.scope === "user").at(-1)?.value,
168
+ worktree: definitions.filter((d) => d.scope === "worktree").at(-1)?.value,
169
+ });
170
+ export const hasRemainingHooksPath = (remaining) => remaining.user !== undefined || remaining.worktree !== undefined;
171
+ export const manualUnsetGuidance = async (opts) => {
172
+ const message = (key, params) => getMessage(key, opts.locale, params);
173
+ const { failure } = opts;
174
+ const symlink = failure.kind === "notEditable" && failure.reason === "symlink";
175
+ const fixable = fixableByGit(failure);
176
+ if (opts.mode === "migration") {
177
+ if (!symlink && !fixable)
178
+ return "retry";
179
+ if (opts.legacyCheckActive === true && opts.placedHookPath === undefined) {
180
+ return [];
181
+ }
182
+ }
183
+ const placed = opts.placedHookPath === undefined
184
+ ? undefined
185
+ : sanitizeDisplayText(opts.placedHookPath);
186
+ const config = sanitizeDisplayText(displayRelative(opts.repoRoot, opts.configPath));
187
+ const afterUnset = [];
188
+ if (opts.mode === "detach") {
189
+ if (opts.legacyDirHasFiles)
190
+ afterUnset.push(message("gitHooks.detach.legacyKeepDir"));
191
+ }
192
+ else if (placed === undefined) {
193
+ afterUnset.push(message("gitHooks.manualUnset.thenLogin"));
194
+ }
195
+ else if (opts.legacyDirHasFiles) {
196
+ afterUnset.push(message("gitHooks.manualUnset.keepDir"));
197
+ if (opts.legacyScriptExists === true) {
198
+ afterUnset.push(message("gitHooks.migrated.teamNotice"));
199
+ }
200
+ }
201
+ if (symlink) {
202
+ const target = sanitizeDisplayText(await realpath(failure.configPath).catch(() => failure.configPath));
203
+ if (opts.mode === "detach") {
204
+ return [message("gitHooks.detach.legacySymlink", { target }), ...afterUnset];
205
+ }
206
+ return placed === undefined
207
+ ? [message("gitHooks.manualUnset.symlink", { target })]
208
+ : [
209
+ message("gitHooks.manualUnset.symlinkPlaced", { target, hookPath: placed }),
210
+ ...afterUnset,
211
+ ];
212
+ }
213
+ if (!fixable) {
214
+ return [message("gitHooks.detach.legacyManual", { config }), ...afterUnset];
215
+ }
216
+ const commandConfigPath = resolve(opts.repoRoot, opts.configPath);
217
+ if (!isShellSafePath(commandConfigPath)) {
218
+ return [message("gitHooks.manualUnset.noCommand", { config }), ...afterUnset];
219
+ }
220
+ const { remaining } = opts;
221
+ const intro = remaining.worktree !== undefined
222
+ ? message("gitHooks.manualUnset.introWorktreeHooksPath", {
223
+ config,
224
+ hooksPath: sanitizeDisplayText(remaining.worktree),
225
+ })
226
+ : remaining.user !== undefined
227
+ ? message("gitHooks.manualUnset.introOtherHooksPath", {
228
+ config,
229
+ hooksPath: sanitizeDisplayText(remaining.user),
230
+ })
231
+ : placed === undefined
232
+ ? message("gitHooks.manualUnset.intro", { config })
233
+ : message("gitHooks.manualUnset.introPlaced", { config, hookPath: placed });
234
+ const lines = [
235
+ intro,
236
+ ` git config --file "${commandConfigPath}" --unset-all core.hooksPath`,
237
+ ];
238
+ if (opts.reenabled.length > 0) {
239
+ lines.push(message("gitHooks.manualUnset.reenabled", { hooks: opts.reenabled.join(", ") }));
240
+ }
241
+ if (opts.worktreesExist)
242
+ lines.push(message("gitHooks.manualUnset.worktrees"));
243
+ lines.push(message("gitHooks.manualUnset.undo"), ` git config --file "${commandConfigPath}" core.hooksPath ${LEGACY_HOOKS_PATH}`, ...afterUnset);
244
+ return lines;
245
+ };
246
+ export const readHooksPathDefinitions = (layout, env) => readScopedConfigValues(layout, "core", "hooksPath", env);
247
+ export const resolveHooksPathDir = (value, worktreeRoot, env) => {
248
+ const expanded = expandTilde(value, resolveUserConfigPaths(env).home);
249
+ return isAbsolute(expanded) ? expanded : resolve(worktreeRoot, expanded);
250
+ };
251
+ export const displayRelative = (base, path) => {
252
+ const rel = relative(base, path);
253
+ if (rel === "" || isOutsideRelative(rel))
254
+ return path;
255
+ return sep === "/" ? rel : rel.split(sep).join("/");
256
+ };
257
+ const listWorktreeRoots = async (layout) => {
258
+ const roots = new Map();
259
+ const add = async (dir) => {
260
+ const stats = await stat(dir).catch(() => null);
261
+ if (stats?.isDirectory())
262
+ roots.set(comparablePath(dir), dir);
263
+ };
264
+ await add(layout.worktreeRoot);
265
+ if (basename(layout.commonDir) === ".git") {
266
+ await add(dirname(layout.commonDir));
267
+ }
268
+ const worktreesDir = join(layout.commonDir, "worktrees");
269
+ const ids = await readdir(worktreesDir).catch(() => []);
270
+ for (const id of ids) {
271
+ const ref = await readTextFileCapped(join(worktreesDir, id, "gitdir"), 64 * 1024);
272
+ const target = ref?.trim() ?? "";
273
+ if (target.length === 0)
274
+ continue;
275
+ const dotGit = isAbsolute(target)
276
+ ? target
277
+ : resolve(join(worktreesDir, id), target);
278
+ await add(dirname(dotGit));
279
+ }
280
+ return [...roots.values()];
281
+ };
282
+ export const hasLinkedWorktrees = async (commonDir) => {
283
+ const ids = await readdir(join(commonDir, "worktrees")).catch(() => []);
284
+ return ids.length > 0;
285
+ };
286
+ export const containsOnlyManagedFiles = async (dir) => {
287
+ const state = await classifyDir(dir);
288
+ if (state === "absent")
289
+ return true;
290
+ if (state === "other")
291
+ return false;
292
+ const entries = await readdir(dir).catch(() => null);
293
+ if (entries === null)
294
+ return false;
295
+ for (const name of entries) {
296
+ if ((await classifyFile(join(dir, name))) !== "managed")
297
+ return false;
298
+ }
299
+ return true;
300
+ };
301
+ export const classifyLegacy = async (layout, definitions) => {
302
+ const locals = definitions.filter((d) => d.scope === "local");
303
+ const local = locals[locals.length - 1];
304
+ if (local === undefined || local.value !== LEGACY_HOOKS_PATH) {
305
+ return { kind: "none" };
306
+ }
307
+ const dir = join(layout.worktreeRoot, INTEGRATION_DIR);
308
+ if (layout.projectRel === "") {
309
+ const roots = await listWorktreeRoots(layout);
310
+ let clean = true;
311
+ for (const root of roots) {
312
+ if (!(await containsOnlyManagedFiles(join(root, INTEGRATION_DIR)))) {
313
+ clean = false;
314
+ break;
315
+ }
316
+ }
317
+ if (clean)
318
+ return { kind: "migratable", dir };
319
+ }
320
+ const effective = definitions[definitions.length - 1];
321
+ const scriptPath = join(dir, INTEGRATION_SCRIPT);
322
+ if (effective === local &&
323
+ (await classifyDir(dir)) === "directory" &&
324
+ (await classifyFile(scriptPath)) === "managed") {
325
+ return { kind: "protected", scriptPath };
326
+ }
327
+ return { kind: "set" };
328
+ };
329
+ const startsWithShebang = async (path) => {
330
+ let handle;
331
+ try {
332
+ handle = await open(path, "r");
333
+ const buf = Buffer.alloc(2);
334
+ const { bytesRead } = await handle.read(buf, 0, 2, 0);
335
+ return bytesRead === 2 && buf.toString("latin1") === "#!";
336
+ }
337
+ catch {
338
+ return false;
339
+ }
340
+ finally {
341
+ await handle?.close();
342
+ }
343
+ };
344
+ const isRunnableHookFile = async (path) => process.platform === "win32"
345
+ ? startsWithShebang(path)
346
+ : access(path, fsConstants.X_OK).then(() => true, () => false);
347
+ export const isRunnableManagedHook = async (path) => (await classifyFile(path)) === "managed" && (await isRunnableHookFile(path));
348
+ export const listActiveHooks = async (hooksDir) => {
349
+ const names = await readdir(hooksDir).catch(() => []);
350
+ const active = [];
351
+ for (const name of names) {
352
+ if (!KNOWN_HOOK_NAMES.has(name))
353
+ continue;
354
+ const path = join(hooksDir, name);
355
+ const stats = await stat(path).catch(() => null);
356
+ if (stats === null || !stats.isFile())
357
+ continue;
358
+ if ((await classifyFile(path)) === "managed")
359
+ continue;
360
+ if (await isRunnableHookFile(path))
361
+ active.push(name);
362
+ }
363
+ return active.sort();
364
+ };
@@ -1,3 +1,12 @@
1
- export declare const syncGitHooks: (projectDir: string, opts: {
1
+ export type HookPlacementTarget = {
2
+ hookPath: string;
3
+ repositoryRoot: string;
4
+ };
5
+ export type ConfirmHookPlacement = (target: HookPlacementTarget) => Promise<boolean>;
6
+ export type SyncGitHooksOptions = {
7
+ info?: (msg: string) => void;
2
8
  warn: (msg: string) => void;
3
- }) => Promise<void>;
9
+ confirmHookPlacement?: ConfirmHookPlacement;
10
+ env?: NodeJS.ProcessEnv;
11
+ };
12
+ export declare const syncGitHooks: (projectDirInput: string, opts: SyncGitHooksOptions) => Promise<void>;