Skip to content

Commit

Permalink
CLI application for minomax
Browse files Browse the repository at this point in the history
  • Loading branch information
darsan-in committed Jul 18, 2024
1 parent fcbc94e commit 158e6ad
Show file tree
Hide file tree
Showing 4 changed files with 302 additions and 4 deletions.
186 changes: 186 additions & 0 deletions bin/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
#!/usr/bin/env node

import { Command } from "commander";
import configurations from "../configLoader";
import {
compressImages,
compressVideos,
generateImageSets,
minifyWebdoc,
minomax,
} from "../minomax";

const program = new Command();

// Minomax command
program
.command("make")
.description("Run the minomax process")
.option(
"-f, --format [jpg | avif | webp]",
"Image output format",
"webp",
)
.option(
"-c, --codec [wav1 | mav1 | mx265]",
"Output video codec type",
"wav1",
)
.option("-e, --encode [1 | 2 | 3]", "Video encoding level", parseInt, 3)
.option(
"-d, --dest <path>",
"Destination base path",
configurations.destPath,
)
.option(
"-i, --ignore <patterns>",
"Ignore path patterns",
(value) => value.split(","),
[],
)
.action(async (options) => {
await minomax(
{ targetFormat: options.format },
{ codecType: options.codec, encodeLevel: options.encode },
options.dest,
options.ignore,
);
});

// Compress Images command
program
.command("image")
.description("Compress images")
.option(
"-p, --patterns <patterns>",
"Path patterns",
(value) => value.split(","),
[],
)
.option(
"-f, --format [jpg | avif | webp]",
"Image output format",
"webp",
)
.option(
"-d, --dest <path>",
"Destination base path",
configurations.destPath,
)
.option(
"-i, --ignore <patterns>",
"Ignore patterns",
(value) => value.split(","),
[],
)
.action(async (options) => {
await compressImages(
options.patterns,
options.format,
options.dest,
options.ignore,
);
});

// Compress Videos command
program
.command("video")
.description("Compress videos")
.option(
"-p, --patterns <patterns>",
"Path patterns",
(value) => value.split(","),
[],
)
.option(
"-c, --codec [wav1 | mav1 | mx265]",
"Output video codec type",
"wav1",
)
.option("-e, --encode [1 | 2 | 3]", "Video encoding level", parseInt, 3)
.option(
"-d, --dest <path>",
"Destination base path",
configurations.destPath,
)
.option(
"-I, --ignore <patterns>",
"Ignore patterns",
(value) => value.split(","),
[],
)
.action(async (options) => {
await compressVideos(
options.patterns,
options.codec,
options.encode,
options.dest,
options.ignore,
);
});

// Minify Webdoc command
program
.command("minify")
.description("Minify web documents - JS , CSS & HTML")
.option(
"-p, --patterns <patterns>",
"Path patterns",
(value) => value.split(","),
[],
)
.option(
"-d, --dest <path>",
"Destination base path",
configurations.destPath,
)
.option(
"-s, --searchBase <path>",
"File search base path",
process.cwd(),
)
.option(
"-i, --ignore <patterns>",
"Ignore patterns",
(value) => value.split(","),
[],
)
.action(async (options) => {
await minifyWebdoc(
options.patterns,
options.dest,
options.searchBase,
options.ignore,
);
});

// Generate Image Sets command
program
.command("genset")
.description("Generate image sets")
.option(
"-p, --patterns <patterns>",
"Path patterns",
(value) => value.split(","),
[],
)
.option(
"-d, --dest <path>",
"Destination base path",
configurations.destPath,
)
.option(
"-i, --ignore <patterns>",
"Ignore patterns",
(value) => value.split(","),
[],
)
.action(async (options) => {
await generateImageSets(
options.patterns,
options.dest,
options.ignore,
);
});

program.parse(process.argv);
107 changes: 107 additions & 0 deletions minomax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import configurations from "./configLoader";
import imageWorker from "./lib/image";
import imageGenerator from "./lib/imageset/imageset";
import {
ImageWorkerOutputTypes,
ImageWorkerParamsMain,
VideoWorkerParamsMain,
} from "./lib/options";
Expand Down Expand Up @@ -129,3 +130,109 @@ export async function minomax(
);
/* ------- */
}

export async function compressImages(
pathPatterns: string[],
targetFormat: ImageWorkerOutputTypes,
destinationBasePath: string = configurations.destPath,
ignorePatterns: string[] = [],
) {
ignorePatterns = [
...ignorePatterns,
"node_modules/**",
`${destinationBasePath}/**`,
];

const imagesFiles: string[] = globSync(pathPatterns, {
ignore: ignorePatterns,
absolute: true,
});

try {
await imageWorker(imagesFiles, targetFormat, destinationBasePath);
} catch (err) {
console.log(err);
process.exit(1);
}
}

export async function compressVideos(
pathPatterns: string[],
codecType: "wav1" | "mav1" | "mx265",
encodeLevel: 1 | 2 | 3 = 3,
destinationBasePath: string = configurations.destPath,
ignorePatterns: string[] = [],
) {
ignorePatterns = [
...ignorePatterns,
"node_modules/**",
`${destinationBasePath}/**`,
];

const videoFiles: string[] = globSync(pathPatterns, {
ignore: ignorePatterns,
absolute: true,
});

try {
const thumbnailSeekPercent: number = 20;
await videoWorker(
videoFiles,
codecType,
encodeLevel,
thumbnailSeekPercent,
destinationBasePath,
);
} catch (err) {
console.log(err);
process.exit(1);
}
}

export async function minifyWebdoc(
pathPatterns: string[],
destinationBasePath: string = configurations.destPath,
fileSearchBasePath: string = process.cwd(),
ignorePatterns: string[] = [],
) {
ignorePatterns = [
...ignorePatterns,
"node_modules/**",
`${destinationBasePath}/**`,
];

try {
await webDocWorker(
pathPatterns,
destinationBasePath,
fileSearchBasePath,
ignorePatterns,
);
} catch (err) {
console.log(err);
process.exit(1);
}
}

export async function generateImageSets(
pathPatterns: string[],
destinationBasePath: string = configurations.destPath,
ignorePatterns: string[] = [],
) {
ignorePatterns = [
...ignorePatterns,
"node_modules/**",
`${destinationBasePath}/**`,
];

try {
await imageGenerator(
pathPatterns,
destinationBasePath,
ignorePatterns,
);
} catch (err) {
console.log(err);
process.exit(1);
}
}
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@cresteem/minomax",
"displayName": "Minomax",
"version": "0.0.1a",
"version": "1.0.0",
"description": "Minomax is a powerful Node.js package designed to efficiently compress a wide range of web resources, including videos, images, HTML, CSS, JavaScript, and SVG files. Enhance your website's performance and reduce load times with Minomax's robust compression capabilities.",
"main": "./dist/minomax.js",
"types": "./dist/types/minomax.d.ts",
Expand All @@ -15,7 +15,7 @@
],
"preferGlobal": true,
"bin": {
"mm": "./dist/bin/cli.js"
"minomax": "./dist/bin/cli.js"
},
"repository": {
"url": "https://github.com/cresteem/minomax"
Expand Down Expand Up @@ -68,6 +68,7 @@
"@ffmpeg-installer/ffmpeg": "latest",
"@ffprobe-installer/ffprobe": "latest",
"cheerio": "1.0.0-rc.12",
"commander": "12.1.0",
"css": "3.0.0",
"cssnano": "^7.0.4",
"cssnano-preset-advanced": "^7.0.4",
Expand All @@ -81,8 +82,7 @@
"remige": "^0.1.1",
"sharp": "^0.33.4",
"svgo": "3.3.2",
"terser": "^5.31.2",
"yargs": "17.7.2"
"terser": "^5.31.2"
},
"devDependencies": {
"@babel/core": "7.24.8",
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2229,6 +2229,11 @@ colord@^2.9.3:
resolved "https://registry.yarnpkg.com/colord/-/colord-2.9.3.tgz#4f8ce919de456f1d5c1c368c307fe20f3e59fb43"
integrity sha512-jeC1axXpnb0/2nn/Y1LPuLdgXBLH7aDcHu4KEKfqw3CUhX7ZpfBSlPKyqXE6btIgEzfWtrX3/tyBCaCvXvMkOw==

[email protected]:
version "12.1.0"
resolved "https://registry.yarnpkg.com/commander/-/commander-12.1.0.tgz#01423b36f501259fdaac4d0e4d60c96c991585d3"
integrity sha512-Vw8qHK3bZM9y/P10u3Vib8o/DdkvA2OtPtZvD871QKjy74Wj1WSKFILMPRPSdUSx5RFK1arlJzEtA4PkFgnbuA==

commander@^2.19.0, commander@^2.20.0, commander@^2.8.1:
version "2.20.3"
resolved "https://registry.yarnpkg.com/commander/-/commander-2.20.3.tgz#fd485e84c03eb4881c20722ba48035e8531aeb33"
Expand Down

0 comments on commit 158e6ad

Please sign in to comment.