opencode-github-sync 3.1.1 → 3.3.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.
Files changed (136) hide show
  1. package/README.md +535 -220
  2. package/README.zh-CN.md +387 -197
  3. package/dist/cli/app.d.ts +39 -0
  4. package/dist/cli/app.d.ts.map +1 -0
  5. package/dist/cli/app.js +179 -0
  6. package/dist/cli/app.js.map +1 -0
  7. package/dist/cli/args.d.ts +10 -0
  8. package/dist/cli/args.d.ts.map +1 -0
  9. package/dist/cli/args.js +39 -0
  10. package/dist/cli/args.js.map +1 -0
  11. package/dist/cli/i18n.d.ts +378 -0
  12. package/dist/cli/i18n.d.ts.map +1 -0
  13. package/dist/cli/i18n.js +793 -0
  14. package/dist/cli/i18n.js.map +1 -0
  15. package/dist/cli/index.js +362 -115
  16. package/dist/cli/index.js.map +1 -1
  17. package/dist/cli/menu.d.ts +52 -0
  18. package/dist/cli/menu.d.ts.map +1 -0
  19. package/dist/cli/menu.js +1126 -0
  20. package/dist/cli/menu.js.map +1 -0
  21. package/dist/cli/render.d.ts +3 -0
  22. package/dist/cli/render.d.ts.map +1 -1
  23. package/dist/cli/render.js +6 -1
  24. package/dist/cli/render.js.map +1 -1
  25. package/dist/cli/ui.d.ts +62 -0
  26. package/dist/cli/ui.d.ts.map +1 -0
  27. package/dist/cli/ui.js +383 -0
  28. package/dist/cli/ui.js.map +1 -0
  29. package/dist/core/autosync.d.ts +136 -0
  30. package/dist/core/autosync.d.ts.map +1 -0
  31. package/dist/core/autosync.js +516 -0
  32. package/dist/core/autosync.js.map +1 -0
  33. package/dist/core/bundle.d.ts +11 -0
  34. package/dist/core/bundle.d.ts.map +1 -0
  35. package/dist/core/bundle.js +87 -0
  36. package/dist/core/bundle.js.map +1 -0
  37. package/dist/core/categories.d.ts +37 -0
  38. package/dist/core/categories.d.ts.map +1 -0
  39. package/dist/core/categories.js +115 -0
  40. package/dist/core/categories.js.map +1 -0
  41. package/dist/core/compare.d.ts +21 -0
  42. package/dist/core/compare.d.ts.map +1 -0
  43. package/dist/core/compare.js +124 -0
  44. package/dist/core/compare.js.map +1 -0
  45. package/dist/core/credentials.d.ts +40 -0
  46. package/dist/core/credentials.d.ts.map +1 -0
  47. package/dist/core/credentials.js +364 -0
  48. package/dist/core/credentials.js.map +1 -0
  49. package/dist/core/env.d.ts +16 -0
  50. package/dist/core/env.d.ts.map +1 -0
  51. package/dist/core/env.js +47 -0
  52. package/dist/core/env.js.map +1 -0
  53. package/dist/core/fsx.d.ts +2 -0
  54. package/dist/core/fsx.d.ts.map +1 -1
  55. package/dist/core/fsx.js +6 -0
  56. package/dist/core/fsx.js.map +1 -1
  57. package/dist/core/git.d.ts +6 -0
  58. package/dist/core/git.d.ts.map +1 -1
  59. package/dist/core/git.js +44 -4
  60. package/dist/core/git.js.map +1 -1
  61. package/dist/core/guard.d.ts +36 -0
  62. package/dist/core/guard.d.ts.map +1 -0
  63. package/dist/core/guard.js +65 -0
  64. package/dist/core/guard.js.map +1 -0
  65. package/dist/core/history.d.ts +38 -0
  66. package/dist/core/history.d.ts.map +1 -0
  67. package/dist/core/history.js +98 -0
  68. package/dist/core/history.js.map +1 -0
  69. package/dist/core/index.d.ts +11 -0
  70. package/dist/core/index.d.ts.map +1 -1
  71. package/dist/core/index.js +11 -0
  72. package/dist/core/index.js.map +1 -1
  73. package/dist/core/lease.d.ts +52 -0
  74. package/dist/core/lease.d.ts.map +1 -0
  75. package/dist/core/lease.js +235 -0
  76. package/dist/core/lease.js.map +1 -0
  77. package/dist/core/notify.d.ts +9 -0
  78. package/dist/core/notify.d.ts.map +1 -0
  79. package/dist/core/notify.js +69 -0
  80. package/dist/core/notify.js.map +1 -0
  81. package/dist/core/projects.d.ts +79 -0
  82. package/dist/core/projects.d.ts.map +1 -0
  83. package/dist/core/projects.js +192 -0
  84. package/dist/core/projects.js.map +1 -0
  85. package/dist/core/recovery.d.ts +26 -0
  86. package/dist/core/recovery.d.ts.map +1 -0
  87. package/dist/core/recovery.js +56 -0
  88. package/dist/core/recovery.js.map +1 -0
  89. package/dist/core/repair.d.ts +44 -0
  90. package/dist/core/repair.d.ts.map +1 -0
  91. package/dist/core/repair.js +81 -0
  92. package/dist/core/repair.js.map +1 -0
  93. package/dist/core/repo.d.ts +34 -2
  94. package/dist/core/repo.d.ts.map +1 -1
  95. package/dist/core/repo.js +87 -5
  96. package/dist/core/repo.js.map +1 -1
  97. package/dist/core/rows.d.ts +36 -0
  98. package/dist/core/rows.d.ts.map +1 -0
  99. package/dist/core/rows.js +75 -0
  100. package/dist/core/rows.js.map +1 -0
  101. package/dist/core/scheduler.d.ts +138 -0
  102. package/dist/core/scheduler.d.ts.map +1 -0
  103. package/dist/core/scheduler.js +531 -0
  104. package/dist/core/scheduler.js.map +1 -0
  105. package/dist/core/sessions.d.ts +32 -4
  106. package/dist/core/sessions.d.ts.map +1 -1
  107. package/dist/core/sessions.js +316 -104
  108. package/dist/core/sessions.js.map +1 -1
  109. package/dist/core/settings.d.ts +67 -0
  110. package/dist/core/settings.d.ts.map +1 -1
  111. package/dist/core/settings.js +45 -2
  112. package/dist/core/settings.js.map +1 -1
  113. package/dist/core/setup.d.ts +20 -0
  114. package/dist/core/setup.d.ts.map +1 -1
  115. package/dist/core/setup.js +87 -4
  116. package/dist/core/setup.js.map +1 -1
  117. package/dist/core/sqlite.d.ts +2 -0
  118. package/dist/core/sqlite.d.ts.map +1 -1
  119. package/dist/core/sqlite.js +14 -0
  120. package/dist/core/sqlite.js.map +1 -1
  121. package/dist/core/stage.d.ts +18 -0
  122. package/dist/core/stage.d.ts.map +1 -1
  123. package/dist/core/stage.js +133 -4
  124. package/dist/core/stage.js.map +1 -1
  125. package/dist/core/sync.d.ts +25 -0
  126. package/dist/core/sync.d.ts.map +1 -1
  127. package/dist/core/sync.js +283 -43
  128. package/dist/core/sync.js.map +1 -1
  129. package/dist/core/update.d.ts +21 -0
  130. package/dist/core/update.d.ts.map +1 -0
  131. package/dist/core/update.js +75 -0
  132. package/dist/core/update.js.map +1 -0
  133. package/dist/plugin/index.d.ts.map +1 -1
  134. package/dist/plugin/index.js +6 -5
  135. package/dist/plugin/index.js.map +1 -1
  136. package/package.json +4 -4
package/dist/cli/index.js CHANGED
@@ -1,140 +1,148 @@
1
1
  #!/usr/bin/env node
2
2
  import fs from "node:fs";
3
3
  import path from "node:path";
4
- import readline from "node:readline";
4
+ import { appendLog, backoffRemainingMs, failureCount, readState, runAutosync, writeState, } from "../core/autosync.js";
5
+ import { LargeChangeError } from "../core/guard.js";
5
6
  import { withSyncLock } from "../core/lock.js";
6
7
  import { getDatabasePath, getRoots } from "../core/paths.js";
8
+ import { repairSessions } from "../core/repair.js";
7
9
  import { listSessions } from "../core/sessions.js";
8
10
  import { OVERRIDES_TEMPLATE, loadSettings, overridesPath, saveSettings, settingsPath, } from "../core/settings.js";
9
- import { SetupError, discoverSyncRepos, initSync, linkSync } from "../core/setup.js";
11
+ import { SetupError, discoverSyncRepos, initSync, isRepoUrl, linkByUrl, linkSync, } from "../core/setup.js";
10
12
  import { sqliteAvailable } from "../core/sqlite.js";
11
13
  import { SyncError, formatBytes, pull, push, status } from "../core/sync.js";
14
+ import { applyLocalSettings, autosyncDeps, formatTime, realScheduler, safeLoadSettings, tomorrowMorning, } from "./app.js";
15
+ import { parseArgs } from "./args.js";
16
+ import { ago, t } from "./i18n.js";
17
+ import { disableAutosync, enableAutosync, runMenu, runSetup } from "./menu.js";
12
18
  import { Spinner, banner, createCliReporter, detail, fail, heading, info, keyValue, ok, outcome, rule, warn, write, } from "./render.js";
13
19
  import { glyph, paint, style } from "./theme.js";
20
+ import { Ui } from "./ui.js";
14
21
  const LOCK_WAIT_MS = 5 * 60 * 1000;
15
- function parseArgs(argv) {
16
- const positional = [];
17
- const flags = new Set();
18
- const values = new Map();
19
- for (const arg of argv) {
20
- if (!arg.startsWith("-")) {
21
- positional.push(arg);
22
- continue;
23
- }
24
- const clean = arg.replace(/^--?/, "");
25
- const eq = clean.indexOf("=");
26
- if (eq !== -1)
27
- values.set(clean.slice(0, eq), clean.slice(eq + 1));
28
- else
29
- flags.add(clean);
30
- }
31
- return { command: positional.shift() ?? "help", positional, flags, values };
22
+ function newUi() {
23
+ return new Ui({
24
+ input: process.stdin,
25
+ output: process.stdout,
26
+ interactive: Boolean(process.stdin.isTTY && process.stdout.isTTY),
27
+ });
32
28
  }
33
- async function confirm(question, lines) {
34
- if (!process.stdin.isTTY)
29
+ async function confirmForce(action, lines, args) {
30
+ if (args.flags.has("yes") || args.flags.has("y"))
31
+ return true;
32
+ if (!process.stdin.isTTY) {
33
+ fail(t("cliNeedTty"));
35
34
  return false;
35
+ }
36
36
  write();
37
- write(` ${paint(" FORCE ", style.bold, style.red)} ${paint(question, style.bold, style.text)}`);
37
+ write(` ${paint(" FORCE ", style.bold, style.red)} ${paint(t("cliForceTitle", { action }), style.bold, style.text)}`);
38
38
  for (const line of lines)
39
39
  write(` ${paint(`${glyph.bullet} ${line}`, style.muted)}`);
40
40
  write();
41
- const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
42
- const answer = await new Promise((resolve) => {
43
- rl.question(` ${paint("Type", style.muted)} ${paint("yes", style.bold, style.red)} ${paint("to continue: ", style.muted)}`, resolve);
44
- });
45
- rl.close();
46
- write();
47
- return answer.trim().toLowerCase() === "yes";
41
+ const ui = newUi();
42
+ const answer = await ui.text(t("cliTypeYes"));
43
+ ui.close();
44
+ return answer?.trim().toLowerCase() === "yes";
48
45
  }
49
46
  // ── Commands ────────────────────────────────────────────────────────────────
50
47
  async function runSync(action, args) {
51
48
  const force = args.flags.has("force") || args.flags.has("f");
52
49
  const dryRun = args.flags.has("dry-run") || args.flags.has("n");
50
+ const allowLarge = args.flags.has("allow-large");
53
51
  const roots = getRoots();
54
- banner(action === "push" ? "push to GitHub" : "pull from GitHub", force ? "FORCE" : undefined);
52
+ banner(action === "push" ? t("cliPushTitle") : t("cliPullTitle"), force ? "FORCE" : undefined);
55
53
  if (force && !dryRun) {
56
- const consequences = action === "push"
57
- ? ["Any change made on another machine and not pulled here will be overwritten."]
58
- : [
59
- "Uncommitted local changes will be discarded.",
60
- "Skills and state directories will be rewritten from the repository.",
61
- ];
62
- if (!(await confirm(`Force ${action}?`, consequences))) {
63
- info("Cancelled.");
54
+ const consequences = action === "push" ? [t("cliForcePushWarn")] : [t("cliForcePullWarn")];
55
+ if (!(await confirmForce(action, consequences, args))) {
56
+ info(t("cancelled"));
64
57
  return;
65
58
  }
66
59
  }
67
- const spinner = new Spinner(action === "push" ? "Preparing push" : "Preparing pull");
60
+ const spinner = new Spinner(action === "push" ? t("cliPreparingPush") : t("cliPreparingPull"));
68
61
  spinner.start();
69
62
  const reporter = createCliReporter(spinner);
70
63
  try {
71
64
  const result = await withSyncLock(roots.config, { waitMs: LOCK_WAIT_MS }, () => action === "push"
72
- ? push({ force, dryRun, reporter, roots })
65
+ ? push({ force, dryRun, reporter, roots, allowLarge })
73
66
  : pull({ force, dryRun, reporter, roots }));
74
67
  spinner.stop();
75
68
  if (result.sessions) {
76
69
  const bits = [];
77
70
  if (result.sessions.exported !== undefined)
78
- bits.push(`${result.sessions.exported} exported`);
71
+ bits.push(t("cliExported", { n: result.sessions.exported }));
79
72
  if (result.sessions.imported !== undefined)
80
- bits.push(`${result.sessions.imported} imported`);
73
+ bits.push(t("cliImported", { n: result.sessions.imported }));
81
74
  if (result.sessions.skipped)
82
- bits.push(`${result.sessions.skipped} skipped`);
75
+ bits.push(t("cliSkipped", { n: result.sessions.skipped }));
83
76
  if (bits.length > 0)
84
- detail(`Sessions: ${bits.join(", ")}`);
77
+ detail(t("cliSessionsLine", { text: bits.join(", ") }));
85
78
  }
86
- outcome(dryRun ? `${result.message} (dry run)` : result.message, result.changed);
79
+ if (result.guard) {
80
+ warn(t("largeTitle"));
81
+ detail(`${result.guard.addedFiles} / ${formatBytes(result.guard.totalBytes)}`);
82
+ }
83
+ outcome(dryRun ? t("cliDryRun", { message: result.message }) : result.message, result.changed);
87
84
  if (result.restartRequired && result.changed) {
88
- info("Restart OpenCode for the new configuration to take effect.");
85
+ info(t("restartHint"));
89
86
  write();
90
87
  }
91
88
  }
89
+ catch (error) {
90
+ if (error instanceof LargeChangeError) {
91
+ spinner.stop();
92
+ fail(`${t("largeRefused")}\n${error.message}`);
93
+ process.exitCode = 1;
94
+ return;
95
+ }
96
+ throw error;
97
+ }
92
98
  finally {
93
99
  spinner.stop();
94
100
  }
95
101
  }
96
102
  function runStatus() {
97
- banner("status");
103
+ banner(t("stTitle"));
98
104
  const roots = getRoots();
99
105
  const state = status({ roots });
100
106
  if (!state.configured) {
101
- fail("No sync repository configured.");
107
+ fail(t("cliNoRepo"));
102
108
  write();
103
- info("Run `opencode-sync init` to create one.");
109
+ info(t("cliRunInit"));
104
110
  const found = discoverSyncRepos();
105
111
  if (found.length > 0) {
106
112
  write();
107
- detail(`Existing private repositories that look like a match: ${found.slice(0, 5).join(", ")}`);
108
- detail("Link one with: opencode-sync link <owner/repo>");
113
+ detail(t("cliFoundRepos", { list: found.slice(0, 5).join(", ") }));
114
+ detail(t("cliLinkHint"));
109
115
  }
110
116
  write();
111
117
  return;
112
118
  }
113
- keyValue("repository", paint(state.remote ?? "-", style.text));
114
- keyValue("branch", paint(state.branch, style.text));
115
- keyValue("machine", paint(state.machineAlias, style.text));
119
+ keyValue(t("stRepository"), paint(state.remote ?? "-", style.text));
120
+ keyValue(t("stBranch"), paint(state.branch, style.text));
121
+ keyValue(t("stMachine"), paint(state.machineAlias, style.text));
116
122
  if (!state.initialized) {
117
123
  write();
118
- warn("The local repository is not initialised yet. Run `opencode-sync pull`.");
124
+ warn(t("cliNotInitialised"));
119
125
  write();
120
126
  return;
121
127
  }
122
128
  const sync = state.ahead === 0 && state.behind === 0
123
- ? paint("in sync", style.green)
129
+ ? paint(t("stInSync"), style.green)
124
130
  : [
125
- state.ahead > 0 ? paint(`${state.ahead} to push`, style.yellow) : "",
126
- state.behind > 0 ? paint(`${state.behind} to pull`, style.cyan) : "",
131
+ state.ahead > 0 ? paint(t("stToPush", { n: state.ahead }), style.yellow) : "",
132
+ state.behind > 0 ? paint(t("stToPull", { n: state.behind }), style.cyan) : "",
127
133
  ]
128
134
  .filter(Boolean)
129
135
  .join(paint(" ", style.faint));
130
- keyValue("state", sync);
131
- keyValue("uncommitted", state.dirty === 0 ? paint("clean", style.green) : paint(`${state.dirty} file(s)`, style.yellow));
132
- keyValue("overrides", state.overridesActive ? paint("active", style.accent) : paint("none", style.muted));
133
- keyValue("sessions", state.sessionsEnabled
134
- ? paint(`enabled · ${state.sessionShards} shard(s)`, style.text)
135
- : paint("disabled", style.muted));
136
+ keyValue(t("stState"), sync);
137
+ keyValue(t("stUncommitted"), state.dirty === 0
138
+ ? paint(t("stClean"), style.green)
139
+ : paint(t("stFiles", { n: state.dirty }), style.yellow));
140
+ keyValue(t("stOverrides"), state.overridesActive ? paint(t("stActive"), style.accent) : paint(t("none"), style.muted));
141
+ keyValue(t("stSessions"), state.sessionsEnabled
142
+ ? paint(t("stSessionsOn", { n: state.sessionShards }), style.text)
143
+ : paint(t("stDisabled"), style.muted));
136
144
  if (state.lastCommit) {
137
- heading("Last commit");
145
+ heading(t("stLastCommit"));
138
146
  detail(state.lastCommit);
139
147
  if (state.lastCommitDate)
140
148
  detail(state.lastCommitDate);
@@ -142,7 +150,7 @@ function runStatus() {
142
150
  write();
143
151
  }
144
152
  function runInit(args, mode) {
145
- banner(mode === "init" ? "create a sync repository" : "link an existing repository");
153
+ banner(mode === "init" ? t("initTitle") : t("linkTitleCli"));
146
154
  const reporter = createCliReporter();
147
155
  const options = {
148
156
  repo: args.positional[0],
@@ -150,15 +158,22 @@ function runInit(args, mode) {
150
158
  includeCredentials: args.flags.has("credentials"),
151
159
  includeSessions: args.flags.has("sessions"),
152
160
  };
153
- const result = mode === "init" ? initSync(options) : linkSync(options);
161
+ const result = mode === "link" && isRepoUrl(options.repo)
162
+ ? linkByUrl({ ...options, url: options.repo })
163
+ : mode === "init"
164
+ ? initSync(options)
165
+ : linkSync(options);
166
+ const label = result.target.owner
167
+ ? `${result.target.owner}/${result.target.name}`
168
+ : result.target.name;
154
169
  ok(result.created
155
- ? `Created ${paint(`${result.target.owner}/${result.target.name}`, style.bold, style.text)}`
156
- : `Linked ${paint(`${result.target.owner}/${result.target.name}`, style.bold, style.text)}`);
157
- detail(`settings ${result.settingsFile}`);
158
- detail(`overrides ${result.overridesFile}`);
159
- heading("Next");
170
+ ? t("initCreated", { repo: paint(label, style.bold, style.text) })
171
+ : t("initLinked", { repo: paint(label, style.bold, style.text) }));
172
+ detail(t("initSettings", { file: result.settingsFile }));
173
+ detail(t("initOverrides", { file: result.overridesFile }));
174
+ heading(t("initNext"));
160
175
  write(` ${paint("1.", style.faint)} ${mode === "init" ? "opencode-sync push" : "opencode-sync pull"}`);
161
- write(` ${paint("2.", style.faint)} Restart OpenCode`);
176
+ write(` ${paint("2.", style.faint)} ${t("initRestart")}`);
162
177
  write();
163
178
  }
164
179
  async function runSessions(args) {
@@ -166,78 +181,124 @@ async function runSessions(args) {
166
181
  const roots = getRoots();
167
182
  const settings = loadSettings(roots.config);
168
183
  if (sub === "list") {
169
- banner("sessions");
184
+ banner(t("sesTitle"));
170
185
  if (!(await sqliteAvailable())) {
171
- fail("No SQLite driver available. Use Node 22.5+ or run under Bun.");
186
+ fail(t("sesNoSqlite"));
172
187
  write();
173
188
  return;
174
189
  }
175
190
  const limit = Number(args.values.get("limit") ?? 20);
176
191
  const sessions = await listSessions(getDatabasePath(roots), limit);
177
192
  if (sessions.length === 0) {
178
- info("No sessions found.");
193
+ info(t("sesNone"));
179
194
  write();
180
195
  return;
181
196
  }
182
197
  for (const session of sessions) {
183
198
  const when = new Date(session.timeUpdated).toISOString().slice(0, 16).replace("T", " ");
184
- const title = session.title || paint("(untitled)", style.faint);
199
+ const title = session.title || paint(t("sesUntitled"), style.faint);
185
200
  write(` ${paint(when, style.faint)} ${title}`);
186
201
  write(` ${paint(" ".repeat(16), style.faint)} ${paint(session.id, style.faint)}`);
187
202
  }
188
203
  write();
189
- detail(`Pin a session with: opencode-sync sessions include <id>`);
204
+ detail(t("sesPinHint"));
190
205
  write();
191
206
  return;
192
207
  }
193
208
  if (sub === "include" || sub === "exclude") {
194
209
  const id = args.positional[1];
195
210
  if (!id)
196
- throw new SyncError(`Usage: opencode-sync sessions ${sub} <session-id>`);
211
+ throw new SyncError(t("sesUsage", { sub }));
197
212
  const list = sub === "include" ? settings.sessions.include : settings.sessions.exclude;
198
213
  if (!list.includes(id))
199
214
  list.push(id);
200
215
  saveSettings(settings, roots.config);
201
- banner("sessions");
202
- ok(`${sub === "include" ? "Pinned" : "Excluded"} ${paint(id, style.text)}`);
216
+ banner(t("sesTitle"));
217
+ ok(sub === "include"
218
+ ? t("sesPinned", { id: paint(id, style.text) })
219
+ : t("sesExcluded", { id: paint(id, style.text) }));
203
220
  write();
204
221
  return;
205
222
  }
206
223
  if (sub === "enable" || sub === "disable") {
207
224
  settings.sessions.enabled = sub === "enable";
208
225
  saveSettings(settings, roots.config);
209
- banner("sessions");
210
- ok(`Session sync ${sub}d`);
226
+ banner(t("sesTitle"));
227
+ ok(sub === "enable" ? t("sesEnabled") : t("sesDisabled"));
211
228
  if (sub === "enable") {
212
- detail(`Window: ${settings.sessions.days} day(s), up to ${settings.sessions.maxSessions} sessions`);
213
- detail(`Per-session cap: ${formatBytes(settings.sessions.maxSessionBytes)}`);
214
- warn("Only enable this for a private repository — conversations are private data.");
229
+ detail(t("sesWindow", { days: settings.sessions.days, max: settings.sessions.maxSessions }));
230
+ detail(t("sesCap", { size: formatBytes(settings.sessions.maxSessionBytes) }));
231
+ warn(t("sessionsWarn"));
215
232
  }
216
233
  write();
217
234
  return;
218
235
  }
219
- throw new SyncError(`Unknown sessions subcommand: ${sub}`);
236
+ throw new SyncError(t("sesUnknown", { sub }));
237
+ }
238
+ async function runRepair(args) {
239
+ banner(t("rpTitle"));
240
+ if (!(await sqliteAvailable())) {
241
+ fail(t("sesNoSqlite"));
242
+ write();
243
+ return;
244
+ }
245
+ const apply = args.flags.has("apply");
246
+ const result = await repairSessions(getDatabasePath(getRoots()), { apply });
247
+ if (!result.supported) {
248
+ info(t("rpNotV2"));
249
+ write();
250
+ return;
251
+ }
252
+ if (result.moves.length === 0 && result.unresolved.length === 0) {
253
+ ok(t("rpNothing"));
254
+ write();
255
+ return;
256
+ }
257
+ info(t("rpSummary", { sessions: result.sessions, moves: result.moves.length }));
258
+ const groups = new Map();
259
+ let parked = 0;
260
+ for (const move of result.moves) {
261
+ if (move.parked)
262
+ parked++;
263
+ const key = `${move.from}\u0000${move.to}`;
264
+ groups.set(key, (groups.get(key) ?? 0) + 1);
265
+ }
266
+ for (const [key, n] of groups) {
267
+ const [from, to] = key.split("\u0000");
268
+ detail(t("rpMove", { n, from: from ?? "", to: to ?? "" }));
269
+ }
270
+ if (parked > 0)
271
+ detail(t("rpParked", { n: parked }));
272
+ for (const u of result.unresolved) {
273
+ detail(t("rpUnresolved", { n: u.count, directory: u.directory }));
274
+ }
275
+ write();
276
+ if (apply)
277
+ ok(t("rpApplied", { n: result.applied }));
278
+ else if (result.moves.length > 0)
279
+ info(t("rpDryRun"));
280
+ write();
220
281
  }
221
282
  function runOverrides() {
222
- banner("per-machine overrides");
283
+ banner(t("ovCliTitle"));
223
284
  const roots = getRoots();
224
285
  const file = overridesPath(roots.config);
225
286
  if (!fs.existsSync(file)) {
226
287
  fs.mkdirSync(path.dirname(file), { recursive: true });
227
288
  fs.writeFileSync(file, OVERRIDES_TEMPLATE);
228
- ok("Created the overrides file");
289
+ ok(t("ovCreated"));
229
290
  }
230
291
  detail(file);
231
292
  write();
232
- info("Anything in this file stays on this machine and is re-applied after every pull.");
293
+ info(t("ovCliInfo"));
233
294
  write();
234
295
  }
235
296
  function runConfig() {
236
- banner("settings");
297
+ banner(t("cfgTitle"));
237
298
  const roots = getRoots();
238
299
  const file = settingsPath(roots.config);
239
300
  if (!fs.existsSync(file)) {
240
- fail("No settings file yet. Run `opencode-sync init`.");
301
+ fail(t("cfgMissing"));
241
302
  write();
242
303
  return;
243
304
  }
@@ -246,39 +307,219 @@ function runConfig() {
246
307
  write(fs.readFileSync(file, "utf8").trimEnd());
247
308
  write();
248
309
  }
310
+ async function runAutosyncCommand(args) {
311
+ const sub = args.positional[0] ?? "status";
312
+ const roots = getRoots();
313
+ const ctx = { roots, scheduler: realScheduler };
314
+ switch (sub) {
315
+ case "run": {
316
+ // Called by the scheduler: no banner, no colour noise, one log line.
317
+ const trigger = args.values.get("trigger") ?? "scheduled";
318
+ const outcomeResult = await runAutosync(autosyncDeps(roots), trigger);
319
+ write(outcomeResult.action === "skip"
320
+ ? t("asRunSkipped", { reason: outcomeResult.reason })
321
+ : t("asRunDone", {
322
+ action: outcomeResult.action,
323
+ message: outcomeResult.message ?? outcomeResult.reason,
324
+ }));
325
+ if (!outcomeResult.ok)
326
+ process.exitCode = 1;
327
+ return;
328
+ }
329
+ case "install": {
330
+ banner(t("asTitle"));
331
+ const settings = loadSettings(roots.config);
332
+ const result = realScheduler.install(roots, settings);
333
+ if (!result.ok) {
334
+ fail(t("autoInstallFailed", { error: result.detail.join("; ") }));
335
+ process.exitCode = 1;
336
+ return;
337
+ }
338
+ ok(t("asInstalled", { kind: result.kind }));
339
+ for (const d of result.detail)
340
+ detail(d);
341
+ for (const w of result.warnings)
342
+ warn(t("autoWarning", { text: w }));
343
+ if (!settings.autosync.enabled)
344
+ warn(`${t("autoOff")} opencode-sync autosync enable`);
345
+ write();
346
+ return;
347
+ }
348
+ case "uninstall": {
349
+ banner(t("asTitle"));
350
+ const result = realScheduler.uninstall(roots);
351
+ ok(result.removed ? t("asUninstalled") : t("asNotInstalled"));
352
+ write();
353
+ return;
354
+ }
355
+ case "enable": {
356
+ banner(t("asTitle"));
357
+ const interval = Number(args.values.get("interval"));
358
+ if (Number.isFinite(interval) && interval >= 2) {
359
+ const s = loadSettings(roots.config);
360
+ s.autosync.intervalMinutes = Math.floor(interval);
361
+ saveSettings(s, roots.config);
362
+ }
363
+ if (!(await enableAutosync(ctx)))
364
+ process.exitCode = 1;
365
+ write();
366
+ return;
367
+ }
368
+ case "disable":
369
+ banner(t("asTitle"));
370
+ disableAutosync(ctx);
371
+ write();
372
+ return;
373
+ case "reset": {
374
+ banner(t("asTitle"));
375
+ const before = failureCount(readState(roots));
376
+ writeState(roots, {
377
+ consecutiveFailures: 0,
378
+ lastError: null,
379
+ failureNotifiedAt: null,
380
+ lastAttemptAt: 0,
381
+ });
382
+ appendLog(roots, `reset: cleared ${before} failure(s)`);
383
+ ok(t("resetDone", { n: before }));
384
+ write();
385
+ return;
386
+ }
387
+ case "pause":
388
+ case "resume": {
389
+ banner(t("asTitle"));
390
+ const s = loadSettings(roots.config);
391
+ if (sub === "resume") {
392
+ s.autosync.pausedUntil = 0;
393
+ saveSettings(s, roots.config);
394
+ ok(t("resumed"));
395
+ }
396
+ else {
397
+ const arg = args.positional[1] ?? "1";
398
+ const until = arg === "tomorrow" ? tomorrowMorning() : Date.now() + Number(arg) * 3600_000;
399
+ if (!Number.isFinite(until) || until <= Date.now())
400
+ throw new SyncError(t("asPauseUsage"));
401
+ s.autosync.pausedUntil = until;
402
+ saveSettings(s, roots.config);
403
+ ok(t("pausedUntil", { time: formatTime(until) }));
404
+ }
405
+ write();
406
+ return;
407
+ }
408
+ case "status": {
409
+ banner(t("asTitle"));
410
+ const s = loadSettings(roots.config);
411
+ const state = readState(roots);
412
+ const sched = realScheduler.status();
413
+ keyValue(t("asEnabled"), s.autosync.enabled ? paint(t("on"), style.green) : paint(t("off"), style.muted), 20);
414
+ keyValue(t("asInterval"), t("intervalMinutes", { n: s.autosync.intervalMinutes }), 20);
415
+ keyValue(t("asScheduler"), sched.installed
416
+ ? `${t("diagRegistered", { kind: sched.kind })}${sched.nextRun ? ` · ${sched.nextRun}` : ""}`
417
+ : t("diagNotRegistered"), 20);
418
+ if (s.autosync.pausedUntil > Date.now())
419
+ keyValue(t("asPaused"), formatTime(s.autosync.pausedUntil), 20);
420
+ keyValue(t("asLastRun"), ago(state.lastRunAt), 20);
421
+ keyValue(t("asLastSuccess"), ago(state.lastSuccessAt), 20);
422
+ if (state.lastOutcome)
423
+ keyValue(t("asOutcome"), state.lastOutcome, 20);
424
+ const failures = failureCount(state);
425
+ if (failures)
426
+ keyValue(t("asFailures"), String(failures), 20);
427
+ const wait = backoffRemainingMs(state, Date.now());
428
+ if (wait > 0)
429
+ keyValue(t("asNextAttempt"), t("inMinutes", { n: Math.ceil(wait / 60000) }), 20);
430
+ if (state.lastError)
431
+ keyValue(t("asLastError"), paint(state.lastError, style.red), 20);
432
+ write();
433
+ return;
434
+ }
435
+ default:
436
+ throw new SyncError(t("asUsage"));
437
+ }
438
+ }
439
+ function setupFlags(args) {
440
+ const mode = (args.values.get("mode") ?? "cli");
441
+ if (!["cli", "link", "auto"].includes(mode))
442
+ throw new SetupError(t("invalidValue", { value: mode }));
443
+ const interval = args.values.get("interval");
444
+ const prefer = args.values.get("prefer");
445
+ return {
446
+ mode,
447
+ repo: args.values.get("repo") ?? args.positional[0],
448
+ interval: interval ? Number(interval) : undefined,
449
+ lang: args.values.get("lang"),
450
+ proxy: args.values.get("proxy"),
451
+ prefer: prefer === "local" ? "local" : prefer === "repo" ? "repo" : undefined,
452
+ };
453
+ }
249
454
  function runHelp() {
250
- banner("sync OpenCode across machines");
251
- heading("Setup");
252
- command("init [name|owner/repo]", "Create a private sync repository");
253
- command("link <owner/repo>", "Use an existing sync repository");
254
- heading("Sync");
255
- command("push", "Send this machine's configuration");
256
- command("pull", "Apply the shared configuration");
257
- command("status", "Show what is in sync and what is not");
258
- heading("Sessions");
259
- command("sessions list", "Show recent sessions and their ids");
260
- command("sessions enable | disable", "Turn selective session sync on or off");
261
- command("sessions include <id>", "Always sync a session, ignoring the time window");
262
- command("sessions exclude <id>", "Never sync a session");
263
- heading("Machine-local");
264
- command("overrides", "Open the per-machine configuration patch");
265
- command("config", "Show the current settings");
266
- heading("Flags");
267
- command("--force", "Overwrite the other side on conflict");
268
- command("--dry-run", "Report what would change without doing it");
455
+ banner(t("helpTitle"));
456
+ heading(t("helpSetup"));
457
+ command("(no arguments) | menu", t("helpMenu"));
458
+ command("init [name|owner/repo]", t("helpInit"));
459
+ command("link <owner/repo|url>", t("helpLink"));
460
+ command("setup --mode cli|link|auto", t("helpSetupCmd"));
461
+ heading(t("helpSync"));
462
+ command("push", t("helpPush"));
463
+ command("pull", t("helpPull"));
464
+ command("status", t("helpStatus"));
465
+ heading(t("helpAutosync"));
466
+ command("autosync install | uninstall", t("helpAsInstall"));
467
+ command("autosync enable | disable", t("helpAsToggle"));
468
+ command("autosync status", t("helpAsStatus"));
469
+ command("autosync run", t("helpAsRun"));
470
+ command("autosync pause <h> | resume", t("helpAsPause"));
471
+ command("autosync reset", t("helpAsReset"));
472
+ heading(t("helpSessions"));
473
+ command("sessions list", t("helpSesList"));
474
+ command("sessions enable | disable", t("helpSesToggle"));
475
+ command("sessions include <id>", t("helpSesInclude"));
476
+ command("sessions exclude <id>", t("helpSesExclude"));
477
+ command("repair-sessions [--apply]", t("helpRepair"));
478
+ heading(t("helpLocal"));
479
+ command("overrides", t("helpOverrides"));
480
+ command("config", t("helpConfig"));
481
+ heading(t("helpFlags"));
482
+ command("--force", t("helpForce"));
483
+ command("--dry-run", t("helpDryRun"));
484
+ command("--allow-large", t("helpAllowLarge"));
485
+ command("--yes", t("helpYes"));
486
+ command("--prefer repo|local", t("helpPrefer"));
487
+ command("--lang en|zh-CN", t("helpLang"));
269
488
  write();
270
489
  rule();
271
- write(` ${paint("docs", style.faint)} ${paint("https://github.com/doomsday616/opencode-github-sync", style.muted)}`);
490
+ write(` ${paint("docs", style.faint)} ${paint("https://github.com/lazyalex616/opencode-github-sync", style.muted)}`);
272
491
  write();
273
492
  }
274
493
  function command(name, description) {
275
- const padded = name.padEnd(30);
494
+ const padded = name.padEnd(32);
276
495
  write(` ${paint(padded, style.cyan)}${paint(description, style.muted)}`);
277
496
  }
278
497
  // ── Entry point ─────────────────────────────────────────────────────────────
279
498
  async function main() {
280
499
  const args = parseArgs(process.argv.slice(2));
500
+ const roots = getRoots();
501
+ applyLocalSettings(safeLoadSettings(roots), args.values.get("lang"));
281
502
  switch (args.command) {
503
+ case "menu": {
504
+ const ui = newUi();
505
+ try {
506
+ await runMenu({ ui, roots });
507
+ }
508
+ finally {
509
+ ui.close();
510
+ }
511
+ return;
512
+ }
513
+ case "setup": {
514
+ const ui = newUi();
515
+ try {
516
+ await runSetup(setupFlags(args), { ui, roots });
517
+ }
518
+ finally {
519
+ ui.close();
520
+ }
521
+ return;
522
+ }
282
523
  case "push":
283
524
  case "pull":
284
525
  await runSync(args.command, args);
@@ -295,6 +536,12 @@ async function main() {
295
536
  case "sessions":
296
537
  await runSessions(args);
297
538
  return;
539
+ case "repair-sessions":
540
+ await runRepair(args);
541
+ return;
542
+ case "autosync":
543
+ await runAutosyncCommand(args);
544
+ return;
298
545
  case "overrides":
299
546
  runOverrides();
300
547
  return;
@@ -308,9 +555,9 @@ async function main() {
308
555
  return;
309
556
  default:
310
557
  banner();
311
- fail(`Unknown command: ${args.command}`);
558
+ fail(t("cliUnknown", { cmd: args.command }));
312
559
  write();
313
- info("Run `opencode-sync help` to see everything.");
560
+ info(t("cliHelpHint"));
314
561
  write();
315
562
  process.exitCode = 1;
316
563
  }