型情報を追加
This commit is contained in:
parent
88f4a15138
commit
61fd14aa66
5 changed files with 39 additions and 26 deletions
|
@ -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",
|
||||
|
|
|
@ -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)) {
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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()),
|
||||
);
|
||||
|
||||
|
|
11
src/index.ts
11
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();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue