@clarionstack/notesia 0.0.5 → 0.0.6
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/package.json +1 -1
- package/src/import.mjs +112 -15
- package/src/io.mjs +3 -1
package/package.json
CHANGED
package/src/import.mjs
CHANGED
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
import { createReadStream } from 'node:fs';
|
|
10
10
|
import fs from 'node:fs/promises';
|
|
11
11
|
import path from 'node:path';
|
|
12
|
+
import readline from 'node:readline';
|
|
12
13
|
import { requestBridge } from './client.mjs';
|
|
13
14
|
|
|
14
15
|
const NOTE_CHAR_LIMIT = 100_000;
|
|
@@ -16,11 +17,17 @@ const NOTE_CHAR_LIMIT = 100_000;
|
|
|
16
17
|
const USAGE = [
|
|
17
18
|
'Usage: notesia import <path> [options]',
|
|
18
19
|
'',
|
|
20
|
+
'Arguments:',
|
|
21
|
+
' <path> Path to directory to import (positional or --path flag)',
|
|
22
|
+
'',
|
|
19
23
|
'Options:',
|
|
24
|
+
' --path <dir> Path to directory to import',
|
|
20
25
|
' --ext <list> Comma-separated extensions to import (default: md,txt)',
|
|
21
26
|
' --folder <name> Target folder name in Notesia',
|
|
22
27
|
' --tags <list> Comma-separated tags to apply to all imported notes',
|
|
23
28
|
' --conflict <mode> Conflict resolution: skip | overwrite (default: skip)',
|
|
29
|
+
' --create-folder Create folder if it does not exist (default: yes)',
|
|
30
|
+
' --no-create-folder Do not create folder if it does not exist',
|
|
24
31
|
' --dry-run Preview without writing',
|
|
25
32
|
' --format <fmt> Output format: text | json (default: text)',
|
|
26
33
|
' --quiet Suppress stdout output',
|
|
@@ -38,34 +45,37 @@ export function parseImportArgs(argv) {
|
|
|
38
45
|
process.exit(0);
|
|
39
46
|
}
|
|
40
47
|
|
|
41
|
-
const importPath = args.shift();
|
|
42
|
-
if (!importPath || importPath.startsWith('--')) {
|
|
43
|
-
process.stderr.write('Error: <path> argument is required\n\n' + USAGE);
|
|
44
|
-
process.exit(2);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
48
|
const opts = {
|
|
48
|
-
importPath:
|
|
49
|
+
importPath: null,
|
|
49
50
|
ext: ['md', 'txt'],
|
|
50
51
|
folder: null,
|
|
51
52
|
tags: [],
|
|
52
53
|
conflict: 'skip',
|
|
54
|
+
createFolder: true, // Auto-create folder by default (prompt if not exists)
|
|
53
55
|
dryRun: false,
|
|
54
56
|
format: 'text',
|
|
55
57
|
quiet: false,
|
|
56
58
|
verbose: false,
|
|
57
59
|
};
|
|
58
60
|
|
|
61
|
+
// Handle positional <path> argument OR --path flag
|
|
62
|
+
const firstArg = args[0];
|
|
63
|
+
if (firstArg && !firstArg.startsWith('--')) {
|
|
64
|
+
// Positional path: notesia import ~/Downloads
|
|
65
|
+
opts.importPath = path.resolve(firstArg);
|
|
66
|
+
args.shift();
|
|
67
|
+
}
|
|
68
|
+
|
|
59
69
|
for (let i = 0; i < args.length; i++) {
|
|
60
70
|
const token = args[i];
|
|
61
71
|
|
|
62
|
-
if (token === '--
|
|
72
|
+
if (token === '--path') {
|
|
63
73
|
const val = args[++i];
|
|
64
|
-
if (!val || val.startsWith('--')) { process.stderr.write('Error: --
|
|
65
|
-
opts.
|
|
66
|
-
} else if (token === '--
|
|
74
|
+
if (!val || val.startsWith('--')) { process.stderr.write('Error: --path requires a value\n\n' + USAGE); process.exit(2); }
|
|
75
|
+
opts.importPath = path.resolve(val);
|
|
76
|
+
} else if (token === '--ext') {
|
|
67
77
|
const val = args[++i];
|
|
68
|
-
if (!val || val.startsWith('--')) { process.stderr.write('Error: --
|
|
78
|
+
if (!val || val.startsWith('--')) { process.stderr.write('Error: --ext requires a value\n\n' + USAGE); process.exit(2); }
|
|
69
79
|
opts.folder = val;
|
|
70
80
|
} else if (token === '--tags') {
|
|
71
81
|
const val = args[++i];
|
|
@@ -75,6 +85,10 @@ export function parseImportArgs(argv) {
|
|
|
75
85
|
const val = args[++i];
|
|
76
86
|
if (val !== 'skip' && val !== 'overwrite') { process.stderr.write('Error: --conflict must be "skip" or "overwrite"\n\n' + USAGE); process.exit(2); }
|
|
77
87
|
opts.conflict = val;
|
|
88
|
+
} else if (token === '--create-folder') {
|
|
89
|
+
opts.createFolder = true;
|
|
90
|
+
} else if (token === '--no-create-folder') {
|
|
91
|
+
opts.createFolder = false;
|
|
78
92
|
} else if (token === '--dry-run') {
|
|
79
93
|
opts.dryRun = true;
|
|
80
94
|
} else if (token === '--format') {
|
|
@@ -91,6 +105,12 @@ export function parseImportArgs(argv) {
|
|
|
91
105
|
}
|
|
92
106
|
}
|
|
93
107
|
|
|
108
|
+
// Validate that a path was provided
|
|
109
|
+
if (!opts.importPath) {
|
|
110
|
+
process.stderr.write('Error: <path> argument is required (use positional argument or --path flag)\n\n' + USAGE);
|
|
111
|
+
process.exit(2);
|
|
112
|
+
}
|
|
113
|
+
|
|
94
114
|
return opts;
|
|
95
115
|
}
|
|
96
116
|
|
|
@@ -210,11 +230,60 @@ function printResult(result, opts) {
|
|
|
210
230
|
const { format = 'text', quiet = false } = opts;
|
|
211
231
|
if (quiet) return;
|
|
212
232
|
if (format === 'json') { process.stdout.write(JSON.stringify(result) + '\n'); return; }
|
|
213
|
-
|
|
233
|
+
|
|
234
|
+
const { created, skipped, overwritten, failed, elapsedMs, items = [] } = result;
|
|
214
235
|
const parts = [`${created} created`];
|
|
215
236
|
if (overwritten > 0) parts.push(`${overwritten} overwritten`);
|
|
216
237
|
parts.push(`${skipped} skipped`, `${failed} failed`);
|
|
217
238
|
process.stdout.write(`Import complete: ${parts.join(', ')} (${formatElapsed(elapsedMs)})\n`);
|
|
239
|
+
|
|
240
|
+
// Print summary table if there are items
|
|
241
|
+
if (items.length > 0) {
|
|
242
|
+
process.stdout.write('\n');
|
|
243
|
+
process.stdout.write('─'.repeat(80) + '\n');
|
|
244
|
+
process.stdout.write(`${'Status'.padEnd(12)} | ${'Source'.padEnd(50)} | Reason\n`);
|
|
245
|
+
process.stdout.write('─'.repeat(80) + '\n');
|
|
246
|
+
|
|
247
|
+
for (const item of items) {
|
|
248
|
+
const status = (item.status || 'unknown').padEnd(12);
|
|
249
|
+
const source = (item.source_path || '').slice(0, 48).padEnd(50);
|
|
250
|
+
let reason = '';
|
|
251
|
+
|
|
252
|
+
if (item.status === 'created') {
|
|
253
|
+
reason = `noteId: ${item.noteId}`;
|
|
254
|
+
} else if (item.status === 'skipped') {
|
|
255
|
+
reason = `noteId: ${item.noteId}`;
|
|
256
|
+
} else if (item.status === 'overwritten') {
|
|
257
|
+
reason = `noteId: ${item.noteId}`;
|
|
258
|
+
} else if (item.status === 'failed') {
|
|
259
|
+
reason = item.error || 'Unknown error';
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
process.stdout.write(`${status} | ${source} | ${reason}\n`);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
process.stdout.write('─'.repeat(80) + '\n');
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// ─── Interactive prompt ──────────────────────────────────────────────────────
|
|
270
|
+
|
|
271
|
+
function createInterface() {
|
|
272
|
+
return readline.createInterface({
|
|
273
|
+
input: process.stdin,
|
|
274
|
+
output: process.stdout,
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
async function promptYesNo(question) {
|
|
279
|
+
return new Promise((resolve) => {
|
|
280
|
+
const rl = createInterface();
|
|
281
|
+
rl.question(question, (answer) => {
|
|
282
|
+
rl.close();
|
|
283
|
+
const normalized = answer.trim().toLowerCase();
|
|
284
|
+
resolve(normalized === 'y' || normalized === 'yes');
|
|
285
|
+
});
|
|
286
|
+
});
|
|
218
287
|
}
|
|
219
288
|
|
|
220
289
|
// ─── Main entry point ────────────────────────────────────────────────────────
|
|
@@ -271,6 +340,8 @@ export async function runImport(argv, deps = {}) {
|
|
|
271
340
|
folder: opts.folder,
|
|
272
341
|
tags: opts.tags,
|
|
273
342
|
conflict: opts.conflict,
|
|
343
|
+
// Always check first - we'll prompt if needed
|
|
344
|
+
create_folder_if_missing: false,
|
|
274
345
|
dry_run: opts.dryRun,
|
|
275
346
|
};
|
|
276
347
|
|
|
@@ -293,8 +364,34 @@ export async function runImport(argv, deps = {}) {
|
|
|
293
364
|
if (!response || response.ok === false) {
|
|
294
365
|
const code = response?.error?.code || 'BRIDGE_ERROR';
|
|
295
366
|
const message = response?.error?.message || 'Bridge request failed';
|
|
296
|
-
|
|
297
|
-
|
|
367
|
+
|
|
368
|
+
// Handle folder not found - prompt user to create (only if --create-folder is set)
|
|
369
|
+
if (code === 'FOLDER_NOT_FOUND' && opts.folder && opts.createFolder) {
|
|
370
|
+
const folderName = response?.error?.folderName || opts.folder;
|
|
371
|
+
process.stdout.write(`\nFolder "${folderName}" does not exist in Notesia.\n`);
|
|
372
|
+
const shouldCreate = await promptYesNo('Do you want to create it and continue import? [y/N] ');
|
|
373
|
+
if (shouldCreate) {
|
|
374
|
+
// Retry with create_folder_if_missing = true
|
|
375
|
+
const retryPayload = { ...payload, create_folder_if_missing: true };
|
|
376
|
+
response = await bridge({ op: 'bulk_import', payload: retryPayload }, { timeoutMs });
|
|
377
|
+
if (response?.ok === false) {
|
|
378
|
+
const retryCode = response?.error?.code || 'BRIDGE_ERROR';
|
|
379
|
+
const retryMessage = response?.error?.message || 'Bridge request failed';
|
|
380
|
+
process.stderr.write(`[${retryCode}] ${retryMessage}\n`);
|
|
381
|
+
process.exit(1);
|
|
382
|
+
}
|
|
383
|
+
} else {
|
|
384
|
+
process.stderr.write('Import cancelled.\n');
|
|
385
|
+
process.exit(1);
|
|
386
|
+
}
|
|
387
|
+
} else if (code === 'FOLDER_NOT_FOUND' && opts.folder && !opts.createFolder) {
|
|
388
|
+
// User specified --no-create-folder, so just show the error
|
|
389
|
+
process.stderr.write(`[${code}] ${message}\n`);
|
|
390
|
+
process.exit(1);
|
|
391
|
+
} else {
|
|
392
|
+
process.stderr.write(`[${code}] ${message}\n`);
|
|
393
|
+
process.exit(1);
|
|
394
|
+
}
|
|
298
395
|
}
|
|
299
396
|
|
|
300
397
|
const result = { ...response.data, elapsedMs: response.data?.elapsedMs ?? (Date.now() - startMs) };
|
package/src/io.mjs
CHANGED
|
@@ -170,7 +170,9 @@ export function printKbResult(op, result, {
|
|
|
170
170
|
if (citations.length > 0) {
|
|
171
171
|
writeStdout('\n\nSources:\n');
|
|
172
172
|
for (const c of citations) {
|
|
173
|
-
|
|
173
|
+
const sourceType = c.type ? `[${c.type.toUpperCase()}] ` : '';
|
|
174
|
+
const id = c.noteId ?? c.todoId ?? 'unknown';
|
|
175
|
+
writeStdout(` - ${sourceType}[${id}] ${c.title}\n`);
|
|
174
176
|
}
|
|
175
177
|
} else {
|
|
176
178
|
writeStdout('\n');
|