nemu-cli 1.5.0 → 1.6.1
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 +46 -0
- package/README.md +38 -0
- package/dist/args.js +9 -1
- package/dist/commands/sync.js +197 -0
- package/dist/commands/vault.js +189 -0
- package/dist/device.js +1 -1
- package/dist/http.js +32 -20
- package/dist/index.js +13 -0
- package/dist/vault/api.js +204 -0
- package/dist/vault/connect.js +41 -0
- package/dist/vault/engine.js +634 -0
- package/dist/vault/files.js +187 -0
- package/dist/vault/ignore.js +100 -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/state.js +112 -0
- package/dist/vault/watch.js +101 -0
- package/dist/version.js +1 -1
- package/package.json +4 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,51 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 1.6.1
|
|
4
|
+
|
|
5
|
+
When `nemu sync` merges a note automatically, it now records the merge in the vault's merge log,
|
|
6
|
+
with this computer's text from before the merge, so the console can restore either side with one
|
|
7
|
+
click. A merge log that cannot be reached never fails the sync; the run reports it as a notice.
|
|
8
|
+
|
|
9
|
+
`-w <workspace>` works. It was read as a switch, so the workspace after it was dropped and the
|
|
10
|
+
command ran in your first workspace; `--workspace` was not affected. `-o` and `-d` had the same
|
|
11
|
+
problem and are fixed too.
|
|
12
|
+
|
|
13
|
+
`nemu vault link` on a folder that is no longer linked, such as a drive folder that was emptied and
|
|
14
|
+
linked again, now starts fresh and downloads the vault, instead of treating every missing file as
|
|
15
|
+
deleted and stopping at the mass-delete guard.
|
|
16
|
+
|
|
17
|
+
Sync never writes `.claude/`, `.codex/`, `.cursor/`, `.vscode/`, `.idea/`, `.husky/`,
|
|
18
|
+
`.github/workflows/` or `.git/hooks/`, in either direction, because files there can run code when
|
|
19
|
+
you open the folder in an editor or an agent. A folder inside the vault that is a symbolic link to
|
|
20
|
+
somewhere else is never written through.
|
|
21
|
+
|
|
22
|
+
## 1.6.0
|
|
23
|
+
|
|
24
|
+
`nemu sync` keeps a folder, such as an Obsidian vault, in step with a Context vault.
|
|
25
|
+
`nemu vault link <folder>` links it once (`--vault <name|id>` picks the vault, `--create` makes
|
|
26
|
+
one) and writes `.nemu/vault.json`, without which sync refuses to run, so an unmounted drive never
|
|
27
|
+
looks like a folder whose notes were deleted. `nemu sync --watch` keeps syncing until stopped.
|
|
28
|
+
`nemu vault list`, `status` and `unlink` round it off.
|
|
29
|
+
|
|
30
|
+
Each run reads the vault's changes since the last run, or lists every file when the device has been
|
|
31
|
+
away longer than the change history, and compares files by content, never by modification time.
|
|
32
|
+
Markdown changed on both sides is merged line by line when the edits do not overlap, treating CRLF
|
|
33
|
+
and LF as equal and keeping your file's line endings. Otherwise the vault's version keeps the name
|
|
34
|
+
and yours is saved beside it as `Note (conflict, <device>, <date> <time>).md`; no note ever gets
|
|
35
|
+
conflict markers. `--conflicts=file` always writes the conflict file instead. An edit wins over a
|
|
36
|
+
delete, a rename is sent as a move so the note keeps its history, and two renames onto one name
|
|
37
|
+
give the second ` (2)`.
|
|
38
|
+
|
|
39
|
+
A run that would delete more than 10 files, every file, or at least 3 files and more than 10% of
|
|
40
|
+
the vault stops and deletes nothing until you pass `--allow-deletes`. `.nemu/lock` keeps two syncs
|
|
41
|
+
off one folder and is taken over when its process is gone. Downloads are written to a temporary file
|
|
42
|
+
and renamed; uploads wait until a file has been unchanged for 2 seconds and reads the same twice.
|
|
43
|
+
Symbolic links, files above the plan's size, case clashes and names Windows cannot hold are skipped
|
|
44
|
+
and listed. Out of storage stops uploads and keeps downloading; a revoked device or key stops sync
|
|
45
|
+
and never deletes a local file. `.git/`, `node_modules/`, `.trash/`, `.DS_Store`, `._*` and
|
|
46
|
+
`.obsidian/workspace*.json` are never synced, the rest of `.obsidian/` is opt in per device, and
|
|
47
|
+
`.nemuignore` adds patterns.
|
|
48
|
+
|
|
3
49
|
## 1.5.0
|
|
4
50
|
|
|
5
51
|
`nemu login key` signs in with a workspace API key, the same `sk_` key that calls the gateway. It
|
package/README.md
CHANGED
|
@@ -181,6 +181,44 @@ and again on the server, and saved notes are always given to agents as data, nev
|
|
|
181
181
|
Each computer you sign in from is registered as a device. Revoke one in the console under Context to
|
|
182
182
|
stop it reading or writing; what it already saved stays.
|
|
183
183
|
|
|
184
|
+
## Syncing a folder with a vault
|
|
185
|
+
|
|
186
|
+
A vault is an Obsidian compatible folder of Markdown notes kept in your workspace's Context.
|
|
187
|
+
`nemu sync` keeps a folder on this computer in step with one.
|
|
188
|
+
|
|
189
|
+
```bash
|
|
190
|
+
nemu vault link ~/Notes --vault "Notes" # or --create for a new vault named after the folder
|
|
191
|
+
nemu sync ~/Notes # one run
|
|
192
|
+
nemu sync ~/Notes --watch # keep syncing until Ctrl C
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
| Command | Does |
|
|
196
|
+
| --- | --- |
|
|
197
|
+
| `nemu vault list` | Vaults in the workspace |
|
|
198
|
+
| `nemu vault link <folder> [--vault <name\|id>] [--create] [--obsidian]` | Link a folder; writes `<folder>/.nemu/vault.json` |
|
|
199
|
+
| `nemu sync [folder] [--watch] [--dry-run] [--conflicts=merge\|file] [--allow-deletes]` | Sync a linked folder (the current one, or the nearest parent that is linked) |
|
|
200
|
+
| `nemu vault status [folder]` | Last sync, device settings and open conflict copies |
|
|
201
|
+
| `nemu vault unlink [folder]` | Stop syncing; the files stay |
|
|
202
|
+
|
|
203
|
+
Files are compared by content, never by modification time. A note changed on both sides is merged
|
|
204
|
+
line by line when the edits do not overlap; otherwise the vault's version keeps the name and yours
|
|
205
|
+
is saved beside it as `Note (conflict, <device>, <date> <time>).md`. Conflict markers are never
|
|
206
|
+
written into a note. `--conflicts=file` always writes the conflict file instead of merging, and is
|
|
207
|
+
remembered for this device. An edit wins over a delete, and a rename keeps the note's history.
|
|
208
|
+
|
|
209
|
+
Sync refuses to run without `.nemu/vault.json`, so an unmounted drive never reads as "everything was
|
|
210
|
+
deleted". A run that would delete more than 10 files, every file, or at least 3 files and more than
|
|
211
|
+
10% of the vault stops and deletes nothing; pass `--allow-deletes` if you meant it. Symbolic links,
|
|
212
|
+
files above the plan's size, case clashes and names Windows cannot hold are skipped and listed.
|
|
213
|
+
`.git/`, `node_modules/`, `.trash/`, `.DS_Store`, `._*` and `.obsidian/workspace*.json` are never
|
|
214
|
+
synced, the rest of `.obsidian/` only with `--obsidian`, and a `.nemuignore` file adds patterns.
|
|
215
|
+
What each folder last agreed with its vault is kept in `~/.nemu/vaults/<vault id>.json`.
|
|
216
|
+
|
|
217
|
+
`tests/sim/simulate.ts` runs several fake devices with random edits, moves, deletes, crashes and
|
|
218
|
+
duplicated requests against a development platform and checks that they converge without losing a
|
|
219
|
+
byte. It does nothing unless `NEMU_SIM_API_URL`, `NEMU_SIM_API_KEY` and `NEMU_SIM_ROOT` are set, so
|
|
220
|
+
`bun test` stays offline.
|
|
221
|
+
|
|
184
222
|
## Error references
|
|
185
223
|
|
|
186
224
|
After you sign in, a command that fails sends a small report and prints a reference like
|
package/dist/args.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
const SHORT_WITH_VALUE = new Set(["w", "o", "d"]);
|
|
1
2
|
export function parse_args(argv) {
|
|
2
3
|
const positional = [];
|
|
3
4
|
const flags = {};
|
|
@@ -24,7 +25,14 @@ export function parse_args(argv) {
|
|
|
24
25
|
continue;
|
|
25
26
|
}
|
|
26
27
|
if (token.startsWith("-") && token.length > 1) {
|
|
27
|
-
|
|
28
|
+
const name = token.slice(1);
|
|
29
|
+
const next = argv[i + 1];
|
|
30
|
+
if (SHORT_WITH_VALUE.has(name) && next !== undefined && !next.startsWith("-")) {
|
|
31
|
+
flags[name] = next;
|
|
32
|
+
i += 1;
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
flags[name] = true;
|
|
28
36
|
continue;
|
|
29
37
|
}
|
|
30
38
|
positional.push(token);
|
|
@@ -0,0 +1,197 @@
|
|
|
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
|
+
return settings;
|
|
28
|
+
}
|
|
29
|
+
async function root_of(args) {
|
|
30
|
+
const raw = args.positional[0];
|
|
31
|
+
if (raw !== undefined) {
|
|
32
|
+
const folder = resolve(raw);
|
|
33
|
+
if ((await read_marker(folder)) === null) {
|
|
34
|
+
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");
|
|
35
|
+
}
|
|
36
|
+
return folder;
|
|
37
|
+
}
|
|
38
|
+
const found = await find_root(process.cwd());
|
|
39
|
+
if (found === null) {
|
|
40
|
+
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");
|
|
41
|
+
}
|
|
42
|
+
return found;
|
|
43
|
+
}
|
|
44
|
+
function print_event(event) {
|
|
45
|
+
switch (event.kind) {
|
|
46
|
+
case "pulled":
|
|
47
|
+
out(` ${cyan("↓")} ${event.path}`);
|
|
48
|
+
return;
|
|
49
|
+
case "pushed":
|
|
50
|
+
out(` ${green("↑")} ${event.path}`);
|
|
51
|
+
return;
|
|
52
|
+
case "restored":
|
|
53
|
+
out(` ${green("↑")} ${event.path} ${dim("(restored, edited here after it was deleted elsewhere)")}`);
|
|
54
|
+
return;
|
|
55
|
+
case "merged":
|
|
56
|
+
out(` ${green("⇄")} ${event.path} ${dim("(merged edits from both sides)")}`);
|
|
57
|
+
return;
|
|
58
|
+
case "deleted_local":
|
|
59
|
+
out(` ${red("−")} ${event.path} ${dim("(deleted elsewhere)")}`);
|
|
60
|
+
return;
|
|
61
|
+
case "deleted_remote":
|
|
62
|
+
out(` ${red("−")} ${event.path} ${dim("(deleted here, removed from the vault; history keeps it)")}`);
|
|
63
|
+
return;
|
|
64
|
+
case "moved":
|
|
65
|
+
out(` ${cyan("→")} ${event.from} ${dim("to")} ${event.to}`);
|
|
66
|
+
return;
|
|
67
|
+
case "conflict":
|
|
68
|
+
out(` ${red("!")} ${event.path} ${dim("conflict: your version is kept as")} ${event.copy}`);
|
|
69
|
+
return;
|
|
70
|
+
case "skipped":
|
|
71
|
+
out(dim(` · ${event.path}: ${event.reason}`));
|
|
72
|
+
return;
|
|
73
|
+
case "notice":
|
|
74
|
+
out(dim(` ${event.message}`));
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
function describe(action) {
|
|
79
|
+
switch (action.kind) {
|
|
80
|
+
case "pull":
|
|
81
|
+
return `download ${action.path}`;
|
|
82
|
+
case "push":
|
|
83
|
+
return `upload ${action.local.path}${action.reason === "new" ? " (new)" : ""}`;
|
|
84
|
+
case "merge":
|
|
85
|
+
return `merge ${action.remote.path}`;
|
|
86
|
+
case "keep_both":
|
|
87
|
+
return `keep both versions of ${action.remote.path}; yours becomes ${action.copy_path}`;
|
|
88
|
+
case "delete_local":
|
|
89
|
+
return `delete ${action.disk} here (deleted elsewhere)`;
|
|
90
|
+
case "delete_remote":
|
|
91
|
+
return action.counted ? `delete ${action.path} from the vault` : null;
|
|
92
|
+
case "move_remote":
|
|
93
|
+
return `move ${action.from} to ${action.to} in the vault`;
|
|
94
|
+
case "rename_local":
|
|
95
|
+
return `move ${action.from} to ${action.to} here`;
|
|
96
|
+
case "adopt":
|
|
97
|
+
case "forget":
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
function plural(count, word) {
|
|
102
|
+
return `${count} ${word}${count === 1 ? "" : "s"}`;
|
|
103
|
+
}
|
|
104
|
+
function summary(report) {
|
|
105
|
+
const parts = [
|
|
106
|
+
report.pulled.length > 0 ? `${report.pulled.length} down` : "",
|
|
107
|
+
report.pushed.length > 0 ? `${report.pushed.length} up` : "",
|
|
108
|
+
report.merged.length > 0 ? `${report.merged.length} merged` : "",
|
|
109
|
+
report.moved.length > 0 ? `${report.moved.length} moved` : "",
|
|
110
|
+
report.deleted_local.length + report.deleted_remote.length > 0
|
|
111
|
+
? `${report.deleted_local.length + report.deleted_remote.length} deleted`
|
|
112
|
+
: "",
|
|
113
|
+
report.conflicts.length > 0 ? plural(report.conflicts.length, "conflict") : "",
|
|
114
|
+
].filter((part) => part.length > 0);
|
|
115
|
+
return parts.length === 0 ? "Already in sync" : parts.join(", ");
|
|
116
|
+
}
|
|
117
|
+
function print_report(name, report) {
|
|
118
|
+
if (report.dry_run) {
|
|
119
|
+
const lines = report.planned.map(describe).filter((line) => line !== null);
|
|
120
|
+
out(` ${bold(name)} ${dim("dry run, nothing was changed")}`);
|
|
121
|
+
if (lines.length === 0)
|
|
122
|
+
out(dim(" Already in sync."));
|
|
123
|
+
for (const line of lines)
|
|
124
|
+
out(` ${line}`);
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
if (report.waiting.length > 0) {
|
|
128
|
+
out(dim(` Still being written, synced on the next run: ${report.waiting.join(", ")}`));
|
|
129
|
+
}
|
|
130
|
+
if (report.blocked !== null)
|
|
131
|
+
fail(`Uploads stopped: ${report.blocked.message} Downloads continued.`);
|
|
132
|
+
out(`${green("✓")} ${bold(name)} ${summary(report)}`);
|
|
133
|
+
}
|
|
134
|
+
function json_report(report) {
|
|
135
|
+
const { planned, ...rest } = report;
|
|
136
|
+
return {
|
|
137
|
+
ok: true,
|
|
138
|
+
...rest,
|
|
139
|
+
...(report.dry_run ? { planned: planned.map(describe).filter((line) => line !== null) } : {}),
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
export async function sync(args) {
|
|
143
|
+
const root = await root_of(args);
|
|
144
|
+
const marker = (await read_marker(root));
|
|
145
|
+
const linked = await open_linked(marker);
|
|
146
|
+
const settings = settings_from(args);
|
|
147
|
+
const json = is_json_mode();
|
|
148
|
+
const once = (lock) => run_sync({
|
|
149
|
+
root,
|
|
150
|
+
api: linked.api,
|
|
151
|
+
state_path: linked.state_path,
|
|
152
|
+
device: device_name(),
|
|
153
|
+
dry_run: bool_flag(args, "dry-run"),
|
|
154
|
+
allow_deletes: bool_flag(args, "allow-deletes"),
|
|
155
|
+
settings,
|
|
156
|
+
lock,
|
|
157
|
+
on_event: json ? undefined : print_event,
|
|
158
|
+
});
|
|
159
|
+
if (!bool_flag(args, "watch") || bool_flag(args, "dry-run")) {
|
|
160
|
+
const report = await once(true);
|
|
161
|
+
if (json)
|
|
162
|
+
emit(json_report(report));
|
|
163
|
+
else
|
|
164
|
+
print_report(marker.vault_name, report);
|
|
165
|
+
return report.blocked === null ? 0 : 2;
|
|
166
|
+
}
|
|
167
|
+
if (!json)
|
|
168
|
+
out(dim(` Watching ${root}. Press Ctrl+C to stop.`));
|
|
169
|
+
let exit = 0;
|
|
170
|
+
await watch_folder({
|
|
171
|
+
root,
|
|
172
|
+
run: async () => {
|
|
173
|
+
const report = await once(false);
|
|
174
|
+
const quiet = report.pulled.length + report.pushed.length + report.merged.length + report.moved.length +
|
|
175
|
+
report.deleted_local.length + report.deleted_remote.length + report.conflicts.length ===
|
|
176
|
+
0;
|
|
177
|
+
if (json)
|
|
178
|
+
emit(json_report(report));
|
|
179
|
+
else if (!quiet)
|
|
180
|
+
print_report(marker.vault_name, report);
|
|
181
|
+
},
|
|
182
|
+
on_error: (error) => {
|
|
183
|
+
const code = error instanceof CliError ? error.code : "unexpected";
|
|
184
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
185
|
+
if (json)
|
|
186
|
+
emit({ ok: false, error: message, code });
|
|
187
|
+
else
|
|
188
|
+
fail(message);
|
|
189
|
+
if (code === "sync_stopped" || code === "not_linked" || code === "server_mismatch") {
|
|
190
|
+
exit = 1;
|
|
191
|
+
return "stop";
|
|
192
|
+
}
|
|
193
|
+
return "continue";
|
|
194
|
+
},
|
|
195
|
+
});
|
|
196
|
+
return exit;
|
|
197
|
+
}
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { mkdir, readdir } from "node:fs/promises";
|
|
2
|
+
import { basename, dirname, resolve } from "node:path";
|
|
3
|
+
import { bool_flag, flag } from "../args.js";
|
|
4
|
+
import { CliError } from "../errors.js";
|
|
5
|
+
import { emit, is_json_mode, table } from "../output.js";
|
|
6
|
+
import { bold, dim, out, success } from "../ui.js";
|
|
7
|
+
import { create_vault, list_vaults, VaultApi } from "../vault/api.js";
|
|
8
|
+
import { cli_transport, client_label, signed_in } from "../vault/connect.js";
|
|
9
|
+
import { is_conflict_copy } from "../vault/paths.js";
|
|
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
|
+
import { is_default_ignored } from "../vault/ignore.js";
|
|
12
|
+
const USAGE = "Use: nemu vault list | link <folder> [--vault <name|id>] [--create] [--obsidian] | unlink [folder] | status [folder]";
|
|
13
|
+
function size_of(bytes) {
|
|
14
|
+
if (bytes === undefined)
|
|
15
|
+
return "";
|
|
16
|
+
if (bytes < 1024 * 1024)
|
|
17
|
+
return `${Math.ceil(bytes / 1024)} KB`;
|
|
18
|
+
return `${(bytes / 1024 / 1024).toFixed(1)} MB`;
|
|
19
|
+
}
|
|
20
|
+
async function list(args) {
|
|
21
|
+
await signed_in();
|
|
22
|
+
const vaults = await list_vaults(cli_transport(flag(args, "workspace", "w")));
|
|
23
|
+
table(vaults.map((vault) => ({
|
|
24
|
+
id: vault.id,
|
|
25
|
+
name: vault.name,
|
|
26
|
+
notes: vault.notes ?? "",
|
|
27
|
+
size: size_of(vault.stored_bytes),
|
|
28
|
+
})), ["id", "name", "notes", "size"], "No vaults yet. Create one with nemu vault link <folder> --create.");
|
|
29
|
+
return 0;
|
|
30
|
+
}
|
|
31
|
+
function pick_vault(vaults, wanted) {
|
|
32
|
+
if (wanted === undefined)
|
|
33
|
+
return vaults.length === 1 ? vaults[0] : null;
|
|
34
|
+
return (vaults.find((vault) => vault.id === wanted) ??
|
|
35
|
+
vaults.find((vault) => vault.name.toLowerCase() === wanted.toLowerCase()) ??
|
|
36
|
+
null);
|
|
37
|
+
}
|
|
38
|
+
async function link(args) {
|
|
39
|
+
const raw = args.positional[1];
|
|
40
|
+
if (raw === undefined)
|
|
41
|
+
throw new CliError(USAGE, "invalid_arguments");
|
|
42
|
+
const folder = resolve(raw);
|
|
43
|
+
const credentials = await signed_in();
|
|
44
|
+
const workspace = flag(args, "workspace", "w");
|
|
45
|
+
const wanted = flag(args, "vault");
|
|
46
|
+
const create = bool_flag(args, "create");
|
|
47
|
+
const enclosing = await find_root(dirname(folder));
|
|
48
|
+
if (enclosing !== null && enclosing !== folder) {
|
|
49
|
+
throw new CliError(`${folder} is inside ${enclosing}, which is already linked to a vault.`, "already_linked");
|
|
50
|
+
}
|
|
51
|
+
await mkdir(folder, { recursive: true });
|
|
52
|
+
const existing = await read_marker(folder);
|
|
53
|
+
const transport = cli_transport(workspace);
|
|
54
|
+
const vaults = await list_vaults(transport);
|
|
55
|
+
let vault = pick_vault(vaults, wanted);
|
|
56
|
+
if (vault === null && create) {
|
|
57
|
+
vault = await create_vault(transport, wanted ?? basename(folder));
|
|
58
|
+
}
|
|
59
|
+
if (vault === null) {
|
|
60
|
+
const names = vaults.map((entry) => `"${entry.name}"`).join(", ");
|
|
61
|
+
throw new CliError(vaults.length === 0
|
|
62
|
+
? "This workspace has no vaults yet. Add --create to make one named after the folder."
|
|
63
|
+
: wanted === undefined
|
|
64
|
+
? `Choose a vault with --vault <name|id>: ${names}. Or add --create for a new one.`
|
|
65
|
+
: `No vault is called "${wanted}". Have: ${names}. Add --create to make it.`, "vault_not_found");
|
|
66
|
+
}
|
|
67
|
+
if (existing !== null && existing.vault_id !== vault.id) {
|
|
68
|
+
throw new CliError(`${folder} is already linked to the vault "${existing.vault_name}". Run nemu vault unlink ${folder} first.`, "already_linked");
|
|
69
|
+
}
|
|
70
|
+
const state_path = state_path_for(vault.id);
|
|
71
|
+
const previous = await read_state(state_path);
|
|
72
|
+
if (previous !== null && previous.root !== folder && (await read_marker(previous.root))?.vault_id === vault.id) {
|
|
73
|
+
throw new CliError(`The vault "${vault.name}" is already synced into ${previous.root} on this computer. Run nemu vault unlink ${previous.root} first.`, "already_linked");
|
|
74
|
+
}
|
|
75
|
+
const info = await new VaultApi(transport, vault.id, client_label()).info();
|
|
76
|
+
const workspace_id = info.workspace_id ?? credentials.workspace?.id ?? workspace ?? null;
|
|
77
|
+
await write_marker(folder, {
|
|
78
|
+
version: 1,
|
|
79
|
+
vault_id: vault.id,
|
|
80
|
+
vault_name: vault.name,
|
|
81
|
+
workspace_id,
|
|
82
|
+
server: credentials.api_url,
|
|
83
|
+
linked_at: new Date().toISOString(),
|
|
84
|
+
});
|
|
85
|
+
if (previous === null || previous.root !== folder || existing === null) {
|
|
86
|
+
await write_state(state_path, empty_state(vault.id, folder, {
|
|
87
|
+
conflicts: previous?.settings.conflicts,
|
|
88
|
+
obsidian: bool_flag(args, "obsidian") || (previous?.settings.obsidian ?? false),
|
|
89
|
+
}));
|
|
90
|
+
}
|
|
91
|
+
else if (bool_flag(args, "obsidian")) {
|
|
92
|
+
await write_state(state_path, { ...previous, settings: { ...previous.settings, obsidian: true }, full_next: true });
|
|
93
|
+
}
|
|
94
|
+
if (is_json_mode()) {
|
|
95
|
+
emit({ ok: true, folder, vault: { id: vault.id, name: vault.name }, workspace_id, marker: marker_path(folder) });
|
|
96
|
+
return 0;
|
|
97
|
+
}
|
|
98
|
+
success(`Linked ${folder} to the vault ${bold(vault.name)}`);
|
|
99
|
+
out(dim(` Run nemu sync ${raw} to sync it, or nemu sync ${raw} --watch to keep it in sync.`));
|
|
100
|
+
return 0;
|
|
101
|
+
}
|
|
102
|
+
async function locate(args) {
|
|
103
|
+
const raw = args.positional[1];
|
|
104
|
+
if (raw !== undefined)
|
|
105
|
+
return resolve(raw);
|
|
106
|
+
const found = await find_root(process.cwd());
|
|
107
|
+
if (found === null)
|
|
108
|
+
throw new CliError("This folder is not linked to a vault.", "not_linked");
|
|
109
|
+
return found;
|
|
110
|
+
}
|
|
111
|
+
async function unlink(args) {
|
|
112
|
+
const folder = await locate(args);
|
|
113
|
+
const marker = await read_marker(folder);
|
|
114
|
+
if (marker === null)
|
|
115
|
+
throw new CliError(`${folder} is not linked to a vault.`, "not_linked");
|
|
116
|
+
const state_path = state_path_for(marker.vault_id);
|
|
117
|
+
const state = await read_state(state_path);
|
|
118
|
+
if (state === null || state.root === folder)
|
|
119
|
+
await remove_state(state_path);
|
|
120
|
+
await remove_marker(folder);
|
|
121
|
+
if (is_json_mode()) {
|
|
122
|
+
emit({ ok: true, folder, vault_id: marker.vault_id });
|
|
123
|
+
return 0;
|
|
124
|
+
}
|
|
125
|
+
success(`Unlinked ${folder} from "${marker.vault_name}". Your files are still there.`);
|
|
126
|
+
return 0;
|
|
127
|
+
}
|
|
128
|
+
async function conflict_copies(root, relative = "") {
|
|
129
|
+
const found = [];
|
|
130
|
+
const entries = await readdir(relative === "" ? root : `${root}/${relative}`, { withFileTypes: true }).catch(() => []);
|
|
131
|
+
for (const entry of entries) {
|
|
132
|
+
const path = relative === "" ? entry.name : `${relative}/${entry.name}`;
|
|
133
|
+
if (is_default_ignored(path, false))
|
|
134
|
+
continue;
|
|
135
|
+
if (entry.isDirectory())
|
|
136
|
+
found.push(...(await conflict_copies(root, path)));
|
|
137
|
+
else if (entry.isFile() && is_conflict_copy(path.normalize("NFC")))
|
|
138
|
+
found.push(path);
|
|
139
|
+
}
|
|
140
|
+
return found;
|
|
141
|
+
}
|
|
142
|
+
async function status(args) {
|
|
143
|
+
const folder = await locate(args);
|
|
144
|
+
const marker = await read_marker(folder);
|
|
145
|
+
if (marker === null)
|
|
146
|
+
throw new CliError(`${folder} is not linked to a vault.`, "not_linked");
|
|
147
|
+
const state = await read_state(state_path_for(marker.vault_id));
|
|
148
|
+
const conflicts = await conflict_copies(folder);
|
|
149
|
+
const summary = {
|
|
150
|
+
folder,
|
|
151
|
+
vault: { id: marker.vault_id, name: marker.vault_name },
|
|
152
|
+
server: marker.server,
|
|
153
|
+
workspace_id: marker.workspace_id,
|
|
154
|
+
tracked: state === null ? 0 : Object.keys(state.files).length,
|
|
155
|
+
last_sync: state?.synced_at ?? null,
|
|
156
|
+
conflicts_mode: state?.settings.conflicts ?? "merge",
|
|
157
|
+
obsidian_settings: state?.settings.obsidian ?? false,
|
|
158
|
+
conflicts,
|
|
159
|
+
};
|
|
160
|
+
if (is_json_mode()) {
|
|
161
|
+
emit(summary);
|
|
162
|
+
return 0;
|
|
163
|
+
}
|
|
164
|
+
out(` ${bold(marker.vault_name)} ${dim(marker.vault_id)}`);
|
|
165
|
+
out(` ${dim("folder ")} ${folder}`);
|
|
166
|
+
out(` ${dim("files ")} ${summary.tracked}`);
|
|
167
|
+
out(` ${dim("last sync")} ${summary.last_sync ?? "never"}`);
|
|
168
|
+
out(` ${dim("conflicts")} ${summary.conflicts_mode === "merge" ? "merge when possible" : "always write a conflict file"}`);
|
|
169
|
+
out(` ${dim(".obsidian")} ${summary.obsidian_settings ? "synced" : "not synced on this device"}`);
|
|
170
|
+
if (conflicts.length > 0) {
|
|
171
|
+
out();
|
|
172
|
+
out(` ${bold(`${conflicts.length} open conflict${conflicts.length === 1 ? "" : "s"}`)} ${dim("(delete the copy you do not want)")}`);
|
|
173
|
+
for (const path of conflicts)
|
|
174
|
+
out(` ${path}`);
|
|
175
|
+
}
|
|
176
|
+
return 0;
|
|
177
|
+
}
|
|
178
|
+
export async function vault(args) {
|
|
179
|
+
const action = args.positional[0] ?? "list";
|
|
180
|
+
if (action === "list")
|
|
181
|
+
return list(args);
|
|
182
|
+
if (action === "link")
|
|
183
|
+
return link(args);
|
|
184
|
+
if (action === "unlink")
|
|
185
|
+
return unlink(args);
|
|
186
|
+
if (action === "status" || action === "conflicts")
|
|
187
|
+
return status(args);
|
|
188
|
+
throw new CliError(USAGE, "invalid_arguments");
|
|
189
|
+
}
|
package/dist/device.js
CHANGED
|
@@ -3,7 +3,7 @@ import { read_credentials, read_settings, update_settings } from "./config.js";
|
|
|
3
3
|
import { list_devices, register_device } from "./context/api.js";
|
|
4
4
|
import { VERSION } from "./version.js";
|
|
5
5
|
const PLATFORMS = new Set(["darwin", "linux", "win32", "freebsd"]);
|
|
6
|
-
function device_name() {
|
|
6
|
+
export function device_name() {
|
|
7
7
|
const raw = hostname().replace(/\.local$/, "").replace(/[^\w .-]/g, "").trim();
|
|
8
8
|
return (raw.length > 0 ? raw : "This computer").slice(0, 64);
|
|
9
9
|
}
|
package/dist/http.js
CHANGED
|
@@ -8,7 +8,7 @@ const error_body = z.object({
|
|
|
8
8
|
code: z.string().optional(),
|
|
9
9
|
reference: z.string().optional(),
|
|
10
10
|
});
|
|
11
|
-
export async function
|
|
11
|
+
export async function send(method, path, options = {}) {
|
|
12
12
|
const credentials = await fresh_credentials();
|
|
13
13
|
const settings = await read_settings();
|
|
14
14
|
const url = new URL(`${credentials.api_url}${path}`);
|
|
@@ -20,9 +20,8 @@ export async function api(method, path, schema, options = {}) {
|
|
|
20
20
|
authorization: `Bearer ${credentials.access_token}`,
|
|
21
21
|
"nemu-api-version": API_VERSION,
|
|
22
22
|
"user-agent": `nemu-cli/${VERSION}`,
|
|
23
|
+
...(options.headers ?? {}),
|
|
23
24
|
};
|
|
24
|
-
if (options.body !== undefined)
|
|
25
|
-
headers["content-type"] = "application/json";
|
|
26
25
|
const is_key = credentials.kind === "key";
|
|
27
26
|
if (!is_key &&
|
|
28
27
|
options.device !== false &&
|
|
@@ -30,34 +29,47 @@ export async function api(method, path, schema, options = {}) {
|
|
|
30
29
|
settings.device_api_url === credentials.api_url) {
|
|
31
30
|
headers["x-nemu-device"] = settings.device_id;
|
|
32
31
|
}
|
|
33
|
-
|
|
34
|
-
|
|
32
|
+
const workspace = options.workspace ?? settings.context_workspace;
|
|
33
|
+
if (!is_key && workspace !== undefined) {
|
|
34
|
+
headers["x-nemu-workspace"] = workspace;
|
|
35
35
|
}
|
|
36
|
-
let response;
|
|
37
36
|
try {
|
|
38
|
-
response = await fetch(url, {
|
|
37
|
+
const response = await fetch(url, {
|
|
39
38
|
method,
|
|
40
39
|
headers,
|
|
41
|
-
body: options.body
|
|
40
|
+
body: options.body,
|
|
42
41
|
signal: AbortSignal.timeout(options.timeout_ms ?? 20_000),
|
|
43
42
|
});
|
|
43
|
+
return { api_url: credentials.api_url, response };
|
|
44
44
|
}
|
|
45
45
|
catch {
|
|
46
46
|
throw new CliError(`Could not reach ${credentials.api_url}.`, "network_failed");
|
|
47
47
|
}
|
|
48
|
+
}
|
|
49
|
+
export async function failure_of(response) {
|
|
50
|
+
const payload = await response.json().catch(() => null);
|
|
51
|
+
const failure = error_body.safeParse(payload);
|
|
52
|
+
const message = failure.success && failure.data.error !== undefined
|
|
53
|
+
? failure.data.error
|
|
54
|
+
: `Request failed with status ${response.status}`;
|
|
55
|
+
return new CliError(message, failure.success && failure.data.code !== undefined ? failure.data.code : `http_${response.status}`, {
|
|
56
|
+
status: response.status,
|
|
57
|
+
reference: (failure.success ? failure.data.reference : undefined) ??
|
|
58
|
+
response.headers.get("x-nemu-reference") ??
|
|
59
|
+
undefined,
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
export async function api(method, path, schema, options = {}) {
|
|
63
|
+
const { response } = await send(method, path, {
|
|
64
|
+
query: options.query,
|
|
65
|
+
headers: options.body !== undefined ? { "content-type": "application/json" } : {},
|
|
66
|
+
body: options.body === undefined ? undefined : JSON.stringify(options.body),
|
|
67
|
+
device: options.device,
|
|
68
|
+
timeout_ms: options.timeout_ms,
|
|
69
|
+
});
|
|
70
|
+
if (!response.ok)
|
|
71
|
+
throw await failure_of(response);
|
|
48
72
|
const payload = await response.json().catch(() => null);
|
|
49
|
-
if (!response.ok) {
|
|
50
|
-
const failure = error_body.safeParse(payload);
|
|
51
|
-
const message = failure.success && failure.data.error !== undefined
|
|
52
|
-
? failure.data.error
|
|
53
|
-
: `Request failed with status ${response.status}`;
|
|
54
|
-
throw new CliError(message, failure.success && failure.data.code !== undefined ? failure.data.code : `http_${response.status}`, {
|
|
55
|
-
status: response.status,
|
|
56
|
-
reference: (failure.success ? failure.data.reference : undefined) ??
|
|
57
|
-
response.headers.get("x-nemu-reference") ??
|
|
58
|
-
undefined,
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
73
|
const parsed = schema.safeParse(payload);
|
|
62
74
|
if (!parsed.success) {
|
|
63
75
|
throw new CliError("Nemu sent an answer this CLI does not understand. Update nemu-cli.", "invalid_response");
|
package/dist/index.js
CHANGED
|
@@ -20,6 +20,8 @@ import { send_report } from "./report.js";
|
|
|
20
20
|
import { maybe_offer_update, update_command } from "./update.js";
|
|
21
21
|
import { migrate_after_update } from "./migrations.js";
|
|
22
22
|
import { repair } from "./commands/repair.js";
|
|
23
|
+
import { vault } from "./commands/vault.js";
|
|
24
|
+
import { sync } from "./commands/sync.js";
|
|
23
25
|
import { CliError } from "./errors.js";
|
|
24
26
|
import { VERSION } from "./version.js";
|
|
25
27
|
import { bold, dim, fail, out } from "./ui.js";
|
|
@@ -57,6 +59,8 @@ const COMMANDS = {
|
|
|
57
59
|
config: config_command,
|
|
58
60
|
update: update_command,
|
|
59
61
|
repair,
|
|
62
|
+
vault,
|
|
63
|
+
sync,
|
|
60
64
|
};
|
|
61
65
|
const HELP = `
|
|
62
66
|
${bold("nemu")} ${dim(VERSION)}
|
|
@@ -85,6 +89,15 @@ const HELP = `
|
|
|
85
89
|
update [--yes] Check for a new version and install it
|
|
86
90
|
repair Bring hooks and editor entries up to date
|
|
87
91
|
|
|
92
|
+
${bold("Vaults")}
|
|
93
|
+
vault list Vaults in this workspace
|
|
94
|
+
vault link <folder> [--vault <name|id>] [--create] [--obsidian]
|
|
95
|
+
Link a folder to a vault (writes <folder>/.nemu/vault.json)
|
|
96
|
+
vault unlink [folder] Stop syncing a folder; its files stay
|
|
97
|
+
vault status [folder] Last sync, settings and open conflicts
|
|
98
|
+
sync [folder] [--watch] [--dry-run] [--conflicts=merge|file] [--allow-deletes]
|
|
99
|
+
Sync a linked folder with its vault
|
|
100
|
+
|
|
88
101
|
${bold("Account")}
|
|
89
102
|
login [--force] [--no-browser] Sign in with the device flow
|
|
90
103
|
login key Sign in with a workspace API key (asked for, or NEMU_API_KEY)
|