UNPKG

s3-cli-js

Version:

A TypeScript-based npm package that replaces AWS CLI for S3 operations using presigned URLs

131 lines 3.9 kB
"use strict"; /** * Progress utilities for file operations */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createProgressBar = createProgressBar; exports.createProgressCallback = createProgressCallback; exports.formatBytes = formatBytes; exports.formatDate = formatDate; exports.parseS3Uri = parseS3Uri; exports.isS3Uri = isS3Uri; exports.joinS3Path = joinS3Path; exports.getFileExtension = getFileExtension; exports.matchesPattern = matchesPattern; exports.shouldIncludeFile = shouldIncludeFile; const ProgressBar = require('progress'); const chalk_1 = __importDefault(require("chalk")); /** * Create a progress bar for file operations */ function createProgressBar(total, description) { return new ProgressBar(`${chalk_1.default.blue(description)} [:bar] :percent :etas (:current/:total bytes)`, { complete: '█', incomplete: '░', width: 40, total, }); } /** * Create a progress callback that updates a progress bar */ function createProgressCallback(progressBar) { let lastLoaded = 0; return (progress) => { const increment = progress.loaded - lastLoaded; progressBar.tick(increment); lastLoaded = progress.loaded; }; } /** * Format bytes to human readable format */ function formatBytes(bytes) { if (bytes === 0) return '0 B'; const k = 1024; const sizes = ['B', 'KB', 'MB', 'GB', 'TB']; const i = Math.floor(Math.log(bytes) / Math.log(k)); return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]; } /** * Format date to human readable format */ function formatDate(date) { return date.toISOString().replace('T', ' ').replace(/\.\d{3}Z$/, ' UTC'); } /** * Parse S3 URI (s3://bucket/key) */ function parseS3Uri(uri) { if (!uri.startsWith('s3://')) { throw new Error(`Invalid S3 URI: ${uri}. Must start with s3://`); } const withoutProtocol = uri.slice(5); // Remove 's3://' const slashIndex = withoutProtocol.indexOf('/'); if (slashIndex === -1) { return { bucket: withoutProtocol, key: '' }; } return { bucket: withoutProtocol.slice(0, slashIndex), key: withoutProtocol.slice(slashIndex + 1), }; } /** * Check if path is S3 URI */ function isS3Uri(path) { return path.startsWith('s3://'); } /** * Join S3 paths */ function joinS3Path(...parts) { return parts .filter(part => part.length > 0) .join('/') .replace(/\/+/g, '/'); } /** * Get file extension */ function getFileExtension(filename) { const lastDotIndex = filename.lastIndexOf('.'); return lastDotIndex === -1 ? '' : filename.slice(lastDotIndex + 1); } /** * Check if pattern matches string (simple glob pattern matching) */ function matchesPattern(str, pattern) { // Convert glob pattern to regex const regexPattern = pattern .replace(/\./g, '\\.') .replace(/\*/g, '.*') .replace(/\?/g, '.'); const regex = new RegExp(`^${regexPattern}$`); return regex.test(str); } /** * Filter files based on include/exclude patterns */ function shouldIncludeFile(filename, include, exclude) { // If include patterns are specified, file must match at least one if (include && include.length > 0) { const matchesInclude = include.some(pattern => matchesPattern(filename, pattern)); if (!matchesInclude) { return false; } } // If exclude patterns are specified, file must not match any if (exclude && exclude.length > 0) { const matchesExclude = exclude.some(pattern => matchesPattern(filename, pattern)); if (matchesExclude) { return false; } } return true; } //# sourceMappingURL=progress.js.map