Refactor file listing in list_files.js to return structured objects for easier machine parsing and consistent output. Update sorting mechanism to ensure files are sorted by path before returning.
This commit is contained in:
@@ -160,7 +160,8 @@ export async function run(args) {
|
||||
if (!includeHidden && fileName.startsWith(".")) {
|
||||
return { cwd, files: [] };
|
||||
}
|
||||
return { cwd, files: JSON.stringify([[fileName, 'f', stat.size]]) };
|
||||
// Return structured object for easier machine parsing
|
||||
return { cwd, files: [{ path: fileName, type: 'f', size: stat.size }] };
|
||||
}
|
||||
|
||||
// Handle non-directory case
|
||||
@@ -171,9 +172,13 @@ export async function run(args) {
|
||||
// Handle directory case
|
||||
try {
|
||||
const files = await listEntriesRecursive(resolvedBase, chrootResolved, depth === -1 ? Infinity : depth, includeHidden);
|
||||
// Map to structured objects and sort by path for consistent output
|
||||
const mapped = files
|
||||
.map(([p, t, s]) => ({ path: p, type: t, size: s }))
|
||||
.sort((a, b) => a.path.localeCompare(b.path));
|
||||
return {
|
||||
cwd,
|
||||
files: files.sort((a, b) => a[0].localeCompare(b[0])), // Sort for consistent output
|
||||
files: mapped,
|
||||
};
|
||||
} catch (err) {
|
||||
return { err: `Failed to list files: ${err?.message || String(err)}` };
|
||||
|
||||
Reference in New Issue
Block a user