nemu-cli 1.6.1 → 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 -162
- package/LICENSE +21 -21
- package/README.md +273 -273
- 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 +10 -0
- package/dist/commands/vault.js +71 -3
- package/dist/device.js +14 -8
- package/dist/index.js +113 -109
- package/dist/vault/api.js +101 -5
- package/dist/vault/connect.js +19 -1
- package/dist/vault/engine.js +21 -19
- package/dist/vault/files.js +10 -10
- package/dist/vault/ignore.js +6 -3
- 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 +8 -2
- package/dist/version.js +1 -1
- package/package.json +54 -50
- 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) {
|
package/dist/commands/sync.js
CHANGED
|
@@ -24,6 +24,13 @@ function settings_from(args) {
|
|
|
24
24
|
}
|
|
25
25
|
settings.obsidian = obsidian !== "off";
|
|
26
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
|
+
}
|
|
27
34
|
return settings;
|
|
28
35
|
}
|
|
29
36
|
async function root_of(args) {
|
|
@@ -145,6 +152,9 @@ export async function sync(args) {
|
|
|
145
152
|
const linked = await open_linked(marker);
|
|
146
153
|
const settings = settings_from(args);
|
|
147
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
|
+
}
|
|
148
158
|
const once = (lock) => run_sync({
|
|
149
159
|
root,
|
|
150
160
|
api: linked.api,
|
package/dist/commands/vault.js
CHANGED
|
@@ -9,7 +9,9 @@ import { cli_transport, client_label, signed_in } from "../vault/connect.js";
|
|
|
9
9
|
import { is_conflict_copy } from "../vault/paths.js";
|
|
10
10
|
import { empty_state, find_root, marker_path, read_marker, read_state, remove_marker, remove_state, state_path_for, write_marker, write_state, } from "../vault/state.js";
|
|
11
11
|
import { is_default_ignored } from "../vault/ignore.js";
|
|
12
|
-
|
|
12
|
+
import { load_vault_key } from "../vault/private/keystore.js";
|
|
13
|
+
import { approve_requests, create_private_vault, forget_key, unlock_vault } from "./private.js";
|
|
14
|
+
const USAGE = "Use: nemu vault list | link <folder> [--vault <name|id>] [--create [--private]] [--obsidian] | unlink [folder] | status [folder] | unlock [folder] [--password|--recovery|--approve] | approve [folder] | forget [folder]";
|
|
13
15
|
function size_of(bytes) {
|
|
14
16
|
if (bytes === undefined)
|
|
15
17
|
return "";
|
|
@@ -44,6 +46,10 @@ async function link(args) {
|
|
|
44
46
|
const workspace = flag(args, "workspace", "w");
|
|
45
47
|
const wanted = flag(args, "vault");
|
|
46
48
|
const create = bool_flag(args, "create");
|
|
49
|
+
const is_private = bool_flag(args, "private");
|
|
50
|
+
if (is_private && !create) {
|
|
51
|
+
throw new CliError("--private makes a new vault, so it goes with --create.", "invalid_arguments");
|
|
52
|
+
}
|
|
47
53
|
const enclosing = await find_root(dirname(folder));
|
|
48
54
|
if (enclosing !== null && enclosing !== folder) {
|
|
49
55
|
throw new CliError(`${folder} is inside ${enclosing}, which is already linked to a vault.`, "already_linked");
|
|
@@ -54,7 +60,9 @@ async function link(args) {
|
|
|
54
60
|
const vaults = await list_vaults(transport);
|
|
55
61
|
let vault = pick_vault(vaults, wanted);
|
|
56
62
|
if (vault === null && create) {
|
|
57
|
-
vault =
|
|
63
|
+
vault = is_private
|
|
64
|
+
? await create_private_vault(transport, wanted ?? basename(folder))
|
|
65
|
+
: await create_vault(transport, wanted ?? basename(folder));
|
|
58
66
|
}
|
|
59
67
|
if (vault === null) {
|
|
60
68
|
const names = vaults.map((entry) => `"${entry.name}"`).join(", ");
|
|
@@ -81,6 +89,7 @@ async function link(args) {
|
|
|
81
89
|
workspace_id,
|
|
82
90
|
server: credentials.api_url,
|
|
83
91
|
linked_at: new Date().toISOString(),
|
|
92
|
+
mode: info.data.mode === "private" ? "private" : "standard",
|
|
84
93
|
});
|
|
85
94
|
if (previous === null || previous.root !== folder || existing === null) {
|
|
86
95
|
await write_state(state_path, empty_state(vault.id, folder, {
|
|
@@ -95,7 +104,10 @@ async function link(args) {
|
|
|
95
104
|
emit({ ok: true, folder, vault: { id: vault.id, name: vault.name }, workspace_id, marker: marker_path(folder) });
|
|
96
105
|
return 0;
|
|
97
106
|
}
|
|
98
|
-
success(`Linked ${folder} to the vault ${bold(vault.name)}`);
|
|
107
|
+
success(`Linked ${folder} to the ${info.data.mode === "private" ? "private " : ""}vault ${bold(vault.name)}`);
|
|
108
|
+
if (info.data.mode === "private" && (await load_vault_key(vault.id)) === null) {
|
|
109
|
+
out(dim(` It is locked on this computer. Run nemu vault unlock ${raw} first.`));
|
|
110
|
+
}
|
|
99
111
|
out(dim(` Run nemu sync ${raw} to sync it, or nemu sync ${raw} --watch to keep it in sync.`));
|
|
100
112
|
return 0;
|
|
101
113
|
}
|
|
@@ -146,6 +158,7 @@ async function status(args) {
|
|
|
146
158
|
throw new CliError(`${folder} is not linked to a vault.`, "not_linked");
|
|
147
159
|
const state = await read_state(state_path_for(marker.vault_id));
|
|
148
160
|
const conflicts = await conflict_copies(folder);
|
|
161
|
+
const marker_mode = await vault_mode(marker);
|
|
149
162
|
const summary = {
|
|
150
163
|
folder,
|
|
151
164
|
vault: { id: marker.vault_id, name: marker.vault_name },
|
|
@@ -154,7 +167,10 @@ async function status(args) {
|
|
|
154
167
|
tracked: state === null ? 0 : Object.keys(state.files).length,
|
|
155
168
|
last_sync: state?.synced_at ?? null,
|
|
156
169
|
conflicts_mode: state?.settings.conflicts ?? "merge",
|
|
170
|
+
private: marker_mode === "private",
|
|
171
|
+
unlocked: marker_mode === "private" ? (await load_vault_key(marker.vault_id)) !== null : null,
|
|
157
172
|
obsidian_settings: state?.settings.obsidian ?? false,
|
|
173
|
+
obsidian_plugins: state?.settings.obsidian_plugins ?? false,
|
|
158
174
|
conflicts,
|
|
159
175
|
};
|
|
160
176
|
if (is_json_mode()) {
|
|
@@ -164,9 +180,15 @@ async function status(args) {
|
|
|
164
180
|
out(` ${bold(marker.vault_name)} ${dim(marker.vault_id)}`);
|
|
165
181
|
out(` ${dim("folder ")} ${folder}`);
|
|
166
182
|
out(` ${dim("files ")} ${summary.tracked}`);
|
|
183
|
+
if (summary.private) {
|
|
184
|
+
out(` ${dim("private ")} ${summary.unlocked === true ? "open on this computer" : "locked on this computer (nemu vault unlock)"}`);
|
|
185
|
+
}
|
|
167
186
|
out(` ${dim("last sync")} ${summary.last_sync ?? "never"}`);
|
|
168
187
|
out(` ${dim("conflicts")} ${summary.conflicts_mode === "merge" ? "merge when possible" : "always write a conflict file"}`);
|
|
169
188
|
out(` ${dim(".obsidian")} ${summary.obsidian_settings ? "synced" : "not synced on this device"}`);
|
|
189
|
+
if (summary.obsidian_settings) {
|
|
190
|
+
out(` ${dim("plugins ")} ${summary.obsidian_plugins ? "synced" : "not synced (plugins are code)"}`);
|
|
191
|
+
}
|
|
170
192
|
if (conflicts.length > 0) {
|
|
171
193
|
out();
|
|
172
194
|
out(` ${bold(`${conflicts.length} open conflict${conflicts.length === 1 ? "" : "s"}`)} ${dim("(delete the copy you do not want)")}`);
|
|
@@ -175,6 +197,46 @@ async function status(args) {
|
|
|
175
197
|
}
|
|
176
198
|
return 0;
|
|
177
199
|
}
|
|
200
|
+
async function vault_mode(marker) {
|
|
201
|
+
try {
|
|
202
|
+
await signed_in();
|
|
203
|
+
const info = await new VaultApi(cli_transport(marker.workspace_id), marker.vault_id, client_label()).info();
|
|
204
|
+
return info.data.mode ?? "standard";
|
|
205
|
+
}
|
|
206
|
+
catch {
|
|
207
|
+
return "unknown";
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
async function linked_marker(args) {
|
|
211
|
+
const folder = await locate(args);
|
|
212
|
+
const marker = await read_marker(folder);
|
|
213
|
+
if (marker === null)
|
|
214
|
+
throw new CliError(`${folder} is not linked to a vault.`, "not_linked");
|
|
215
|
+
await signed_in();
|
|
216
|
+
return marker;
|
|
217
|
+
}
|
|
218
|
+
async function unlock(args) {
|
|
219
|
+
const marker = await linked_marker(args);
|
|
220
|
+
await unlock_vault(cli_transport(marker.workspace_id), marker.vault_id, marker.vault_name, {
|
|
221
|
+
password: bool_flag(args, "password"),
|
|
222
|
+
recovery: bool_flag(args, "recovery"),
|
|
223
|
+
approve: bool_flag(args, "approve"),
|
|
224
|
+
});
|
|
225
|
+
return 0;
|
|
226
|
+
}
|
|
227
|
+
async function approve(args) {
|
|
228
|
+
const marker = await linked_marker(args);
|
|
229
|
+
await approve_requests(cli_transport(marker.workspace_id), marker.vault_id, marker.vault_name);
|
|
230
|
+
return 0;
|
|
231
|
+
}
|
|
232
|
+
async function forget(args) {
|
|
233
|
+
const folder = await locate(args);
|
|
234
|
+
const marker = await read_marker(folder);
|
|
235
|
+
if (marker === null)
|
|
236
|
+
throw new CliError(`${folder} is not linked to a vault.`, "not_linked");
|
|
237
|
+
await forget_key(marker.vault_id, marker.vault_name);
|
|
238
|
+
return 0;
|
|
239
|
+
}
|
|
178
240
|
export async function vault(args) {
|
|
179
241
|
const action = args.positional[0] ?? "list";
|
|
180
242
|
if (action === "list")
|
|
@@ -185,5 +247,11 @@ export async function vault(args) {
|
|
|
185
247
|
return unlink(args);
|
|
186
248
|
if (action === "status" || action === "conflicts")
|
|
187
249
|
return status(args);
|
|
250
|
+
if (action === "unlock")
|
|
251
|
+
return unlock(args);
|
|
252
|
+
if (action === "approve")
|
|
253
|
+
return approve(args);
|
|
254
|
+
if (action === "forget")
|
|
255
|
+
return forget(args);
|
|
188
256
|
throw new CliError(USAGE, "invalid_arguments");
|
|
189
257
|
}
|
package/dist/device.js
CHANGED
|
@@ -28,16 +28,22 @@ export async function ensure_device(force = false) {
|
|
|
28
28
|
});
|
|
29
29
|
return device.id;
|
|
30
30
|
}
|
|
31
|
-
export async function
|
|
31
|
+
export async function device_state() {
|
|
32
32
|
const credentials = await read_credentials();
|
|
33
33
|
const settings = await read_settings();
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
const devices = await list_devices();
|
|
38
|
-
const mine = devices.find((device) => device.id === settings.device_id);
|
|
39
|
-
if (mine !== undefined && mine.revoked_at === null)
|
|
40
|
-
return mine.id;
|
|
34
|
+
const id = settings.device_id;
|
|
35
|
+
if (credentials === null || id === undefined || settings.device_api_url !== credentials.api_url) {
|
|
36
|
+
return { state: "unregistered" };
|
|
41
37
|
}
|
|
38
|
+
const devices = await list_devices();
|
|
39
|
+
const mine = devices.find((device) => device.id === id);
|
|
40
|
+
if (mine === undefined)
|
|
41
|
+
return { state: "unknown", id };
|
|
42
|
+
return { state: mine.revoked_at === null ? "active" : "revoked", id };
|
|
43
|
+
}
|
|
44
|
+
export async function verify_device() {
|
|
45
|
+
const current = await device_state();
|
|
46
|
+
if (current.state === "active")
|
|
47
|
+
return current.id;
|
|
42
48
|
return ensure_device(true);
|
|
43
49
|
}
|