|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, Salesforce, Inc. |
| 3 | + * SPDX-License-Identifier: Apache-2 |
| 4 | + * For full license text, see the license.txt file in the repo root or http://www.apache.org/licenses/LICENSE-2.0 |
| 5 | + */ |
| 6 | +import {Args} from '@oclif/core'; |
| 7 | +import {WebDavCommand, createTable, type ColumnDef} from '@salesforce/b2c-tooling-sdk/cli'; |
| 8 | +import type {PropfindEntry} from '@salesforce/b2c-tooling-sdk/clients'; |
| 9 | +import {t} from '../../i18n/index.js'; |
| 10 | + |
| 11 | +/** |
| 12 | + * Formats bytes into human-readable sizes. |
| 13 | + */ |
| 14 | +function formatBytes(bytes: number | undefined): string { |
| 15 | + if (bytes === undefined || bytes === null) return '-'; |
| 16 | + if (bytes === 0) return '0 B'; |
| 17 | + |
| 18 | + const units = ['B', 'KB', 'MB', 'GB']; |
| 19 | + const k = 1024; |
| 20 | + const i = Math.min(Math.floor(Math.log(bytes) / Math.log(k)), units.length - 1); |
| 21 | + const value = bytes / k ** i; |
| 22 | + |
| 23 | + return `${value.toFixed(i === 0 ? 0 : 1)} ${units[i]}`; |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * Extracts the display name from a PropfindEntry. |
| 28 | + */ |
| 29 | +function getDisplayName(entry: PropfindEntry): string { |
| 30 | + if (entry.displayName) { |
| 31 | + return entry.displayName; |
| 32 | + } |
| 33 | + // Extract filename from href |
| 34 | + const parts = entry.href.split('/').filter(Boolean); |
| 35 | + return parts.at(-1) || entry.href; |
| 36 | +} |
| 37 | + |
| 38 | +const COLUMNS: Record<string, ColumnDef<PropfindEntry>> = { |
| 39 | + name: { |
| 40 | + header: 'Name', |
| 41 | + get: (e) => getDisplayName(e), |
| 42 | + }, |
| 43 | + type: { |
| 44 | + header: 'Type', |
| 45 | + get: (e) => (e.isCollection ? 'dir' : 'file'), |
| 46 | + }, |
| 47 | + size: { |
| 48 | + header: 'Size', |
| 49 | + get: (e) => formatBytes(e.contentLength), |
| 50 | + }, |
| 51 | + modified: { |
| 52 | + header: 'Modified', |
| 53 | + get: (e) => (e.lastModified ? e.lastModified.toLocaleString() : '-'), |
| 54 | + extended: true, |
| 55 | + }, |
| 56 | + contentType: { |
| 57 | + header: 'Content-Type', |
| 58 | + get: (e) => e.contentType || '-', |
| 59 | + extended: true, |
| 60 | + }, |
| 61 | +}; |
| 62 | + |
| 63 | +const DEFAULT_COLUMNS = ['name', 'type', 'size']; |
| 64 | + |
| 65 | +interface LsResult { |
| 66 | + path: string; |
| 67 | + count: number; |
| 68 | + entries: PropfindEntry[]; |
| 69 | +} |
| 70 | + |
| 71 | +export default class WebDavLs extends WebDavCommand<typeof WebDavLs> { |
| 72 | + static args = { |
| 73 | + path: Args.string({ |
| 74 | + description: 'Path relative to root (defaults to root directory)', |
| 75 | + default: '', |
| 76 | + }), |
| 77 | + }; |
| 78 | + |
| 79 | + static description = t('commands.webdav.ls.description', 'List files and directories in a WebDAV location'); |
| 80 | + |
| 81 | + static enableJsonFlag = true; |
| 82 | + |
| 83 | + static examples = [ |
| 84 | + '<%= config.bin %> <%= command.id %>', |
| 85 | + '<%= config.bin %> <%= command.id %> src/instance', |
| 86 | + '<%= config.bin %> <%= command.id %> --root=cartridges', |
| 87 | + '<%= config.bin %> <%= command.id %> --root=logs --json', |
| 88 | + ]; |
| 89 | + |
| 90 | + async run(): Promise<LsResult> { |
| 91 | + this.ensureWebDavAuth(); |
| 92 | + |
| 93 | + const fullPath = this.buildPath(this.args.path); |
| 94 | + |
| 95 | + this.log(t('commands.webdav.ls.listing', 'Listing {{path}}...', {path: fullPath})); |
| 96 | + |
| 97 | + const entries = await this.instance.webdav.propfind(fullPath, '1'); |
| 98 | + |
| 99 | + // Filter out the parent directory itself (first entry is usually the queried path) |
| 100 | + const filteredEntries = entries.filter((entry) => { |
| 101 | + const entryPath = decodeURIComponent(entry.href); |
| 102 | + const normalizedFullPath = fullPath.replace(/\/$/, ''); |
| 103 | + return !entryPath.endsWith(`/${normalizedFullPath}`) && !entryPath.endsWith(`/${normalizedFullPath}/`); |
| 104 | + }); |
| 105 | + |
| 106 | + const result: LsResult = { |
| 107 | + path: fullPath, |
| 108 | + count: filteredEntries.length, |
| 109 | + entries: filteredEntries, |
| 110 | + }; |
| 111 | + |
| 112 | + if (this.jsonEnabled()) { |
| 113 | + return result; |
| 114 | + } |
| 115 | + |
| 116 | + if (filteredEntries.length === 0) { |
| 117 | + this.log(t('commands.webdav.ls.empty', 'No files or directories found.')); |
| 118 | + return result; |
| 119 | + } |
| 120 | + |
| 121 | + createTable(COLUMNS).render(filteredEntries, DEFAULT_COLUMNS); |
| 122 | + |
| 123 | + return result; |
| 124 | + } |
| 125 | +} |
0 commit comments