UNPKG

folder-organizer

Version:

Organize files in a folder by type

119 lines (115 loc) 4.42 kB
#!/usr/bin/env node "use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); // src/index.ts var import_commander = require("commander"); // src/organizer.ts var import_fs = __toESM(require("fs")); var import_path = __toESM(require("path")); var import_chalk = __toESM(require("chalk")); var import_minimist = __toESM(require("minimist")); var useEmoji = process.stdout.isTTY && !process.env.NO_COLOR; function log(message, emoji) { const symbol = useEmoji && emoji ? `${emoji} ` : ""; console.log(`${symbol}${message}`); } var fileTypeMap = { jpg: "images", png: "images", gif: "images", mp4: "videos", mp3: "audios", txt: "documents", pdf: "documents", docx: "documents", xlsx: "documents", pptx: "documents", zip: "archives", rar: "archives" }; function organizeFolder(folderPath2, options = {}) { const { verbose: verbose2 = false, dryRun: dryRun2 = false } = options; if (!import_fs.default.existsSync(folderPath2)) { log(import_chalk.default.red("Folder not found."), "\u274C"); return; } const files = import_fs.default.readdirSync(folderPath2); for (const file of files) { const fullPath = import_path.default.join(folderPath2, file); if (import_fs.default.statSync(fullPath).isDirectory()) continue; const ext = import_path.default.extname(file).slice(1).toLowerCase(); const targetFolder = fileTypeMap[ext] || "others"; const targetPath = import_path.default.join(folderPath2, targetFolder); const destination = import_path.default.join(targetPath, file); if (verbose2) { log( import_chalk.default.gray(`Found file: ${file} (ext: .${ext}) \u2192 ${targetFolder}/`), "\u{1F50D}" ); } if (!import_fs.default.existsSync(targetPath)) { if (!dryRun2) { import_fs.default.mkdirSync(targetPath); } if (verbose2) { log(import_chalk.default.cyan(`Creating folder: ${targetFolder}/`), "\u{1F4C1}"); } } if (!dryRun2) { import_fs.default.renameSync(fullPath, destination); } log( import_chalk.default.green( `${dryRun2 ? "[Dry Run] " : ""}Moved ${file} \u2192 ${targetFolder}/` ), dryRun2 ? "\u{1F9EA}" : "\u2705" ); } log( import_chalk.default.blue( dryRun2 ? "Dry run completed. No files were moved." : "\u{1F389} Folder organized successfully!" ), "\u{1F4E6}" ); } var args = (0, import_minimist.default)(process.argv.slice(2)); var folderPath = args._[0]; var verbose = args.verbose || false; var dryRun = args["dry-run"] || false; if (!folderPath) { console.log(import_chalk.default.yellow("\u26A0\uFE0F Please provide a folder path.")); } else { organizeFolder(import_path.default.resolve(folderPath), { verbose, dryRun }); } // src/index.ts var program = new import_commander.Command(); program.name("folder-organizer").description("Organizes files in a folder by type").version("1.0.0").argument("<folderPath>", "Path to the folder you want to organize").option("--verbose", "Enable detailed logs").option("--dry-run", "Preview actions without moving files").action((folderPath2, options) => { organizeFolder(folderPath2, { verbose: options.verbose || false, dryRun: options.dryRun || false }); }); if (require.main === module) { program.parse(); }