pal-explorer-cli 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (156) hide show
  1. package/LICENSE.md +18 -0
  2. package/README.md +314 -0
  3. package/bin/pal.js +230 -0
  4. package/extensions/@palexplorer/analytics/README.md +45 -0
  5. package/extensions/@palexplorer/analytics/docs/MONETIZATION.md +14 -0
  6. package/extensions/@palexplorer/analytics/docs/PLAN.md +23 -0
  7. package/extensions/@palexplorer/analytics/docs/PRIVACY.md +38 -0
  8. package/extensions/@palexplorer/analytics/extension.json +27 -0
  9. package/extensions/@palexplorer/analytics/index.js +186 -0
  10. package/extensions/@palexplorer/analytics/test/analytics.test.js +82 -0
  11. package/extensions/@palexplorer/audit/extension.json +17 -0
  12. package/extensions/@palexplorer/audit/index.js +2 -0
  13. package/extensions/@palexplorer/auth-email/extension.json +17 -0
  14. package/extensions/@palexplorer/auth-email/index.js +102 -0
  15. package/extensions/@palexplorer/auth-oauth/extension.json +16 -0
  16. package/extensions/@palexplorer/auth-oauth/index.js +199 -0
  17. package/extensions/@palexplorer/chat/extension.json +17 -0
  18. package/extensions/@palexplorer/chat/index.js +2 -0
  19. package/extensions/@palexplorer/discovery/extension.json +16 -0
  20. package/extensions/@palexplorer/discovery/index.js +111 -0
  21. package/extensions/@palexplorer/email-notifications/extension.json +23 -0
  22. package/extensions/@palexplorer/email-notifications/index.js +242 -0
  23. package/extensions/@palexplorer/explorer-integration/extension.json +13 -0
  24. package/extensions/@palexplorer/explorer-integration/index.js +122 -0
  25. package/extensions/@palexplorer/groups/extension.json +17 -0
  26. package/extensions/@palexplorer/groups/index.js +2 -0
  27. package/extensions/@palexplorer/networks/extension.json +17 -0
  28. package/extensions/@palexplorer/networks/index.js +2 -0
  29. package/extensions/@palexplorer/share-links/extension.json +17 -0
  30. package/extensions/@palexplorer/share-links/index.js +2 -0
  31. package/extensions/@palexplorer/sync/extension.json +17 -0
  32. package/extensions/@palexplorer/sync/index.js +2 -0
  33. package/extensions/@palexplorer/user-mgmt/extension.json +17 -0
  34. package/extensions/@palexplorer/user-mgmt/index.js +2 -0
  35. package/extensions/@palexplorer/vfs/extension.json +17 -0
  36. package/extensions/@palexplorer/vfs/index.js +167 -0
  37. package/lib/capabilities.js +263 -0
  38. package/lib/commands/analytics.js +175 -0
  39. package/lib/commands/api-keys.js +131 -0
  40. package/lib/commands/audit.js +235 -0
  41. package/lib/commands/auth.js +137 -0
  42. package/lib/commands/backup.js +76 -0
  43. package/lib/commands/billing.js +148 -0
  44. package/lib/commands/chat.js +217 -0
  45. package/lib/commands/cloud-backup.js +231 -0
  46. package/lib/commands/comment.js +99 -0
  47. package/lib/commands/completion.js +203 -0
  48. package/lib/commands/compliance.js +218 -0
  49. package/lib/commands/config.js +136 -0
  50. package/lib/commands/connect.js +44 -0
  51. package/lib/commands/dept.js +294 -0
  52. package/lib/commands/device.js +146 -0
  53. package/lib/commands/download.js +226 -0
  54. package/lib/commands/explorer.js +178 -0
  55. package/lib/commands/extension.js +970 -0
  56. package/lib/commands/favorite.js +90 -0
  57. package/lib/commands/federation.js +270 -0
  58. package/lib/commands/file.js +533 -0
  59. package/lib/commands/group.js +271 -0
  60. package/lib/commands/gui-share.js +29 -0
  61. package/lib/commands/init.js +61 -0
  62. package/lib/commands/invite.js +59 -0
  63. package/lib/commands/list.js +59 -0
  64. package/lib/commands/log.js +116 -0
  65. package/lib/commands/nearby.js +108 -0
  66. package/lib/commands/network.js +251 -0
  67. package/lib/commands/notify.js +198 -0
  68. package/lib/commands/org.js +273 -0
  69. package/lib/commands/pal.js +180 -0
  70. package/lib/commands/permissions.js +216 -0
  71. package/lib/commands/pin.js +97 -0
  72. package/lib/commands/protocol.js +357 -0
  73. package/lib/commands/rbac.js +147 -0
  74. package/lib/commands/recover.js +36 -0
  75. package/lib/commands/register.js +171 -0
  76. package/lib/commands/relay.js +131 -0
  77. package/lib/commands/remote.js +368 -0
  78. package/lib/commands/revoke.js +50 -0
  79. package/lib/commands/scanner.js +280 -0
  80. package/lib/commands/schedule.js +344 -0
  81. package/lib/commands/scim.js +203 -0
  82. package/lib/commands/search.js +181 -0
  83. package/lib/commands/serve.js +438 -0
  84. package/lib/commands/server.js +350 -0
  85. package/lib/commands/share-link.js +199 -0
  86. package/lib/commands/share.js +323 -0
  87. package/lib/commands/sso.js +200 -0
  88. package/lib/commands/status.js +136 -0
  89. package/lib/commands/stream.js +562 -0
  90. package/lib/commands/su.js +187 -0
  91. package/lib/commands/sync.js +827 -0
  92. package/lib/commands/transfers.js +152 -0
  93. package/lib/commands/uninstall.js +188 -0
  94. package/lib/commands/update.js +204 -0
  95. package/lib/commands/user.js +276 -0
  96. package/lib/commands/vfs.js +84 -0
  97. package/lib/commands/web.js +52 -0
  98. package/lib/commands/webhook.js +180 -0
  99. package/lib/commands/whoami.js +59 -0
  100. package/lib/commands/workspace.js +121 -0
  101. package/lib/core/accessLog.js +54 -0
  102. package/lib/core/analytics.js +99 -0
  103. package/lib/core/backup.js +84 -0
  104. package/lib/core/billing.js +336 -0
  105. package/lib/core/bitfieldStore.js +53 -0
  106. package/lib/core/connectionManager.js +182 -0
  107. package/lib/core/dhtDiscovery.js +148 -0
  108. package/lib/core/discoveryClient.js +408 -0
  109. package/lib/core/extensionAnalyzer.js +357 -0
  110. package/lib/core/extensionSandbox.js +250 -0
  111. package/lib/core/extensionWorkerHost.js +166 -0
  112. package/lib/core/extensions.js +1082 -0
  113. package/lib/core/fileDiff.js +69 -0
  114. package/lib/core/groups.js +119 -0
  115. package/lib/core/identity.js +340 -0
  116. package/lib/core/mdnsService.js +126 -0
  117. package/lib/core/networks.js +81 -0
  118. package/lib/core/permissions.js +109 -0
  119. package/lib/core/pro.js +27 -0
  120. package/lib/core/resolver.js +74 -0
  121. package/lib/core/serverList.js +224 -0
  122. package/lib/core/sharePolicy.js +69 -0
  123. package/lib/core/shares.js +325 -0
  124. package/lib/core/signalingServer.js +441 -0
  125. package/lib/core/streamTransport.js +106 -0
  126. package/lib/core/su.js +55 -0
  127. package/lib/core/syncEngine.js +264 -0
  128. package/lib/core/syncState.js +159 -0
  129. package/lib/core/transfers.js +259 -0
  130. package/lib/core/users.js +225 -0
  131. package/lib/core/vfs.js +216 -0
  132. package/lib/core/webServer.js +702 -0
  133. package/lib/core/webrtcStream.js +396 -0
  134. package/lib/crypto/chatEncryption.js +57 -0
  135. package/lib/crypto/shareEncryption.js +195 -0
  136. package/lib/crypto/sharePassword.js +35 -0
  137. package/lib/crypto/streamEncryption.js +189 -0
  138. package/lib/package.json +1 -0
  139. package/lib/protocol/envelope.js +271 -0
  140. package/lib/protocol/handler.js +191 -0
  141. package/lib/protocol/index.js +27 -0
  142. package/lib/protocol/messages.js +247 -0
  143. package/lib/protocol/negotiation.js +127 -0
  144. package/lib/protocol/policy.js +142 -0
  145. package/lib/protocol/router.js +86 -0
  146. package/lib/protocol/sync.js +122 -0
  147. package/lib/utils/cli.js +15 -0
  148. package/lib/utils/config.js +123 -0
  149. package/lib/utils/configIntegrity.js +87 -0
  150. package/lib/utils/downloadDir.js +9 -0
  151. package/lib/utils/explorer.js +83 -0
  152. package/lib/utils/format.js +12 -0
  153. package/lib/utils/help.js +357 -0
  154. package/lib/utils/logger.js +103 -0
  155. package/lib/utils/torrent.js +203 -0
  156. package/package.json +71 -0
@@ -0,0 +1,533 @@
1
+ import chalk from 'chalk';
2
+ import fs from 'fs';
3
+ import path from 'path';
4
+ import { requireIdentity, checkPathAccess, auditLog } from '../core/permissions.js';
5
+ import { formatSize } from '../utils/format.js';
6
+ import { printJson } from '../utils/cli.js';
7
+
8
+ function formatDate(date) {
9
+ return new Date(date).toLocaleString();
10
+ }
11
+
12
+ export default function fileCommand(program) {
13
+ const file = program
14
+ .command('file')
15
+ .description('file operations with Palexplorer permissions')
16
+ .addHelpText('after', `
17
+ Examples:
18
+ $ pe file ls /path/to/dir List directory contents
19
+ $ pe file tree /path/to/dir Show directory tree
20
+ $ pe file info /path/to/file Show file properties
21
+ $ pe file copy src dest Copy file or directory
22
+ $ pe file move src dest Move file or directory
23
+ $ pe file rename old new Rename file or directory
24
+ $ pe file mkdir /path/to/dir Create directory
25
+ $ pe file delete /path/to/file Delete file or directory
26
+ $ pe file search pattern [dir] Search files by name pattern
27
+ $ pe file open /path/to/file Open file with OS default app
28
+ $ pe file reveal /path/to/file Show file in OS file explorer
29
+ `);
30
+
31
+ // ── ls ──────────────────────────────────────────────────────────────────
32
+ file
33
+ .command('ls [dir]')
34
+ .description('list directory contents')
35
+ .option('-a, --all', 'Show hidden files')
36
+ .option('-l, --long', 'Detailed list with sizes and dates')
37
+ .option('-s, --sort <field>', 'Sort by: name, size, date', 'name')
38
+ .option('--json', 'Output as JSON')
39
+ .action(async (dir, opts) => {
40
+ try {
41
+ requireIdentity();
42
+ const targetDir = path.resolve(dir || '.');
43
+ checkPathAccess(targetDir, 'read');
44
+
45
+ const entries = fs.readdirSync(targetDir, { withFileTypes: true });
46
+ let items = entries
47
+ .filter(e => opts.all || !e.name.startsWith('.'))
48
+ .map(e => {
49
+ const fullPath = path.join(targetDir, e.name);
50
+ let stat = null;
51
+ try { stat = fs.statSync(fullPath); } catch {}
52
+ return {
53
+ name: e.name,
54
+ isDirectory: e.isDirectory(),
55
+ size: stat?.size || 0,
56
+ modified: stat?.mtime?.toISOString() || null,
57
+ };
58
+ });
59
+
60
+ if (opts.sort === 'size') items.sort((a, b) => b.size - a.size);
61
+ else if (opts.sort === 'date') items.sort((a, b) => new Date(b.modified) - new Date(a.modified));
62
+ else items.sort((a, b) => a.name.localeCompare(b.name));
63
+
64
+ // Directories first
65
+ items.sort((a, b) => (b.isDirectory ? 1 : 0) - (a.isDirectory ? 1 : 0));
66
+
67
+ if (opts.json || program.opts().json) { printJson(items); return; }
68
+
69
+ console.log(chalk.gray(` ${targetDir}\n`));
70
+
71
+ if (items.length === 0) {
72
+ console.log(chalk.dim(' (empty directory)'));
73
+ return;
74
+ }
75
+
76
+ if (opts.long) {
77
+ const maxNameLen = Math.max(...items.map(i => i.name.length), 4);
78
+ for (const item of items) {
79
+ const icon = item.isDirectory ? chalk.blue('DIR ') : chalk.gray('FILE');
80
+ const size = item.isDirectory ? chalk.dim(' -') : chalk.yellow(formatSize(item.size).padStart(8));
81
+ const date = item.modified ? chalk.dim(formatDate(item.modified)) : '';
82
+ const name = item.isDirectory ? chalk.blue.bold(item.name) : item.name;
83
+ console.log(` ${icon} ${size} ${date} ${name}`);
84
+ }
85
+ } else {
86
+ for (const item of items) {
87
+ const name = item.isDirectory ? chalk.blue.bold(item.name + '/') : item.name;
88
+ process.stdout.write(' ' + name + '\n');
89
+ }
90
+ }
91
+
92
+ const dirs = items.filter(i => i.isDirectory).length;
93
+ const files = items.length - dirs;
94
+ console.log(chalk.dim(`\n ${dirs} folder${dirs !== 1 ? 's' : ''}, ${files} file${files !== 1 ? 's' : ''}`));
95
+
96
+ auditLog('file.ls', { path: targetDir });
97
+ } catch (err) {
98
+ console.error(chalk.red(err.message));
99
+ process.exitCode = 1;
100
+ }
101
+ });
102
+
103
+ // ── tree ────────────────────────────────────────────────────────────────
104
+ file
105
+ .command('tree [dir]')
106
+ .description('show directory tree')
107
+ .option('-d, --depth <n>', 'Max depth', '3')
108
+ .option('--dirs-only', 'Show only directories')
109
+ .action(async (dir, opts) => {
110
+ try {
111
+ requireIdentity();
112
+ const targetDir = path.resolve(dir || '.');
113
+ checkPathAccess(targetDir, 'read');
114
+ const maxDepth = parseInt(opts.depth, 10);
115
+ let fileCount = 0, dirCount = 0;
116
+
117
+ function walk(dirPath, prefix, depth) {
118
+ if (depth > maxDepth) return;
119
+ let entries;
120
+ try { entries = fs.readdirSync(dirPath, { withFileTypes: true }); } catch { return; }
121
+ entries = entries.filter(e => !e.name.startsWith('.'));
122
+ entries.sort((a, b) => {
123
+ if (a.isDirectory() !== b.isDirectory()) return b.isDirectory() ? 1 : -1;
124
+ return a.name.localeCompare(b.name);
125
+ });
126
+
127
+ if (opts.dirsOnly) entries = entries.filter(e => e.isDirectory());
128
+
129
+ entries.forEach((entry, idx) => {
130
+ const isLast = idx === entries.length - 1;
131
+ const connector = isLast ? '└── ' : '├── ';
132
+ const name = entry.isDirectory()
133
+ ? chalk.blue.bold(entry.name + '/')
134
+ : entry.name;
135
+ console.log(prefix + connector + name);
136
+ if (entry.isDirectory()) {
137
+ dirCount++;
138
+ walk(path.join(dirPath, entry.name), prefix + (isLast ? ' ' : '│ '), depth + 1);
139
+ } else {
140
+ fileCount++;
141
+ }
142
+ });
143
+ }
144
+
145
+ console.log(chalk.blue.bold(path.basename(targetDir) + '/'));
146
+ walk(targetDir, '', 1);
147
+ console.log(chalk.dim(`\n${dirCount} directories, ${fileCount} files`));
148
+ auditLog('file.tree', { path: targetDir });
149
+ } catch (err) {
150
+ console.error(chalk.red(err.message));
151
+ process.exitCode = 1;
152
+ }
153
+ });
154
+
155
+ // ── info ────────────────────────────────────────────────────────────────
156
+ file
157
+ .command('info <target>')
158
+ .description('show file or directory properties')
159
+ .option('--json', 'Output as JSON')
160
+ .action(async (target, opts) => {
161
+ try {
162
+ requireIdentity();
163
+ const resolved = path.resolve(target);
164
+ const access = checkPathAccess(resolved, 'read');
165
+ const stat = fs.statSync(resolved);
166
+
167
+ const props = {
168
+ name: path.basename(resolved),
169
+ path: resolved,
170
+ type: stat.isDirectory() ? 'directory' : 'file',
171
+ size: stat.size,
172
+ created: stat.birthtime.toISOString(),
173
+ modified: stat.mtime.toISOString(),
174
+ accessed: stat.atime.toISOString(),
175
+ readonly: !(stat.mode & 0o200),
176
+ isShared: access.isShared,
177
+ };
178
+
179
+ if (stat.isDirectory()) {
180
+ let fileCount = 0, folderCount = 0, totalSize = 0;
181
+ const walk = (dir) => {
182
+ try {
183
+ for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
184
+ const full = path.join(dir, entry.name);
185
+ if (entry.isDirectory()) { folderCount++; walk(full); }
186
+ else { fileCount++; try { totalSize += fs.statSync(full).size; } catch {} }
187
+ }
188
+ } catch {}
189
+ };
190
+ walk(resolved);
191
+ props.fileCount = fileCount;
192
+ props.folderCount = folderCount;
193
+ props.totalSize = totalSize;
194
+ }
195
+
196
+ if (opts.json || program.opts().json) { printJson(props); return; }
197
+
198
+ console.log(chalk.bold(`\n ${props.name}`));
199
+ console.log(chalk.gray(' ' + '─'.repeat(40)));
200
+ console.log(` Type: ${props.type}`);
201
+ console.log(` Path: ${chalk.dim(props.path)}`);
202
+ console.log(` Size: ${chalk.yellow(formatSize(props.size))}`);
203
+ if (props.totalSize != null) {
204
+ console.log(` Total: ${chalk.yellow(formatSize(props.totalSize))} (${props.fileCount} files, ${props.folderCount} folders)`);
205
+ }
206
+ console.log(` Created: ${formatDate(props.created)}`);
207
+ console.log(` Modified: ${formatDate(props.modified)}`);
208
+ console.log(` Read-only: ${props.readonly ? chalk.yellow('Yes') : 'No'}`);
209
+ console.log(` Shared: ${props.isShared ? chalk.green('Yes') : 'No'}`);
210
+ console.log();
211
+
212
+ auditLog('file.info', { path: resolved });
213
+ } catch (err) {
214
+ console.error(chalk.red(err.message));
215
+ process.exitCode = 1;
216
+ }
217
+ });
218
+
219
+ // ── copy ────────────────────────────────────────────────────────────────
220
+ file
221
+ .command('copy <source> <destination>')
222
+ .description('copy file or directory')
223
+ .option('-r, --recursive', 'Copy directories recursively (default for directories)')
224
+ .action(async (source, destination, opts) => {
225
+ try {
226
+ requireIdentity();
227
+ const src = path.resolve(source);
228
+ let dest = path.resolve(destination);
229
+ checkPathAccess(src, 'read');
230
+ checkPathAccess(dest, 'write');
231
+
232
+ try {
233
+ if (fs.statSync(dest).isDirectory()) {
234
+ dest = path.join(dest, path.basename(src));
235
+ }
236
+ } catch {}
237
+
238
+ const stat = fs.statSync(src);
239
+ if (stat.isDirectory()) {
240
+ fs.cpSync(src, dest, { recursive: true });
241
+ } else {
242
+ fs.mkdirSync(path.dirname(dest), { recursive: true });
243
+ fs.copyFileSync(src, dest);
244
+ }
245
+
246
+ console.log(chalk.green(`Copied ${path.basename(src)} → ${dest}`));
247
+ auditLog('file.copy', { source: src, destination: dest });
248
+ } catch (err) {
249
+ console.error(chalk.red(err.message));
250
+ process.exitCode = 1;
251
+ }
252
+ });
253
+
254
+ // ── move ────────────────────────────────────────────────────────────────
255
+ file
256
+ .command('move <source> <destination>')
257
+ .description('move file or directory')
258
+ .action(async (source, destination) => {
259
+ try {
260
+ requireIdentity();
261
+ const src = path.resolve(source);
262
+ let dest = path.resolve(destination);
263
+ checkPathAccess(src, 'move');
264
+ checkPathAccess(dest, 'write');
265
+
266
+ try {
267
+ if (fs.statSync(dest).isDirectory()) {
268
+ dest = path.join(dest, path.basename(src));
269
+ }
270
+ } catch {}
271
+
272
+ fs.renameSync(src, dest);
273
+ console.log(chalk.green(`Moved ${path.basename(src)} → ${dest}`));
274
+ auditLog('file.move', { source: src, destination: dest });
275
+ } catch (err) {
276
+ console.error(chalk.red(err.message));
277
+ process.exitCode = 1;
278
+ }
279
+ });
280
+
281
+ // ── rename ──────────────────────────────────────────────────────────────
282
+ file
283
+ .command('rename <target> <newName>')
284
+ .description('rename file or directory')
285
+ .action(async (target, newName) => {
286
+ try {
287
+ requireIdentity();
288
+ const src = path.resolve(target);
289
+ checkPathAccess(src, 'write');
290
+
291
+ if (newName.includes('/') || newName.includes('\\')) {
292
+ throw new Error('New name cannot contain path separators. Use `pe file move` instead.');
293
+ }
294
+
295
+ const dest = path.join(path.dirname(src), newName);
296
+ fs.renameSync(src, dest);
297
+ console.log(chalk.green(`Renamed ${path.basename(src)} → ${newName}`));
298
+ auditLog('file.rename', { source: src, newName });
299
+ } catch (err) {
300
+ console.error(chalk.red(err.message));
301
+ process.exitCode = 1;
302
+ }
303
+ });
304
+
305
+ // ── mkdir ───────────────────────────────────────────────────────────────
306
+ file
307
+ .command('mkdir <dir>')
308
+ .description('create directory')
309
+ .option('-p, --parents', 'Create parent directories as needed')
310
+ .action(async (dir, opts) => {
311
+ try {
312
+ requireIdentity();
313
+ const targetDir = path.resolve(dir);
314
+ checkPathAccess(targetDir, 'write');
315
+
316
+ fs.mkdirSync(targetDir, { recursive: !!opts.parents });
317
+ console.log(chalk.green(`Created ${targetDir}`));
318
+ auditLog('file.mkdir', { path: targetDir });
319
+ } catch (err) {
320
+ console.error(chalk.red(err.message));
321
+ process.exitCode = 1;
322
+ }
323
+ });
324
+
325
+ // ── delete ──────────────────────────────────────────────────────────────
326
+ file
327
+ .command('delete <targets...>')
328
+ .description('delete files or directories')
329
+ .option('-f, --force', 'Skip confirmation')
330
+ .action(async (targets, opts) => {
331
+ try {
332
+ requireIdentity();
333
+ const resolved = targets.map(t => path.resolve(t));
334
+ for (const p of resolved) checkPathAccess(p, 'delete');
335
+
336
+ if (!opts.force) {
337
+ const names = resolved.map(p => path.basename(p)).join(', ');
338
+ process.stdout.write(chalk.yellow(`Delete ${names}? [y/N] `));
339
+ const answer = await new Promise(resolve => {
340
+ process.stdin.setEncoding('utf8');
341
+ process.stdin.once('data', data => resolve(data.trim().toLowerCase()));
342
+ });
343
+ if (answer !== 'y' && answer !== 'yes') {
344
+ console.log('Cancelled.');
345
+ return;
346
+ }
347
+ }
348
+
349
+ for (const p of resolved) {
350
+ fs.rmSync(p, { recursive: true, force: true });
351
+ console.log(chalk.green(`Deleted ${path.basename(p)}`));
352
+ }
353
+ auditLog('file.delete', { paths: resolved });
354
+ } catch (err) {
355
+ console.error(chalk.red(err.message));
356
+ process.exitCode = 1;
357
+ }
358
+ });
359
+
360
+ // ── search ──────────────────────────────────────────────────────────────
361
+ file
362
+ .command('search <pattern> [dir]')
363
+ .description('search files by name pattern (glob-like)')
364
+ .option('-t, --type <type>', 'Filter: file, dir, all', 'all')
365
+ .option('-d, --depth <n>', 'Max depth', '10')
366
+ .option('-l, --limit <n>', 'Max results', '100')
367
+ .option('--json', 'Output as JSON')
368
+ .action(async (pattern, dir, opts) => {
369
+ try {
370
+ requireIdentity();
371
+ const targetDir = path.resolve(dir || '.');
372
+ checkPathAccess(targetDir, 'read');
373
+
374
+ const maxDepth = parseInt(opts.depth, 10);
375
+ const maxResults = parseInt(opts.limit, 10);
376
+ const regex = new RegExp(
377
+ pattern.replace(/\./g, '\\.').replace(/\*/g, '.*').replace(/\?/g, '.'),
378
+ 'i'
379
+ );
380
+ const results = [];
381
+
382
+ function walk(dirPath, depth) {
383
+ if (depth > maxDepth || results.length >= maxResults) return;
384
+ let entries;
385
+ try { entries = fs.readdirSync(dirPath, { withFileTypes: true }); } catch { return; }
386
+ for (const entry of entries) {
387
+ if (results.length >= maxResults) break;
388
+ const fullPath = path.join(dirPath, entry.name);
389
+ const isDir = entry.isDirectory();
390
+
391
+ if (regex.test(entry.name)) {
392
+ if (opts.type === 'all' || (opts.type === 'file' && !isDir) || (opts.type === 'dir' && isDir)) {
393
+ let size = null;
394
+ try { size = fs.statSync(fullPath).size; } catch {}
395
+ results.push({ name: entry.name, path: fullPath, isDirectory: isDir, size });
396
+ }
397
+ }
398
+
399
+ if (isDir) walk(fullPath, depth + 1);
400
+ }
401
+ }
402
+
403
+ walk(targetDir, 1);
404
+
405
+ if (opts.json || program.opts().json) { printJson(results); return; }
406
+
407
+ if (results.length === 0) {
408
+ console.log(chalk.dim(`No matches for "${pattern}" in ${targetDir}`));
409
+ return;
410
+ }
411
+
412
+ console.log(chalk.gray(`\n Found ${results.length} match${results.length !== 1 ? 'es' : ''} for "${pattern}":\n`));
413
+ for (const r of results) {
414
+ const icon = r.isDirectory ? chalk.blue('DIR ') : chalk.gray('FILE');
415
+ const size = r.isDirectory ? '' : chalk.dim(` (${formatSize(r.size)})`);
416
+ const rel = path.relative(targetDir, r.path);
417
+ console.log(` ${icon} ${rel}${size}`);
418
+ }
419
+ console.log();
420
+
421
+ auditLog('file.search', { pattern, dir: targetDir, resultCount: results.length });
422
+ } catch (err) {
423
+ console.error(chalk.red(err.message));
424
+ process.exitCode = 1;
425
+ }
426
+ });
427
+
428
+ // ── open ────────────────────────────────────────────────────────────────
429
+ file
430
+ .command('open <target>')
431
+ .description('open file with OS default application')
432
+ .action(async (target) => {
433
+ try {
434
+ requireIdentity();
435
+ const resolved = path.resolve(target);
436
+ checkPathAccess(resolved, 'read');
437
+
438
+ const { execFile } = await import('child_process');
439
+ if (process.platform === 'win32') {
440
+ execFile('cmd', ['/c', 'start', '""', resolved], (err) => {
441
+ if (err) console.error(chalk.red(`Failed to open: ${err.message}`));
442
+ });
443
+ } else if (process.platform === 'darwin') {
444
+ execFile('open', [resolved], (err) => {
445
+ if (err) console.error(chalk.red(`Failed to open: ${err.message}`));
446
+ });
447
+ } else {
448
+ execFile('xdg-open', [resolved], (err) => {
449
+ if (err) console.error(chalk.red(`Failed to open: ${err.message}`));
450
+ });
451
+ }
452
+
453
+ auditLog('file.open', { path: resolved });
454
+ } catch (err) {
455
+ console.error(chalk.red(err.message));
456
+ process.exitCode = 1;
457
+ }
458
+ });
459
+
460
+ // ── reveal ──────────────────────────────────────────────────────────────
461
+ file
462
+ .command('reveal <target>')
463
+ .description('show file in OS file explorer')
464
+ .action(async (target) => {
465
+ try {
466
+ requireIdentity();
467
+ const resolved = path.resolve(target);
468
+ checkPathAccess(resolved, 'read');
469
+
470
+ const { execFile } = await import('child_process');
471
+ if (process.platform === 'win32') {
472
+ execFile('explorer', ['/select,', resolved], (err) => {
473
+ if (err) console.error(chalk.red(`Failed to reveal: ${err.message}`));
474
+ });
475
+ } else if (process.platform === 'darwin') {
476
+ execFile('open', ['-R', resolved], (err) => {
477
+ if (err) console.error(chalk.red(`Failed to reveal: ${err.message}`));
478
+ });
479
+ } else {
480
+ execFile('xdg-open', [path.dirname(resolved)], (err) => {
481
+ if (err) console.error(chalk.red(`Failed to reveal: ${err.message}`));
482
+ });
483
+ }
484
+
485
+ auditLog('file.reveal', { path: resolved });
486
+ } catch (err) {
487
+ console.error(chalk.red(err.message));
488
+ process.exitCode = 1;
489
+ }
490
+ });
491
+
492
+ // ── audit ───────────────────────────────────────────────────────────────
493
+ file
494
+ .command('audit')
495
+ .description('show file operation audit log')
496
+ .option('-n, --limit <n>', 'Number of entries', '20')
497
+ .option('--clear', 'Clear audit log')
498
+ .option('--json', 'Output as JSON')
499
+ .action(async (opts) => {
500
+ try {
501
+ requireIdentity();
502
+ const { getAuditLog, clearAuditLog } = await import('../core/permissions.js');
503
+
504
+ if (opts.clear) {
505
+ clearAuditLog();
506
+ console.log(chalk.green('Audit log cleared.'));
507
+ return;
508
+ }
509
+
510
+ const entries = getAuditLog(parseInt(opts.limit, 10));
511
+
512
+ if (opts.json || program.opts().json) { printJson(entries); return; }
513
+
514
+ if (entries.length === 0) {
515
+ console.log(chalk.dim('No audit entries.'));
516
+ return;
517
+ }
518
+
519
+ console.log(chalk.bold('\n File Operation Audit Log\n'));
520
+ for (const entry of entries) {
521
+ const time = chalk.dim(new Date(entry.timestamp).toLocaleString());
522
+ const action = chalk.cyan(entry.action.padEnd(14));
523
+ const user = chalk.yellow(entry.user);
524
+ const detail = entry.path || entry.source || '';
525
+ console.log(` ${time} ${action} ${user} ${chalk.gray(detail)}`);
526
+ }
527
+ console.log();
528
+ } catch (err) {
529
+ console.error(chalk.red(err.message));
530
+ process.exitCode = 1;
531
+ }
532
+ });
533
+ }