From 84b12de33085d2a14fc55a8fc6d5802e0009a84d Mon Sep 17 00:00:00 2001 From: Rikuoh <mail@riq0h.jp> Date: Mon, 17 Feb 2025 22:53:25 +0900 Subject: [PATCH] main files --- README.md | 19 +++++++++++++++++++ src/index.ts | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.ts | 7 +++++++ 3 files changed, 75 insertions(+) create mode 100644 README.md create mode 100644 src/index.ts create mode 100644 src/lib.ts diff --git a/README.md b/README.md new file mode 100644 index 0000000..ac0aea8 --- /dev/null +++ b/README.md @@ -0,0 +1,19 @@ +# mtt(multitool) + +A multi-tool that performs various operations on files within a directory. Currently, it supports the following features. + +## Usage + +`npm insatll -g @riq0h/mtt` + +## `mtt -r` + +Bulk renames filenames within a directory. When launched, `$EDITOR` is called, allowing you to flexibly edit with any editor. + +## `mtt -c` + +Compresses individual files within a directory. If no filenames are specified, all files will be targeted. + +## `mtt -i <files...> -p <value>` + +Resizes image files (`.jpg`, `.jpeg`, `.png`, `.gif`, `.webp`) within a directory. The `-p` option is required to specify the resize ratio. diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..3797f44 --- /dev/null +++ b/src/index.ts @@ -0,0 +1,49 @@ +#!/usr/bin/env node +import { Command } from "commander"; +import { version, description } from "../package.json"; +import { renameFiles, compressFiles, resizeImages } from "./lib"; + +const program = new Command(); + +program.version(version).description(description); + +program + .option("-r, --rename", "rename files in bulk. $EDITOR will be launched") + .option( + "-c, --compress [targets...]", + "compress files individually. If no file name is specified, all files in the directory will be targeted", + ) + .option( + "-i, --image <files...>", + "resize image files. File name and -p option are required", + ) + .option("-p, --percentage <value>", "specify the resize percentage") + .action((options) => { + try { + if (options.rename) { + renameFiles(); + } else if (options.compress) { + compressFiles(options.compress === true ? undefined : options.compress); + } else if (options.image) { + if (!options.percentage) { + throw new Error( + "resize percentage must be specified with the -p option for image resizing.", + ); + } + resizeImages(options.image, Number(options.percentage)); + } else { + program.help(); + } + } catch (error: unknown) { + if (error instanceof Error) { + console.error("error:", error.message); + process.exit(1); + } + } + }); + +program.parse(process.argv); + +if (!process.argv.slice(2).length) { + program.help(); +} diff --git a/src/lib.ts b/src/lib.ts new file mode 100644 index 0000000..0b9038b --- /dev/null +++ b/src/lib.ts @@ -0,0 +1,7 @@ +export { renameFiles } from "./commands/rename"; +export { compressFiles } from "./commands/compress"; +export { resizeImages } from "./commands/resize"; + +export type { RenameOperation } from "./commands/rename"; +export type { CompressOperation } from "./commands/compress"; +export type { ResizeOperation } from "./commands/resize";