pal-explorer-cli 0.4.12 → 0.4.13
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/README.md +149 -149
- package/bin/pal.js +63 -2
- package/extensions/@palexplorer/analytics/extension.json +20 -1
- package/extensions/@palexplorer/analytics/index.js +19 -9
- package/extensions/@palexplorer/audit/extension.json +14 -0
- package/extensions/@palexplorer/auth-email/extension.json +15 -0
- package/extensions/@palexplorer/auth-oauth/extension.json +15 -0
- package/extensions/@palexplorer/chat/extension.json +14 -0
- package/extensions/@palexplorer/discovery/extension.json +17 -0
- package/extensions/@palexplorer/discovery/index.js +1 -1
- package/extensions/@palexplorer/email-notifications/extension.json +23 -0
- package/extensions/@palexplorer/groups/extension.json +15 -0
- package/extensions/@palexplorer/share-links/extension.json +15 -0
- package/extensions/@palexplorer/sync/extension.json +16 -0
- package/extensions/@palexplorer/user-mgmt/extension.json +15 -0
- package/lib/capabilities.js +24 -24
- package/lib/commands/analytics.js +175 -175
- package/lib/commands/api-keys.js +131 -131
- package/lib/commands/audit.js +235 -235
- package/lib/commands/auth.js +137 -137
- package/lib/commands/backup.js +76 -76
- package/lib/commands/billing.js +148 -148
- package/lib/commands/chat.js +217 -217
- package/lib/commands/cloud-backup.js +231 -231
- package/lib/commands/comment.js +99 -99
- package/lib/commands/completion.js +203 -203
- package/lib/commands/compliance.js +218 -218
- package/lib/commands/config.js +136 -136
- package/lib/commands/connect.js +44 -44
- package/lib/commands/dept.js +294 -294
- package/lib/commands/device.js +146 -146
- package/lib/commands/download.js +240 -226
- package/lib/commands/explorer.js +178 -178
- package/lib/commands/extension.js +1060 -970
- package/lib/commands/favorite.js +90 -90
- package/lib/commands/federation.js +270 -270
- package/lib/commands/file.js +533 -533
- package/lib/commands/group.js +271 -271
- package/lib/commands/gui-share.js +29 -29
- package/lib/commands/init.js +61 -61
- package/lib/commands/invite.js +59 -59
- package/lib/commands/list.js +58 -58
- package/lib/commands/log.js +116 -116
- package/lib/commands/nearby.js +108 -108
- package/lib/commands/network.js +251 -251
- package/lib/commands/notify.js +198 -198
- package/lib/commands/org.js +273 -273
- package/lib/commands/pal.js +403 -180
- package/lib/commands/permissions.js +216 -216
- package/lib/commands/pin.js +97 -97
- package/lib/commands/protocol.js +357 -357
- package/lib/commands/rbac.js +147 -147
- package/lib/commands/recover.js +36 -36
- package/lib/commands/register.js +171 -171
- package/lib/commands/relay.js +131 -131
- package/lib/commands/remote.js +368 -368
- package/lib/commands/revoke.js +50 -50
- package/lib/commands/scanner.js +280 -280
- package/lib/commands/schedule.js +344 -344
- package/lib/commands/scim.js +203 -203
- package/lib/commands/search.js +181 -181
- package/lib/commands/serve.js +438 -438
- package/lib/commands/server.js +350 -350
- package/lib/commands/share-link.js +199 -199
- package/lib/commands/share.js +336 -323
- package/lib/commands/sso.js +200 -200
- package/lib/commands/status.js +145 -145
- package/lib/commands/stream.js +562 -562
- package/lib/commands/su.js +187 -187
- package/lib/commands/sync.js +979 -979
- package/lib/commands/transfers.js +152 -152
- package/lib/commands/uninstall.js +188 -188
- package/lib/commands/update.js +204 -204
- package/lib/commands/user.js +276 -276
- package/lib/commands/vfs.js +84 -84
- package/lib/commands/web-login.js +79 -79
- package/lib/commands/web.js +52 -52
- package/lib/commands/webhook.js +180 -180
- package/lib/commands/whoami.js +59 -59
- package/lib/commands/workspace.js +121 -121
- package/lib/core/billing.js +16 -5
- package/lib/core/dhtDiscovery.js +9 -2
- package/lib/core/discoveryClient.js +13 -7
- package/lib/core/extensions.js +142 -1
- package/lib/core/identity.js +33 -2
- package/lib/core/imageProcessor.js +109 -0
- package/lib/core/imageTorrent.js +167 -0
- package/lib/core/permissions.js +1 -1
- package/lib/core/pro.js +11 -4
- package/lib/core/serverList.js +4 -1
- package/lib/core/shares.js +12 -1
- package/lib/core/signalingServer.js +14 -2
- package/lib/core/su.js +1 -1
- package/lib/core/users.js +1 -1
- package/lib/protocol/messages.js +12 -3
- package/lib/utils/explorer.js +1 -1
- package/lib/utils/help.js +357 -357
- package/lib/utils/torrent.js +1 -0
- package/package.json +4 -3
package/lib/commands/file.js
CHANGED
|
@@ -1,533 +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
|
-
$
|
|
19
|
-
$
|
|
20
|
-
$
|
|
21
|
-
$
|
|
22
|
-
$
|
|
23
|
-
$
|
|
24
|
-
$
|
|
25
|
-
$
|
|
26
|
-
$
|
|
27
|
-
$
|
|
28
|
-
$
|
|
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 `
|
|
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
|
-
}
|
|
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
|
+
$ pal file ls /path/to/dir List directory contents
|
|
19
|
+
$ pal file tree /path/to/dir Show directory tree
|
|
20
|
+
$ pal file info /path/to/file Show file properties
|
|
21
|
+
$ pal file copy src dest Copy file or directory
|
|
22
|
+
$ pal file move src dest Move file or directory
|
|
23
|
+
$ pal file rename old new Rename file or directory
|
|
24
|
+
$ pal file mkdir /path/to/dir Create directory
|
|
25
|
+
$ pal file delete /path/to/file Delete file or directory
|
|
26
|
+
$ pal file search pattern [dir] Search files by name pattern
|
|
27
|
+
$ pal file open /path/to/file Open file with OS default app
|
|
28
|
+
$ pal 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 `pal 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
|
+
}
|