型情報を追加
This commit is contained in:
parent
88f4a15138
commit
61fd14aa66
5 changed files with 39 additions and 26 deletions
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"type": "commonjs",
|
"type": "commonjs",
|
||||||
"name": "@riq0h/mtt",
|
"name": "@riq0h/mtt",
|
||||||
"version": "0.0.9",
|
"version": "0.0.10",
|
||||||
"description": "mtt - multitool for file operation",
|
"description": "mtt - multitool for file operation",
|
||||||
"main": "dist/lib.js",
|
"main": "dist/lib.js",
|
||||||
"types": "dist/lib.d.ts",
|
"types": "dist/lib.d.ts",
|
||||||
|
|
|
@ -12,18 +12,22 @@ export async function compressFiles(targets?: string[]): Promise<void> {
|
||||||
const currentDir = process.cwd();
|
const currentDir = process.cwd();
|
||||||
let isCompressed = false;
|
let isCompressed = false;
|
||||||
|
|
||||||
const itemsToCompress =
|
const itemsToCompress: string[] =
|
||||||
!targets || targets.length === 0
|
!targets || targets.length === 0
|
||||||
? fs
|
? fs
|
||||||
.readdirSync(currentDir)
|
.readdirSync(currentDir)
|
||||||
.filter((item) => !item.startsWith(".") && !item.endsWith(".zip"))
|
.filter(
|
||||||
|
(item: string) => !item.startsWith(".") && !item.endsWith(".zip"),
|
||||||
|
)
|
||||||
: targets;
|
: targets;
|
||||||
|
|
||||||
const operations: CompressOperation[] = itemsToCompress.map((item) => ({
|
const operations: CompressOperation[] = itemsToCompress.map(
|
||||||
itemPath: item,
|
(item: string) => ({
|
||||||
zipPath: `${item}.zip`,
|
itemPath: item,
|
||||||
isFile: fs.existsSync(item) ? fs.statSync(item).isFile() : false,
|
zipPath: `${item}.zip`,
|
||||||
}));
|
isFile: fs.existsSync(item) ? fs.statSync(item).isFile() : false,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
for (const op of operations) {
|
for (const op of operations) {
|
||||||
if (fs.existsSync(op.zipPath)) {
|
if (fs.existsSync(op.zipPath)) {
|
||||||
|
|
|
@ -11,12 +11,12 @@ export interface RenameOperation {
|
||||||
|
|
||||||
export async function renameFiles(): Promise<void> {
|
export async function renameFiles(): Promise<void> {
|
||||||
try {
|
try {
|
||||||
const files = fs
|
const files: string[] = fs
|
||||||
.readdirSync(process.cwd())
|
.readdirSync(process.cwd())
|
||||||
.filter((file) => !file.startsWith("."));
|
.filter((file: string) => !file.startsWith("."));
|
||||||
const fileContent = files.join("\n");
|
const fileContent: string = files.join("\n");
|
||||||
const editedContent = await openEditor(fileContent);
|
const editedContent: string = await openEditor(fileContent);
|
||||||
const newNames = editedContent.trim().split("\n");
|
const newNames: string[] = editedContent.trim().split("\n");
|
||||||
|
|
||||||
if (newNames.length !== files.length) {
|
if (newNames.length !== files.length) {
|
||||||
throw new Error(
|
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) {
|
if (uniqueNames.size !== newNames.length) {
|
||||||
throw new Error("file names are duplicated. renaming was not performed.");
|
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(
|
throw new Error(
|
||||||
"file names cannot contain whitespace characters. renaming was not performed.",
|
"file names cannot contain whitespace characters. renaming was not performed.",
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
newNames.forEach((name) => {
|
newNames.forEach((name: string) => {
|
||||||
if (name.includes("/") || name.includes("\\")) {
|
if (name.includes("/") || name.includes("\\")) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
"file names cannot contain paths. renaming was not performed.",
|
"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) => ({
|
const operations: RenameOperation[] = files.map(
|
||||||
oldName,
|
(oldName: string, index: number) => ({
|
||||||
newName: newNames[index],
|
oldName,
|
||||||
path: path.join(process.cwd(), oldName),
|
newName: newNames[index],
|
||||||
newPath: path.join(process.cwd(), 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.");
|
console.log("no changes were made. renaming was not performed.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,7 +21,7 @@ export async function resizeImages(
|
||||||
}
|
}
|
||||||
|
|
||||||
const imageExtensions = [".jpg", ".jpeg", ".png", ".gif", ".webp"];
|
const imageExtensions = [".jpg", ".jpeg", ".png", ".gif", ".webp"];
|
||||||
const validFiles = files.filter((file) =>
|
const validFiles = files.filter((file: string) =>
|
||||||
imageExtensions.includes(path.extname(file).toLowerCase()),
|
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 { version, description } from "../package.json";
|
||||||
import { renameFiles, compressFiles, resizeImages } from "./lib";
|
import { renameFiles, compressFiles, resizeImages } from "./lib";
|
||||||
|
|
||||||
|
interface Options {
|
||||||
|
rename?: boolean;
|
||||||
|
compress?: boolean | string[];
|
||||||
|
image?: string[];
|
||||||
|
percentage?: number;
|
||||||
|
}
|
||||||
|
|
||||||
const program = new Command();
|
const program = new Command();
|
||||||
|
|
||||||
program.version(version).description(description);
|
program.version(version).description(description);
|
||||||
|
@ -18,7 +25,7 @@ program
|
||||||
"resize image files. File name and -p option are required",
|
"resize image files. File name and -p option are required",
|
||||||
)
|
)
|
||||||
.option("-p, --percentage <value>", "specify the resize percentage")
|
.option("-p, --percentage <value>", "specify the resize percentage")
|
||||||
.action((options) => {
|
.action((options: Options) => {
|
||||||
try {
|
try {
|
||||||
if (options.rename) {
|
if (options.rename) {
|
||||||
renameFiles();
|
renameFiles();
|
||||||
|
@ -30,7 +37,7 @@ program
|
||||||
"resize percentage must be specified with the -p option for image resizing.",
|
"resize percentage must be specified with the -p option for image resizing.",
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
resizeImages(options.image, Number(options.percentage));
|
resizeImages(options.image, options.percentage);
|
||||||
} else {
|
} else {
|
||||||
program.help();
|
program.help();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue