diff --git a/package.json b/package.json index 669a438..fb19d56 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "type": "commonjs", "name": "@riq0h/mtt", - "version": "0.0.9", + "version": "0.0.10", "description": "mtt - multitool for file operation", "main": "dist/lib.js", "types": "dist/lib.d.ts", diff --git a/src/commands/compress.ts b/src/commands/compress.ts index c63c38b..e295e78 100644 --- a/src/commands/compress.ts +++ b/src/commands/compress.ts @@ -12,18 +12,22 @@ export async function compressFiles(targets?: string[]): Promise<void> { const currentDir = process.cwd(); let isCompressed = false; - const itemsToCompress = + const itemsToCompress: string[] = !targets || targets.length === 0 ? fs .readdirSync(currentDir) - .filter((item) => !item.startsWith(".") && !item.endsWith(".zip")) + .filter( + (item: string) => !item.startsWith(".") && !item.endsWith(".zip"), + ) : targets; - const operations: CompressOperation[] = itemsToCompress.map((item) => ({ - itemPath: item, - zipPath: `${item}.zip`, - isFile: fs.existsSync(item) ? fs.statSync(item).isFile() : false, - })); + const operations: CompressOperation[] = itemsToCompress.map( + (item: string) => ({ + itemPath: item, + zipPath: `${item}.zip`, + isFile: fs.existsSync(item) ? fs.statSync(item).isFile() : false, + }), + ); for (const op of operations) { if (fs.existsSync(op.zipPath)) { diff --git a/src/commands/rename.ts b/src/commands/rename.ts index 9897bdf..f3d9bd9 100644 --- a/src/commands/rename.ts +++ b/src/commands/rename.ts @@ -11,12 +11,12 @@ export interface RenameOperation { export async function renameFiles(): Promise<void> { try { - const files = fs + const files: string[] = fs .readdirSync(process.cwd()) - .filter((file) => !file.startsWith(".")); - const fileContent = files.join("\n"); - const editedContent = await openEditor(fileContent); - const newNames = editedContent.trim().split("\n"); + .filter((file: string) => !file.startsWith(".")); + const fileContent: string = files.join("\n"); + const editedContent: string = await openEditor(fileContent); + const newNames: string[] = editedContent.trim().split("\n"); if (newNames.length !== files.length) { throw new Error( @@ -24,18 +24,18 @@ export async function renameFiles(): Promise<void> { ); } - const uniqueNames = new Set(newNames); + const uniqueNames: Set<string> = new Set(newNames); if (uniqueNames.size !== newNames.length) { throw new Error("file names are duplicated. renaming was not performed."); } - if (newNames.some((name) => /[\s ]/.test(name))) { + if (newNames.some((name: string) => /[\s ]/.test(name))) { throw new Error( "file names cannot contain whitespace characters. renaming was not performed.", ); } - newNames.forEach((name) => { + newNames.forEach((name: string) => { if (name.includes("/") || name.includes("\\")) { throw new Error( "file names cannot contain paths. renaming was not performed.", @@ -43,14 +43,16 @@ export async function renameFiles(): Promise<void> { } }); - const operations = files.map((oldName, index) => ({ - oldName, - newName: newNames[index], - path: path.join(process.cwd(), oldName), - newPath: path.join(process.cwd(), newNames[index]), - })); + const operations: RenameOperation[] = files.map( + (oldName: string, index: number) => ({ + oldName, + newName: newNames[index], + path: path.join(process.cwd(), oldName), + newPath: path.join(process.cwd(), newNames[index]), + }), + ); - if (!operations.some((op) => op.oldName !== op.newName)) { + if (!operations.some((op: RenameOperation) => op.oldName !== op.newName)) { console.log("no changes were made. renaming was not performed."); return; } diff --git a/src/commands/resize.ts b/src/commands/resize.ts index 177105c..83c561e 100644 --- a/src/commands/resize.ts +++ b/src/commands/resize.ts @@ -21,7 +21,7 @@ export async function resizeImages( } const imageExtensions = [".jpg", ".jpeg", ".png", ".gif", ".webp"]; - const validFiles = files.filter((file) => + const validFiles = files.filter((file: string) => imageExtensions.includes(path.extname(file).toLowerCase()), ); diff --git a/src/index.ts b/src/index.ts index 3797f44..e49192e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,6 +3,13 @@ import { Command } from "commander"; import { version, description } from "../package.json"; import { renameFiles, compressFiles, resizeImages } from "./lib"; +interface Options { + rename?: boolean; + compress?: boolean | string[]; + image?: string[]; + percentage?: number; +} + const program = new Command(); program.version(version).description(description); @@ -18,7 +25,7 @@ program "resize image files. File name and -p option are required", ) .option("-p, --percentage <value>", "specify the resize percentage") - .action((options) => { + .action((options: Options) => { try { if (options.rename) { renameFiles(); @@ -30,7 +37,7 @@ program "resize percentage must be specified with the -p option for image resizing.", ); } - resizeImages(options.image, Number(options.percentage)); + resizeImages(options.image, options.percentage); } else { program.help(); }