compress function

This commit is contained in:
Rikuoh Tsujitani 2025-02-17 22:58:45 +09:00
parent b07badacb1
commit 71e439f770
Signed by: riq0h
GPG key ID: 010F09DEA298C717
2 changed files with 72 additions and 2 deletions

4
package-lock.json generated
View file

@ -1,12 +1,12 @@
{ {
"name": "@riq0h/mtt", "name": "@riq0h/mtt",
"version": "0.0.5", "version": "0.0.9",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "@riq0h/mtt", "name": "@riq0h/mtt",
"version": "0.0.5", "version": "0.0.9",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"commander": "^13.1.0", "commander": "^13.1.0",

70
src/commands/compress.ts Normal file
View file

@ -0,0 +1,70 @@
import * as fs from "fs";
import { archiveFile, archiveFolder } from "zip-lib";
export interface CompressOperation {
itemPath: string;
zipPath: string;
isFile: boolean;
}
export async function compressFiles(targets?: string[]): Promise<void> {
try {
const currentDir = process.cwd();
let isCompressed = false;
const itemsToCompress =
!targets || targets.length === 0
? fs
.readdirSync(currentDir)
.filter((item) => !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,
}));
for (const op of operations) {
if (fs.existsSync(op.zipPath)) {
throw new Error(
`${op.zipPath} already exists. compression was not performed.`,
);
}
}
for (const op of operations) {
if (!fs.existsSync(op.itemPath)) {
throw new Error(
`${op.itemPath} was not found. compression was not performed.`,
);
}
await compressItem(op);
isCompressed = true;
}
if (isCompressed) {
console.log("compression completed.");
} else {
console.log(
"no files to compress were found. compression was not performed.",
);
}
} catch (error: unknown) {
if (error instanceof Error) {
console.error("error:", error.message);
process.exit(1);
} else {
console.error("unknown error.");
process.exit(1);
}
}
}
async function compressItem(operation: CompressOperation): Promise<void> {
if (operation.isFile) {
await archiveFile(operation.itemPath, operation.zipPath);
} else {
await archiveFolder(operation.itemPath, operation.zipPath);
}
}