nemu-cli 1.5.0 → 1.8.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.
- package/CHANGELOG.md +198 -116
- package/LICENSE +21 -21
- package/README.md +273 -235
- package/dist/args.js +9 -1
- package/dist/clients/claude-plugin.js +68 -0
- package/dist/clients/registry.js +9 -1
- package/dist/commands/login.js +9 -4
- package/dist/commands/private.js +259 -0
- package/dist/commands/status.js +42 -4
- package/dist/commands/sync.js +207 -0
- package/dist/commands/vault.js +257 -0
- package/dist/device.js +15 -9
- package/dist/http.js +32 -20
- package/dist/index.js +117 -100
- package/dist/vault/api.js +300 -0
- package/dist/vault/connect.js +59 -0
- package/dist/vault/engine.js +636 -0
- package/dist/vault/files.js +187 -0
- package/dist/vault/ignore.js +103 -0
- package/dist/vault/lock.js +85 -0
- package/dist/vault/merge.js +236 -0
- package/dist/vault/paths.js +99 -0
- package/dist/vault/plan.js +279 -0
- package/dist/vault/private/api.js +110 -0
- package/dist/vault/private/crypto.js +212 -0
- package/dist/vault/private/keystore.js +104 -0
- package/dist/vault/private/locks.js +59 -0
- package/dist/vault/private/strength.js +22 -0
- package/dist/vault/state.js +118 -0
- package/dist/vault/watch.js +101 -0
- package/dist/version.js +1 -1
- package/package.json +54 -48
- package/plugin/.claude-plugin/marketplace.json +14 -0
- package/plugin/nemu/.claude-plugin/plugin.json +8 -0
- package/plugin/nemu/.mcp.json +8 -0
- package/plugin/nemu/commands/inspo.md +22 -0
- package/plugin/nemu/commands/remember.md +16 -0
package/dist/commands/login.js
CHANGED
|
@@ -5,7 +5,7 @@ import { login_key } from "./key.js";
|
|
|
5
5
|
import { verify_device } from "../device.js";
|
|
6
6
|
import { bool_flag } from "../args.js";
|
|
7
7
|
import { done, is_json_mode } from "../output.js";
|
|
8
|
-
import { bold, cyan, dim, open_browser, out, spinner, success, } from "../ui.js";
|
|
8
|
+
import { bold, cyan, dim, open_browser, out, red, spinner, success, } from "../ui.js";
|
|
9
9
|
async function account_name(nemu) {
|
|
10
10
|
try {
|
|
11
11
|
const account = await nemu.user.get();
|
|
@@ -82,21 +82,26 @@ export async function login(args) {
|
|
|
82
82
|
});
|
|
83
83
|
const scope_list = (tokens.scope ?? "").split(/\s+/);
|
|
84
84
|
let device_id = null;
|
|
85
|
+
let device_error = null;
|
|
85
86
|
if (scope_list.includes("context:write")) {
|
|
86
87
|
try {
|
|
87
88
|
device_id = await verify_device();
|
|
88
89
|
}
|
|
89
|
-
catch {
|
|
90
|
-
|
|
90
|
+
catch (error) {
|
|
91
|
+
device_error = error instanceof Error ? error.message : "Unexpected error.";
|
|
91
92
|
}
|
|
92
93
|
}
|
|
93
94
|
if (is_json_mode()) {
|
|
94
|
-
done("Logged in", { account, path, device_id });
|
|
95
|
+
done("Logged in", { account, path, device_id, device_error });
|
|
95
96
|
return 0;
|
|
96
97
|
}
|
|
97
98
|
success(`Logged in${account !== null ? ` as ${bold(account)}` : ""}`);
|
|
98
99
|
out(dim(` Token saved to ${path}`));
|
|
99
100
|
if (device_id !== null)
|
|
100
101
|
out(dim(` This computer is registered as ${device_id}`));
|
|
102
|
+
if (device_error !== null) {
|
|
103
|
+
out(` ${red("This computer could not be registered for Context:")} ${device_error}`);
|
|
104
|
+
out(dim(" Context will refuse this computer until it is. Run nemu login --force to try again."));
|
|
105
|
+
}
|
|
101
106
|
return 0;
|
|
102
107
|
}
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
import { randomBytes, randomUUID } from "node:crypto";
|
|
2
|
+
import { chmod, writeFile } from "node:fs/promises";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { device_name } from "../device.js";
|
|
6
|
+
import { CliError } from "../errors.js";
|
|
7
|
+
import { emit, is_json_mode } from "../output.js";
|
|
8
|
+
import { ask_secret, bold, confirm, dim, is_interactive, out, spinner, success } from "../ui.js";
|
|
9
|
+
import { create_vault, LockApi } from "../vault/api.js";
|
|
10
|
+
import { PrivateCryptoError, approval_code, commitment_of, format_recovery, new_request, new_request_keys, new_vault_key, unbase32, unwrap_from_request, wrap_for_request, } from "../vault/private/crypto.js";
|
|
11
|
+
import { forget_vault_key, load_vault_key, save_vault_key } from "../vault/private/keystore.js";
|
|
12
|
+
import { open_password_lock, open_recovery_lock, password_lock, recovery_lock } from "../vault/private/locks.js";
|
|
13
|
+
import { password_problem } from "../vault/private/strength.js";
|
|
14
|
+
const PASSWORD_ENV = "NEMU_VAULT_PASSWORD";
|
|
15
|
+
const RECOVERY_ENV = "NEMU_VAULT_RECOVERY";
|
|
16
|
+
const POLL_MS = 2000;
|
|
17
|
+
async function new_password(vault_name) {
|
|
18
|
+
const from_env = process.env[PASSWORD_ENV];
|
|
19
|
+
if (from_env !== undefined) {
|
|
20
|
+
const problem = password_problem(from_env, [vault_name]);
|
|
21
|
+
if (problem !== null)
|
|
22
|
+
throw new CliError(problem, "weak_password");
|
|
23
|
+
return from_env;
|
|
24
|
+
}
|
|
25
|
+
if (!is_interactive()) {
|
|
26
|
+
throw new CliError(`Set ${PASSWORD_ENV}, or run this in a terminal to choose the vault password.`, "password_required");
|
|
27
|
+
}
|
|
28
|
+
out(dim(" Choose a vault password. It is not your Nemu password, and Nemu never sees it."));
|
|
29
|
+
for (;;) {
|
|
30
|
+
const first = await ask_secret("Vault password:");
|
|
31
|
+
const problem = password_problem(first, [vault_name]);
|
|
32
|
+
if (problem !== null) {
|
|
33
|
+
out(` ${problem}`);
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
const again = await ask_secret("Type it again:");
|
|
37
|
+
if (again === first)
|
|
38
|
+
return first;
|
|
39
|
+
out(" Those two did not match. Try again.");
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
async function save_recovery(vault_id, vault_name, recovery) {
|
|
43
|
+
const safe = vault_name.replace(/[^\w .-]/g, "").trim() || "vault";
|
|
44
|
+
const text = [
|
|
45
|
+
`Recovery key for the private vault "${vault_name}" (${vault_id})`,
|
|
46
|
+
"",
|
|
47
|
+
format_recovery(recovery),
|
|
48
|
+
"",
|
|
49
|
+
"This opens the vault if you lose every device, passkey and the vault password.",
|
|
50
|
+
"Nemu cannot recover it for you. Keep it somewhere offline and delete this file.",
|
|
51
|
+
"",
|
|
52
|
+
].join("\n");
|
|
53
|
+
for (let attempt = 0; attempt < 50; attempt += 1) {
|
|
54
|
+
const suffix = attempt === 0 ? "" : ` (${attempt + 1})`;
|
|
55
|
+
const path = join(homedir(), `Nemu recovery key - ${safe} - ${vault_id.slice(0, 8)}${suffix}.txt`);
|
|
56
|
+
try {
|
|
57
|
+
await writeFile(path, text, { mode: 0o600, flag: "wx" });
|
|
58
|
+
await chmod(path, 0o600).catch(() => undefined);
|
|
59
|
+
return path;
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
if (error.code !== "EEXIST")
|
|
63
|
+
throw error;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
throw new CliError("Could not save the recovery key file. It is shown above: write it down.", "recovery_file_failed");
|
|
67
|
+
}
|
|
68
|
+
export async function create_private_vault(transport, name) {
|
|
69
|
+
const password = await new_password(name);
|
|
70
|
+
const vault_id = randomUUID();
|
|
71
|
+
const vault_key = new_vault_key();
|
|
72
|
+
const recovery = randomBytes(32);
|
|
73
|
+
const working = spinner("Sealing the vault key");
|
|
74
|
+
const locks = [await password_lock(vault_id, vault_key, password), recovery_lock(vault_id, vault_key, recovery)];
|
|
75
|
+
working.stop();
|
|
76
|
+
const vault = await create_vault(transport, name, { id: vault_id, locks });
|
|
77
|
+
if (vault.id !== vault_id)
|
|
78
|
+
throw new CliError("Nemu did not keep the private vault's id.", "invalid_response");
|
|
79
|
+
await save_vault_key(vault.id, vault_key);
|
|
80
|
+
const recovery_path = await save_recovery(vault.id, name, recovery);
|
|
81
|
+
if (!is_json_mode()) {
|
|
82
|
+
out();
|
|
83
|
+
out(` ${bold("Recovery key")} ${format_recovery(recovery)}`);
|
|
84
|
+
out(dim(` Also saved to ${recovery_path}. Move it somewhere offline: it is the only way back if every other lock is lost.`));
|
|
85
|
+
}
|
|
86
|
+
return vault;
|
|
87
|
+
}
|
|
88
|
+
async function read_recovery() {
|
|
89
|
+
const typed = process.env[RECOVERY_ENV] ?? (is_interactive() ? await ask_secret("Recovery key:") : "");
|
|
90
|
+
const recovery = unbase32(typed);
|
|
91
|
+
if (recovery.length !== 32)
|
|
92
|
+
throw new CliError("That is not a recovery key.", "bad_recovery_key");
|
|
93
|
+
return recovery;
|
|
94
|
+
}
|
|
95
|
+
async function wait_for(deadline, step) {
|
|
96
|
+
while (Date.now() < deadline) {
|
|
97
|
+
const found = await step();
|
|
98
|
+
if (found !== null)
|
|
99
|
+
return found;
|
|
100
|
+
await new Promise((resolve) => setTimeout(resolve, POLL_MS));
|
|
101
|
+
}
|
|
102
|
+
throw new CliError("Nobody finished the approval in time. Start again.", "request_expired");
|
|
103
|
+
}
|
|
104
|
+
async function approved_key(locks) {
|
|
105
|
+
if (!is_interactive()) {
|
|
106
|
+
throw new CliError("Approval needs you to compare a code, so run nemu vault unlock --approve in a terminal.", "needs_terminal");
|
|
107
|
+
}
|
|
108
|
+
const request = new_request();
|
|
109
|
+
const asked = await locks.ask(request.commitment, device_name().slice(0, 80) || "A computer");
|
|
110
|
+
const deadline = Date.parse(asked.expires_at);
|
|
111
|
+
if (is_json_mode())
|
|
112
|
+
emit({ waiting: true, request_id: asked.id, expires_at: asked.expires_at });
|
|
113
|
+
else {
|
|
114
|
+
out(" On a device where this vault is open, approve this computer:");
|
|
115
|
+
out(dim(" in the console under the vault's Devices tab, or with nemu vault approve. This waits up to 10 minutes."));
|
|
116
|
+
}
|
|
117
|
+
const approver_key = await wait_for(deadline, async () => {
|
|
118
|
+
const answer = await locks.answer_of(asked.id);
|
|
119
|
+
if (answer.status === "denied")
|
|
120
|
+
throw new CliError("The request was declined.", "request_denied");
|
|
121
|
+
if (answer.status === "expired")
|
|
122
|
+
throw new CliError("The request expired. Start again.", "request_expired");
|
|
123
|
+
return answer.approver_key ?? null;
|
|
124
|
+
});
|
|
125
|
+
await locks.reveal(asked.id, request.keys.public_key, request.nonce);
|
|
126
|
+
const code = approval_code(locks.vault_id, asked.id, request.keys.public_key, approver_key, request.nonce);
|
|
127
|
+
out(` This computer shows ${bold(code)}.`);
|
|
128
|
+
if (!(await confirm(`Does the approving device show ${code} too?`, false))) {
|
|
129
|
+
await locks.answer(asked.id, null).catch(() => undefined);
|
|
130
|
+
throw new CliError("The codes did not match, so nothing was accepted. Start again.", "approval_mismatch");
|
|
131
|
+
}
|
|
132
|
+
out(dim(" Now approve it on the other device."));
|
|
133
|
+
return wait_for(deadline, async () => {
|
|
134
|
+
const answer = await locks.answer_of(asked.id);
|
|
135
|
+
if (answer.status === "denied")
|
|
136
|
+
throw new CliError("The request was declined.", "request_denied");
|
|
137
|
+
if (answer.status === "expired")
|
|
138
|
+
throw new CliError("The request expired. Start again.", "request_expired");
|
|
139
|
+
if (answer.approver_key !== undefined && answer.approver_key !== approver_key) {
|
|
140
|
+
throw new CliError("The approving device changed during the approval. Nothing was accepted.", "approval_tampered");
|
|
141
|
+
}
|
|
142
|
+
if (answer.wrapped === undefined)
|
|
143
|
+
return null;
|
|
144
|
+
try {
|
|
145
|
+
return unwrap_from_request(locks.vault_id, asked.id, request.keys, approver_key, Buffer.from(answer.wrapped, "base64url"));
|
|
146
|
+
}
|
|
147
|
+
catch {
|
|
148
|
+
throw new CliError("The approval did not come from the device whose code you checked. Nothing was accepted.", "approval_tampered");
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
function method_from(flags) {
|
|
153
|
+
if (flags.password)
|
|
154
|
+
return "password";
|
|
155
|
+
if (flags.recovery)
|
|
156
|
+
return "recovery";
|
|
157
|
+
if (flags.approve)
|
|
158
|
+
return "approve";
|
|
159
|
+
if (process.env[PASSWORD_ENV] !== undefined)
|
|
160
|
+
return "password";
|
|
161
|
+
if (process.env[RECOVERY_ENV] !== undefined)
|
|
162
|
+
return "recovery";
|
|
163
|
+
return null;
|
|
164
|
+
}
|
|
165
|
+
export async function unlock_vault(transport, vault_id, vault_name, flags) {
|
|
166
|
+
if ((await load_vault_key(vault_id)) !== null) {
|
|
167
|
+
if (!is_json_mode())
|
|
168
|
+
success(`"${vault_name}" is already open on this computer.`);
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
const locks = new LockApi(transport, vault_id);
|
|
172
|
+
const available = await locks.locks();
|
|
173
|
+
const chosen = method_from(flags) ??
|
|
174
|
+
(available.some((lock) => lock.kind === "password") && is_interactive()
|
|
175
|
+
? (await confirm("Unlock with the vault password?", true))
|
|
176
|
+
? "password"
|
|
177
|
+
: (await confirm("Use the recovery key?", false))
|
|
178
|
+
? "recovery"
|
|
179
|
+
: "approve"
|
|
180
|
+
: "approve");
|
|
181
|
+
let vault_key;
|
|
182
|
+
if (chosen === "approve") {
|
|
183
|
+
vault_key = await approved_key(locks);
|
|
184
|
+
}
|
|
185
|
+
else {
|
|
186
|
+
const lock = available.find((entry) => entry.kind === chosen);
|
|
187
|
+
if (lock === undefined)
|
|
188
|
+
throw new CliError(`This vault has no ${chosen} lock.`, "no_such_lock");
|
|
189
|
+
const wrapped = await locks.wrapped(lock.id);
|
|
190
|
+
try {
|
|
191
|
+
vault_key =
|
|
192
|
+
chosen === "password"
|
|
193
|
+
? await open_password_lock(vault_id, lock.params, wrapped, process.env[PASSWORD_ENV] ?? (await ask_secret("Vault password:")))
|
|
194
|
+
: open_recovery_lock(vault_id, lock.params, wrapped, await read_recovery());
|
|
195
|
+
}
|
|
196
|
+
catch (error) {
|
|
197
|
+
if (error instanceof CliError)
|
|
198
|
+
throw error;
|
|
199
|
+
if (error instanceof PrivateCryptoError && error.code === "bad_lock") {
|
|
200
|
+
throw new CliError("This vault's password lock is damaged or was changed on the server, so it was not used. Unlock with the recovery key or another device.", "bad_lock");
|
|
201
|
+
}
|
|
202
|
+
throw new CliError(chosen === "password" ? "That is not the vault password." : "That recovery key does not open this vault.", "wrong_secret");
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
const store = await save_vault_key(vault_id, vault_key);
|
|
206
|
+
if (is_json_mode())
|
|
207
|
+
emit({ ok: true, vault_id, unlocked: true, keystore: store });
|
|
208
|
+
else
|
|
209
|
+
success(`"${vault_name}" is open on this computer. Its key is kept in ${store === "keychain" ? "the macOS Keychain" : store === "dpapi" ? "Windows, protected for your account" : "a file only your account can read"}.`);
|
|
210
|
+
}
|
|
211
|
+
export async function approve_requests(transport, vault_id, vault_name) {
|
|
212
|
+
const vault_key = await load_vault_key(vault_id);
|
|
213
|
+
if (vault_key === null) {
|
|
214
|
+
throw new CliError(`"${vault_name}" is locked on this computer, so it cannot approve another one.`, "vault_locked");
|
|
215
|
+
}
|
|
216
|
+
if (!is_interactive()) {
|
|
217
|
+
throw new CliError("Approving a device needs you to compare a code, so run this in a terminal.", "needs_terminal");
|
|
218
|
+
}
|
|
219
|
+
const locks = new LockApi(transport, vault_id);
|
|
220
|
+
const pending = (await locks.requests()).filter((request) => request.approver_key === null);
|
|
221
|
+
if (pending.length === 0) {
|
|
222
|
+
out(" No device is waiting to open this vault.");
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
let approved = 0;
|
|
226
|
+
for (const request of pending) {
|
|
227
|
+
if (!(await confirm(`"${request.label}" asks to open this vault. Did you just ask for it?`, false))) {
|
|
228
|
+
await locks.answer(request.id, null).catch(() => undefined);
|
|
229
|
+
continue;
|
|
230
|
+
}
|
|
231
|
+
const approver = new_request_keys();
|
|
232
|
+
await locks.offer(request.id, approver.public_key);
|
|
233
|
+
const revealed = await wait_for(Date.parse(request.expires_at), async () => {
|
|
234
|
+
const current = await locks.request(request.id);
|
|
235
|
+
if (current === null)
|
|
236
|
+
throw new CliError("The request is gone.", "request_expired");
|
|
237
|
+
return current.public_key !== null && current.nonce !== null ? current : null;
|
|
238
|
+
});
|
|
239
|
+
if (commitment_of(revealed.public_key, revealed.nonce) !== request.commitment) {
|
|
240
|
+
await locks.answer(request.id, null).catch(() => undefined);
|
|
241
|
+
throw new CliError("The asking device's key changed after it asked. It was declined.", "approval_tampered");
|
|
242
|
+
}
|
|
243
|
+
const code = approval_code(vault_id, request.id, revealed.public_key, approver.public_key, revealed.nonce);
|
|
244
|
+
if (!(await confirm(`Does "${request.label}" show ${code}?`, false))) {
|
|
245
|
+
await locks.answer(request.id, null).catch(() => undefined);
|
|
246
|
+
continue;
|
|
247
|
+
}
|
|
248
|
+
await locks.answer(request.id, wrap_for_request(vault_id, request.id, approver, revealed.public_key, vault_key).toString("base64url"));
|
|
249
|
+
approved += 1;
|
|
250
|
+
}
|
|
251
|
+
success(`Approved ${approved} device${approved === 1 ? "" : "s"}.`);
|
|
252
|
+
}
|
|
253
|
+
export async function forget_key(vault_id, vault_name) {
|
|
254
|
+
await forget_vault_key(vault_id);
|
|
255
|
+
if (is_json_mode())
|
|
256
|
+
emit({ ok: true, vault_id, unlocked: false });
|
|
257
|
+
else
|
|
258
|
+
success(`"${vault_name}" is locked on this computer again. Your files are still there.`);
|
|
259
|
+
}
|
package/dist/commands/status.js
CHANGED
|
@@ -4,11 +4,44 @@ import { CLIENTS, client_installed } from "../clients/registry.js";
|
|
|
4
4
|
import { EDITORS, editor_has_nemu } from "../clients/editors.js";
|
|
5
5
|
import { run, which } from "../system.js";
|
|
6
6
|
import { VERSION } from "../version.js";
|
|
7
|
+
import { device_state } from "../device.js";
|
|
8
|
+
import { error_code } from "../errors.js";
|
|
7
9
|
import { bold, dim, green, out, red } from "../ui.js";
|
|
8
10
|
import { emit, is_json_mode } from "../output.js";
|
|
9
11
|
function mark(ok) {
|
|
10
12
|
return ok ? green("ok") : red("no");
|
|
11
13
|
}
|
|
14
|
+
async function check_device() {
|
|
15
|
+
try {
|
|
16
|
+
return { checked: true, result: await device_state() };
|
|
17
|
+
}
|
|
18
|
+
catch (error) {
|
|
19
|
+
return {
|
|
20
|
+
checked: false,
|
|
21
|
+
code: error_code(error),
|
|
22
|
+
message: error instanceof Error ? error.message : "Unexpected error.",
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
function device_line(device_id, check) {
|
|
27
|
+
const local = device_id ?? red("not registered");
|
|
28
|
+
if (check === null)
|
|
29
|
+
return local;
|
|
30
|
+
if (!check.checked) {
|
|
31
|
+
return check.code === "not_signed_in" ? local : `${local}${dim(` could not check: ${check.message}`)}`;
|
|
32
|
+
}
|
|
33
|
+
const fix = dim(" run nemu login --force");
|
|
34
|
+
switch (check.result.state) {
|
|
35
|
+
case "active":
|
|
36
|
+
return check.result.id;
|
|
37
|
+
case "revoked":
|
|
38
|
+
return `${check.result.id} ${red("revoked")}${fix}`;
|
|
39
|
+
case "unknown":
|
|
40
|
+
return `${check.result.id} ${red("not known to Nemu")}${fix}`;
|
|
41
|
+
default:
|
|
42
|
+
return `${red("not registered")}${fix}`;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
12
45
|
export async function status() {
|
|
13
46
|
const credentials = await read_credentials();
|
|
14
47
|
const settings = await read_settings();
|
|
@@ -39,13 +72,18 @@ export async function status() {
|
|
|
39
72
|
config: editor.config_path(),
|
|
40
73
|
};
|
|
41
74
|
}));
|
|
75
|
+
const context_access = credentials !== null && has_scope(credentials, "context:write");
|
|
76
|
+
const check = context_access && credentials.kind !== "key" ? await check_device() : null;
|
|
77
|
+
const is_session_expired = check !== null && !check.checked && check.code === "not_signed_in";
|
|
42
78
|
const report = {
|
|
43
79
|
version: VERSION,
|
|
44
80
|
api_url: credentials?.api_url ?? api_url(),
|
|
45
|
-
signed_in: credentials !== null,
|
|
81
|
+
signed_in: credentials !== null && !is_session_expired,
|
|
46
82
|
account: credentials?.account ?? null,
|
|
47
|
-
context_access
|
|
83
|
+
context_access,
|
|
48
84
|
device_id: settings.device_id ?? null,
|
|
85
|
+
device_state: check === null ? null : check.checked ? check.result.state : "unchecked",
|
|
86
|
+
device_check_error: check !== null && !check.checked ? check.message : null,
|
|
49
87
|
reports: settings.reports,
|
|
50
88
|
global_install: global,
|
|
51
89
|
global_version,
|
|
@@ -58,9 +96,9 @@ export async function status() {
|
|
|
58
96
|
}
|
|
59
97
|
out();
|
|
60
98
|
out(` ${bold("nemu")} ${dim(VERSION)} ${dim(report.api_url)}`);
|
|
61
|
-
out(` Signed in ${mark(report.signed_in)}${report.account !== null ? ` ${report.account}` : ""}`);
|
|
99
|
+
out(` Signed in ${is_session_expired ? red("expired") : mark(report.signed_in)}${report.account !== null ? ` ${report.account}` : ""}${is_session_expired ? dim(" run nemu login") : ""}`);
|
|
62
100
|
out(` Context access ${mark(report.context_access)}${report.signed_in && !report.context_access ? dim(" run nemu login --force") : ""}`);
|
|
63
|
-
out(` This device ${report.device_id
|
|
101
|
+
out(` This device ${device_line(report.device_id, check)}`);
|
|
64
102
|
out(` Global install ${mark(global !== null)}${global !== null ? dim(` ${global} ${global_version ?? ""}`) : dim(" npm i -g nemu-cli")}`);
|
|
65
103
|
out(` Error reports ${report.reports ? "on" : "off"}`);
|
|
66
104
|
for (const client of clients) {
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import { resolve } from "node:path";
|
|
2
|
+
import { bool_flag, flag } from "../args.js";
|
|
3
|
+
import { device_name } from "../device.js";
|
|
4
|
+
import { CliError } from "../errors.js";
|
|
5
|
+
import { emit, is_json_mode } from "../output.js";
|
|
6
|
+
import { bold, cyan, dim, fail, green, out, red } from "../ui.js";
|
|
7
|
+
import { open_linked } from "../vault/connect.js";
|
|
8
|
+
import { run_sync } from "../vault/engine.js";
|
|
9
|
+
import { find_root, read_marker } from "../vault/state.js";
|
|
10
|
+
import { watch_folder } from "../vault/watch.js";
|
|
11
|
+
function settings_from(args) {
|
|
12
|
+
const settings = {};
|
|
13
|
+
const conflicts = flag(args, "conflicts");
|
|
14
|
+
if (conflicts !== undefined) {
|
|
15
|
+
if (conflicts !== "merge" && conflicts !== "file") {
|
|
16
|
+
throw new CliError("--conflicts is merge or file.", "invalid_arguments");
|
|
17
|
+
}
|
|
18
|
+
settings.conflicts = conflicts;
|
|
19
|
+
}
|
|
20
|
+
const obsidian = flag(args, "obsidian");
|
|
21
|
+
if (obsidian !== undefined || bool_flag(args, "obsidian")) {
|
|
22
|
+
if (obsidian !== undefined && obsidian !== "on" && obsidian !== "off") {
|
|
23
|
+
throw new CliError("--obsidian is on or off.", "invalid_arguments");
|
|
24
|
+
}
|
|
25
|
+
settings.obsidian = obsidian !== "off";
|
|
26
|
+
}
|
|
27
|
+
const plugins = flag(args, "obsidian-plugins");
|
|
28
|
+
if (plugins !== undefined || bool_flag(args, "obsidian-plugins")) {
|
|
29
|
+
if (plugins !== undefined && plugins !== "on" && plugins !== "off") {
|
|
30
|
+
throw new CliError("--obsidian-plugins is on or off.", "invalid_arguments");
|
|
31
|
+
}
|
|
32
|
+
settings.obsidian_plugins = plugins !== "off";
|
|
33
|
+
}
|
|
34
|
+
return settings;
|
|
35
|
+
}
|
|
36
|
+
async function root_of(args) {
|
|
37
|
+
const raw = args.positional[0];
|
|
38
|
+
if (raw !== undefined) {
|
|
39
|
+
const folder = resolve(raw);
|
|
40
|
+
if ((await read_marker(folder)) === null) {
|
|
41
|
+
throw new CliError(`${folder} has no .nemu/vault.json, so it is not a linked vault folder. Nothing was synced. If it lives on a drive, check that the drive is mounted.`, "not_linked");
|
|
42
|
+
}
|
|
43
|
+
return folder;
|
|
44
|
+
}
|
|
45
|
+
const found = await find_root(process.cwd());
|
|
46
|
+
if (found === null) {
|
|
47
|
+
throw new CliError("This folder is not linked to a vault. Run nemu vault link <folder> first, or pass the folder to nemu sync.", "not_linked");
|
|
48
|
+
}
|
|
49
|
+
return found;
|
|
50
|
+
}
|
|
51
|
+
function print_event(event) {
|
|
52
|
+
switch (event.kind) {
|
|
53
|
+
case "pulled":
|
|
54
|
+
out(` ${cyan("↓")} ${event.path}`);
|
|
55
|
+
return;
|
|
56
|
+
case "pushed":
|
|
57
|
+
out(` ${green("↑")} ${event.path}`);
|
|
58
|
+
return;
|
|
59
|
+
case "restored":
|
|
60
|
+
out(` ${green("↑")} ${event.path} ${dim("(restored, edited here after it was deleted elsewhere)")}`);
|
|
61
|
+
return;
|
|
62
|
+
case "merged":
|
|
63
|
+
out(` ${green("⇄")} ${event.path} ${dim("(merged edits from both sides)")}`);
|
|
64
|
+
return;
|
|
65
|
+
case "deleted_local":
|
|
66
|
+
out(` ${red("−")} ${event.path} ${dim("(deleted elsewhere)")}`);
|
|
67
|
+
return;
|
|
68
|
+
case "deleted_remote":
|
|
69
|
+
out(` ${red("−")} ${event.path} ${dim("(deleted here, removed from the vault; history keeps it)")}`);
|
|
70
|
+
return;
|
|
71
|
+
case "moved":
|
|
72
|
+
out(` ${cyan("→")} ${event.from} ${dim("to")} ${event.to}`);
|
|
73
|
+
return;
|
|
74
|
+
case "conflict":
|
|
75
|
+
out(` ${red("!")} ${event.path} ${dim("conflict: your version is kept as")} ${event.copy}`);
|
|
76
|
+
return;
|
|
77
|
+
case "skipped":
|
|
78
|
+
out(dim(` · ${event.path}: ${event.reason}`));
|
|
79
|
+
return;
|
|
80
|
+
case "notice":
|
|
81
|
+
out(dim(` ${event.message}`));
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
function describe(action) {
|
|
86
|
+
switch (action.kind) {
|
|
87
|
+
case "pull":
|
|
88
|
+
return `download ${action.path}`;
|
|
89
|
+
case "push":
|
|
90
|
+
return `upload ${action.local.path}${action.reason === "new" ? " (new)" : ""}`;
|
|
91
|
+
case "merge":
|
|
92
|
+
return `merge ${action.remote.path}`;
|
|
93
|
+
case "keep_both":
|
|
94
|
+
return `keep both versions of ${action.remote.path}; yours becomes ${action.copy_path}`;
|
|
95
|
+
case "delete_local":
|
|
96
|
+
return `delete ${action.disk} here (deleted elsewhere)`;
|
|
97
|
+
case "delete_remote":
|
|
98
|
+
return action.counted ? `delete ${action.path} from the vault` : null;
|
|
99
|
+
case "move_remote":
|
|
100
|
+
return `move ${action.from} to ${action.to} in the vault`;
|
|
101
|
+
case "rename_local":
|
|
102
|
+
return `move ${action.from} to ${action.to} here`;
|
|
103
|
+
case "adopt":
|
|
104
|
+
case "forget":
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
function plural(count, word) {
|
|
109
|
+
return `${count} ${word}${count === 1 ? "" : "s"}`;
|
|
110
|
+
}
|
|
111
|
+
function summary(report) {
|
|
112
|
+
const parts = [
|
|
113
|
+
report.pulled.length > 0 ? `${report.pulled.length} down` : "",
|
|
114
|
+
report.pushed.length > 0 ? `${report.pushed.length} up` : "",
|
|
115
|
+
report.merged.length > 0 ? `${report.merged.length} merged` : "",
|
|
116
|
+
report.moved.length > 0 ? `${report.moved.length} moved` : "",
|
|
117
|
+
report.deleted_local.length + report.deleted_remote.length > 0
|
|
118
|
+
? `${report.deleted_local.length + report.deleted_remote.length} deleted`
|
|
119
|
+
: "",
|
|
120
|
+
report.conflicts.length > 0 ? plural(report.conflicts.length, "conflict") : "",
|
|
121
|
+
].filter((part) => part.length > 0);
|
|
122
|
+
return parts.length === 0 ? "Already in sync" : parts.join(", ");
|
|
123
|
+
}
|
|
124
|
+
function print_report(name, report) {
|
|
125
|
+
if (report.dry_run) {
|
|
126
|
+
const lines = report.planned.map(describe).filter((line) => line !== null);
|
|
127
|
+
out(` ${bold(name)} ${dim("dry run, nothing was changed")}`);
|
|
128
|
+
if (lines.length === 0)
|
|
129
|
+
out(dim(" Already in sync."));
|
|
130
|
+
for (const line of lines)
|
|
131
|
+
out(` ${line}`);
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
if (report.waiting.length > 0) {
|
|
135
|
+
out(dim(` Still being written, synced on the next run: ${report.waiting.join(", ")}`));
|
|
136
|
+
}
|
|
137
|
+
if (report.blocked !== null)
|
|
138
|
+
fail(`Uploads stopped: ${report.blocked.message} Downloads continued.`);
|
|
139
|
+
out(`${green("✓")} ${bold(name)} ${summary(report)}`);
|
|
140
|
+
}
|
|
141
|
+
function json_report(report) {
|
|
142
|
+
const { planned, ...rest } = report;
|
|
143
|
+
return {
|
|
144
|
+
ok: true,
|
|
145
|
+
...rest,
|
|
146
|
+
...(report.dry_run ? { planned: planned.map(describe).filter((line) => line !== null) } : {}),
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
export async function sync(args) {
|
|
150
|
+
const root = await root_of(args);
|
|
151
|
+
const marker = (await read_marker(root));
|
|
152
|
+
const linked = await open_linked(marker);
|
|
153
|
+
const settings = settings_from(args);
|
|
154
|
+
const json = is_json_mode();
|
|
155
|
+
if (settings.obsidian_plugins === true && !json) {
|
|
156
|
+
out(` ${bold("Obsidian plugins will sync on this device.")} ${dim("Plugins are code Obsidian runs, so a plugin added on any synced device runs here too. Turn it off with --obsidian-plugins off.")}`);
|
|
157
|
+
}
|
|
158
|
+
const once = (lock) => run_sync({
|
|
159
|
+
root,
|
|
160
|
+
api: linked.api,
|
|
161
|
+
state_path: linked.state_path,
|
|
162
|
+
device: device_name(),
|
|
163
|
+
dry_run: bool_flag(args, "dry-run"),
|
|
164
|
+
allow_deletes: bool_flag(args, "allow-deletes"),
|
|
165
|
+
settings,
|
|
166
|
+
lock,
|
|
167
|
+
on_event: json ? undefined : print_event,
|
|
168
|
+
});
|
|
169
|
+
if (!bool_flag(args, "watch") || bool_flag(args, "dry-run")) {
|
|
170
|
+
const report = await once(true);
|
|
171
|
+
if (json)
|
|
172
|
+
emit(json_report(report));
|
|
173
|
+
else
|
|
174
|
+
print_report(marker.vault_name, report);
|
|
175
|
+
return report.blocked === null ? 0 : 2;
|
|
176
|
+
}
|
|
177
|
+
if (!json)
|
|
178
|
+
out(dim(` Watching ${root}. Press Ctrl+C to stop.`));
|
|
179
|
+
let exit = 0;
|
|
180
|
+
await watch_folder({
|
|
181
|
+
root,
|
|
182
|
+
run: async () => {
|
|
183
|
+
const report = await once(false);
|
|
184
|
+
const quiet = report.pulled.length + report.pushed.length + report.merged.length + report.moved.length +
|
|
185
|
+
report.deleted_local.length + report.deleted_remote.length + report.conflicts.length ===
|
|
186
|
+
0;
|
|
187
|
+
if (json)
|
|
188
|
+
emit(json_report(report));
|
|
189
|
+
else if (!quiet)
|
|
190
|
+
print_report(marker.vault_name, report);
|
|
191
|
+
},
|
|
192
|
+
on_error: (error) => {
|
|
193
|
+
const code = error instanceof CliError ? error.code : "unexpected";
|
|
194
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
195
|
+
if (json)
|
|
196
|
+
emit({ ok: false, error: message, code });
|
|
197
|
+
else
|
|
198
|
+
fail(message);
|
|
199
|
+
if (code === "sync_stopped" || code === "not_linked" || code === "server_mismatch") {
|
|
200
|
+
exit = 1;
|
|
201
|
+
return "stop";
|
|
202
|
+
}
|
|
203
|
+
return "continue";
|
|
204
|
+
},
|
|
205
|
+
});
|
|
206
|
+
return exit;
|
|
207
|
+
}
|