Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cli/size options #11

Merged
merged 5 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions babel.config.js

This file was deleted.

11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@
"type": "git",
"url": "git+https://github.com/wp-blocks/squashify.git"
},
"module": "lib/esm/",
"main": "lib/esm/index.js",
"main": "lib/esm/index.js",
"module": "lib/esm/",
"reqire": "lib/cjs/index.js",
"types": "lib/esm/index.d.ts",
"types": "lib/@types/index.d.ts",
"scripts": {
"build": "npx esbuild ./src/* --outdir=lib/esm/ --format=esm --platform=node --minify",
"postbuild": "npx esbuild src/index.ts --outfile=lib/cjs/index.js --bundle --platform=node --minify",
"watch": "tsc --watch --sourceMap",
"postbuild": "npx esbuild src/index.ts --outfile=lib/cjs/index.js --bundle --platform=node --minify && tsc --emitDeclarationOnly --outDir lib/@types/",
"watch": "tsc --watch --sourceMap --outDir lib/esm/",
"squashify": "node . --in tests/images/ --out tests/output --verbose",
"squashifyDefaults": "node . -d",
"test": "vitest --coverage"
},
"files": [
Expand Down
43 changes: 37 additions & 6 deletions src/args.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { defaultConfigFile, extModes } from "./constants.js";
import yargs from "yargs";
import { CliOptions, ExtMode } from "./types.js";
import yargs, { Argv } from "yargs";
import { CliOptions, ExtMode, ResizeType, resizeType } from "./types.js";
import { hideBin } from "yargs/helpers";
import { generateDefaultConfigFile } from "./utils.js";

/**
* Get the command-line settings
*/
export function getCliOptions(
rawArgs: yargs.Argv<object> | undefined,
): CliOptions {
export function getCliOptions(rawArgs: Argv<object> | undefined): CliOptions {
if (!rawArgs) {
rawArgs = yargs(hideBin(process.argv));
}
Expand All @@ -35,6 +34,24 @@ export function getCliOptions(
describe: "Interactive mode",
type: "boolean",
})
.option("defaultIni", {
alias: "d",
describe: "Generate default config file",
type: "boolean",
})
.option("maxSize", {
alias: "s",
describe: "Maximum image size in pixels",
type: "number",
default: undefined,
})
.option("resizeType", {
alias: "r",
describe: "Resize type",
type: "string",
choices: resizeType,
default: undefined,
})
.option("extMode", {
alias: "e",
describe: "Whenever to add or replace extension",
Expand All @@ -60,15 +77,29 @@ export function getCliOptions(
? (argv.extMode as ExtMode)
: "replace";

const configFileName = argv.config ?? defaultConfigFile;

if (Boolean(argv.defaultIni) && configFileName) {
generateDefaultConfigFile(configFileName, argv as Record<string, string>);

console.log(`Generated default config file: ${configFileName}`);

process.exit(0);
}

return {
srcDir: argv.input ?? "",
distDir: argv.output ?? "",
configFile: argv.config ?? defaultConfigFile,
configFile: configFileName,
interactive: Boolean(argv.interactive),
verbose: Boolean(argv.verbose),
options: extMode
? {
extMode: (extMode as ExtMode) || "replace",
maxSize: argv.maxSize ? Number(argv.maxSize) : undefined,
resizeType: argv.resizeType
? (argv.resizeType as ResizeType)
: undefined,
}
: undefined,
};
Expand Down
2 changes: 1 addition & 1 deletion src/encodeImage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CompressionMeta, OutputData } from "./types";
import { CompressionMeta, OutputData } from "./types.js";
import sharp, { FitEnum } from "sharp";
import { transparentColor } from "./constants.js";

Expand Down
2 changes: 1 addition & 1 deletion src/prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
getImageCompressionOptions,
srcDirQuestion,
} from "./options.js";
import { CompressionOptionsMap, type ScriptOptions } from "./types";
import { CompressionOptionsMap, type ScriptOptions } from "./types.js";
import {
defaultCompressionOptions,
getImageFormatsInFolder,
Expand Down
39 changes: 39 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ import {
type CompressionOptionsMap,
ExtMode,
GenericCompressionOptions,
ResizeType,
} from "./types.js";
import ini from "ini";
import { writeFileSync } from "node:fs";

/**
* The function returns compression settings for a given image format.
Expand Down Expand Up @@ -295,3 +298,39 @@ export function getFileName(
? paths?.base + ext
: paths?.name + ext;
}

/**
* Generates a default configuration file based on the provided arguments.
*
* @param filename - The name of the configuration file.
* @param {Record<string, string>} argv - The input arguments for configuration.
*/
export function generateDefaultConfigFile(
filename: string,
argv: Record<string, string>,
) {
let defaultConfig = {
path: {
in: argv.input ?? "images",
out: argv.output ?? "optimized",
},
options: {
verbose: argv.verbose ?? undefined,
extMode: (argv.extMode as ExtMode) || "replace",
maxSize: Number(argv.maxSize) || undefined,
resizeType: (argv.resizeType as ResizeType) || undefined,
},
};

defaultConfig = {
...defaultConfig,
options: {
...defaultConfig.options,
},
...defaultCompressionOptions(),
};

const iniFileContent = ini.stringify(defaultConfig);

return writeFileSync(filename, iniFileContent);
}
16 changes: 0 additions & 16 deletions tests/tsconfig.test.json

This file was deleted.

4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"module": "ESNext",
"moduleResolution": "node",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"target": "ESNext",
"esModuleInterop": true,
"strict": true,
Expand Down