UNPKG

tidyai-ts

Version:

AI-powered cross-platform file organizer using OpenRouter API

146 lines (145 loc) 6.01 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.main = main; const organizer_1 = require("./organizer"); const cli_1 = require("./cli"); const cli_utils_1 = require("./cli-utils"); const readline = __importStar(require("readline")); const dotenv = __importStar(require("dotenv")); const path = __importStar(require("path")); // Load environment variables from .env file dotenv.config({ path: path.resolve(process.cwd(), '.env') }); const OPENROUTER_API_KEY = process.env.TIDYAI_API_KEY; function showHelp() { console.log('Usage:'); console.log(' tidyai [options] [folder_path]'); console.log(' tidyai --help'); console.log(' tidyai --version'); console.log(''); console.log('Options:'); console.log(' --undo, -u Undo the organization of a folder'); console.log(' --delete, -d Delete unnecessary files after organization'); console.log(' --help, -h Show this help message'); console.log(' --version, -v Show version information'); console.log(''); console.log('Examples:'); console.log(' tidyai /path/to/folder # Organize files in a folder'); console.log(' tidyai --delete /path/to/folder # Organize and delete unnecessary files'); console.log(' tidyai --undo /path/to/folder # Undo organization'); console.log(' tidyai --help # Show this help message'); console.log(''); console.log('Notes:'); console.log(' - Unnecessary files include: thumbs.db, .DS_Store, *.tmp, *.log, desktop.ini'); console.log(' - Deletion requires confirmation and cannot be undone (files are permanently deleted)'); console.log(' - Organization history is used for undo operations'); console.log(''); console.log('Environment Variables:'); console.log(' TIDYAI_API_KEY OpenRouter API key for AI-powered suggestions'); console.log(''); console.log('For more information, visit: https://github.com/pawan67/tidyai-ts'); } function showVersion() { console.log('TidyAI-TS v1.1.0'); } function promptUser(message) { const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); return new Promise((resolve) => { rl.question(`${message} (y/N): `, (answer) => { rl.close(); resolve(answer.toLowerCase() === 'y' || answer.toLowerCase() === 'yes'); }); }); } async function checkApiKey() { if (!OPENROUTER_API_KEY) { (0, cli_utils_1.displayWarning)('TIDYAI_API_KEY environment variable is not set.'); (0, cli_utils_1.displayInfo)('Without an API key, TidyAI will use default folder suggestions.'); (0, cli_utils_1.displayInfo)('For better organization, consider getting a free API key from OpenRouter.ai'); const continueWithoutApiKey = await promptUser('Do you want to continue with default suggestions?'); return continueWithoutApiKey; } return true; } async function main() { // Display the TidyAI header (0, cli_utils_1.displayHeader)(); // If no arguments are provided, show help if (process.argv.length <= 2) { showHelp(); return; } const args = process.argv.slice(2); // Check for help flag if (args.includes('--help') || args.includes('-h')) { showHelp(); return; } // Check for version flag if (args.includes('--version') || args.includes('-v')) { showVersion(); return; } try { const parsedArgs = (0, cli_1.parseArguments)(args); // Check API key for non-undo operations if (!parsedArgs.undo) { const shouldContinue = await checkApiKey(); if (!shouldContinue) { (0, cli_utils_1.displayInfo)('Operation cancelled by user.'); return; } } if (parsedArgs.undo) { await (0, organizer_1.undoOrganize)(parsedArgs.path); (0, cli_utils_1.displaySuccess)(`Successfully undid organization of folder: ${parsedArgs.path}`); } else if (parsedArgs.delete) { await (0, organizer_1.deleteUnnecessaryFiles)(parsedArgs.path); (0, cli_utils_1.displaySuccess)(`Successfully organized and deleted unnecessary files in folder: ${parsedArgs.path}`); } else { await (0, organizer_1.organizeFolder)(parsedArgs.path); (0, cli_utils_1.displaySuccess)(`Successfully organized folder: ${parsedArgs.path}`); } } catch (error) { (0, cli_utils_1.displayError)(`Error: ${error.message}`); process.exit(1); } }