UNPKG

photo-watermark-cli

Version:

A modern TypeScript CLI tool to add timestamp watermarks to photos with intelligent size scaling

125 lines 4.61 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.DirectorySelector = void 0; exports.selectDirectory = selectDirectory; const inquirer_1 = __importDefault(require("inquirer")); const fs_1 = require("fs"); const path_1 = require("path"); const os_1 = require("os"); const chalk_1 = __importDefault(require("chalk")); class DirectorySelector { currentPath; constructor(initialPath) { this.currentPath = initialPath || process.cwd(); } /** * 获取当前目录下的所有文件夹和返回上级目录的选项 */ getDirectoryItems() { const items = []; try { // 添加返回上级目录选项(除非已经在根目录) const parentPath = (0, path_1.dirname)(this.currentPath); if (parentPath !== this.currentPath) { items.push({ name: '📁 .. (返回上级目录)', path: parentPath, isDirectory: true }); } // 添加快捷目录选项 if (this.currentPath !== (0, os_1.homedir)()) { items.push({ name: '🏠 用户主目录', path: (0, os_1.homedir)(), isDirectory: true }); } if (this.currentPath !== process.cwd()) { items.push({ name: '📂 当前工作目录', path: process.cwd(), isDirectory: true }); } // 添加当前目录下的所有文件夹 const entries = (0, fs_1.readdirSync)(this.currentPath); const directories = entries .filter(entry => { try { const fullPath = (0, path_1.join)(this.currentPath, entry); return (0, fs_1.statSync)(fullPath).isDirectory() && !entry.startsWith('.'); } catch { return false; } }) .sort() .map(entry => ({ name: `📁 ${entry}`, path: (0, path_1.join)(this.currentPath, entry), isDirectory: true })); items.push(...directories); // 添加选择当前目录的选项 items.push({ name: `✅ 选择当前目录: ${this.currentPath}`, path: this.currentPath, isDirectory: false }); } catch (error) { console.error(chalk_1.default.red('读取目录失败:', error.message)); } return items; } /** * 显示目录选择器 */ async selectDirectory() { while (true) { console.clear(); console.log(chalk_1.default.blue('📁 文件夹选择器')); console.log(chalk_1.default.gray('当前位置:'), chalk_1.default.cyan(this.currentPath)); console.log(chalk_1.default.gray('使用方向键选择,Enter确认\n')); const items = this.getDirectoryItems(); if (items.length === 0) { console.log(chalk_1.default.yellow('当前目录没有可访问的子文件夹')); return this.currentPath; } const choices = items.map(item => ({ name: item.name, value: item })); const { selectedItem } = await inquirer_1.default.prompt([ { type: 'list', name: 'selectedItem', message: '请选择一个选项:', choices, pageSize: Math.min(15, choices.length) } ]); // 如果选择的是目录,继续浏览 if (selectedItem.isDirectory) { this.currentPath = (0, path_1.resolve)(selectedItem.path); } else { // 选择当前目录,返回结果 return this.currentPath; } } } } exports.DirectorySelector = DirectorySelector; /** * 便捷函数:显示文件夹选择器 */ async function selectDirectory(initialPath) { const selector = new DirectorySelector(initialPath); return await selector.selectDirectory(); } //# sourceMappingURL=directory-selector.js.map