image-convert-cli 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/index.ts +9 -3
- package/dist/index.js +40 -3
- package/package.json +1 -1
- package/src/cli.ts +31 -0
- package/src/converter.ts +7 -1
- package/src/index.ts +1 -1
- package/src/prompts.ts +7 -1
- package/src/types.ts +1 -1
package/bin/index.ts
CHANGED
|
@@ -1,11 +1,17 @@
|
|
|
1
1
|
#!/usr/bin/env bun
|
|
2
2
|
|
|
3
|
-
import { runCli } from "../src/cli";
|
|
3
|
+
import { runCli, handleUpdate } from "../src/cli";
|
|
4
4
|
import type { ConvertOptions } from "../src/types";
|
|
5
5
|
|
|
6
6
|
const args = process.argv.slice(2);
|
|
7
7
|
const options: ConvertOptions = {};
|
|
8
8
|
|
|
9
|
+
// Check for update command (positional argument)
|
|
10
|
+
if (args[0] === "update") {
|
|
11
|
+
await handleUpdate();
|
|
12
|
+
process.exit(0);
|
|
13
|
+
}
|
|
14
|
+
|
|
9
15
|
// Parse arguments
|
|
10
16
|
for (let i = 0; i < args.length; i++) {
|
|
11
17
|
const arg = args[i];
|
|
@@ -16,8 +22,8 @@ for (let i = 0; i < args.length; i++) {
|
|
|
16
22
|
} else if (arg === "--source" || arg === "-s") {
|
|
17
23
|
options.source = args[++i];
|
|
18
24
|
} else if (arg === "--format" || arg === "-f") {
|
|
19
|
-
const format = args[++i] as "webp" | "jpeg" | "jpg" | undefined;
|
|
20
|
-
if (format && ["webp", "jpeg", "jpg"].includes(format)) {
|
|
25
|
+
const format = args[++i] as "webp" | "jpeg" | "jpg" | "png" | undefined;
|
|
26
|
+
if (format && ["webp", "jpeg", "jpg", "png"].includes(format)) {
|
|
21
27
|
options.format = format;
|
|
22
28
|
}
|
|
23
29
|
} else if (arg === "--dest" || arg === "-d") {
|
package/dist/index.js
CHANGED
|
@@ -8309,6 +8309,10 @@ async function convertImage(sourcePath, destinationPath, format, compress) {
|
|
|
8309
8309
|
sharpFormat = "jpeg";
|
|
8310
8310
|
options = compress ? { quality: 100, mozjpeg: true } : { quality: 100 };
|
|
8311
8311
|
break;
|
|
8312
|
+
case "png":
|
|
8313
|
+
sharpFormat = "png";
|
|
8314
|
+
options = compress ? { compressionLevel: 9 } : { compressionLevel: 6 };
|
|
8315
|
+
break;
|
|
8312
8316
|
default:
|
|
8313
8317
|
throw new Error(`Unsupported format: ${format}`);
|
|
8314
8318
|
}
|
|
@@ -8425,7 +8429,8 @@ class InteractivePromptService {
|
|
|
8425
8429
|
choices: [
|
|
8426
8430
|
{ value: "webp", description: "WebP format (recommended for web)" },
|
|
8427
8431
|
{ value: "jpeg", description: "JPEG format" },
|
|
8428
|
-
{ value: "jpg", description: "JPG format" }
|
|
8432
|
+
{ value: "jpg", description: "JPG format" },
|
|
8433
|
+
{ value: "png", description: "PNG format (lossless)" }
|
|
8429
8434
|
]
|
|
8430
8435
|
});
|
|
8431
8436
|
}
|
|
@@ -8448,20 +8453,25 @@ Usage:
|
|
|
8448
8453
|
imgc [options]
|
|
8449
8454
|
imgc --source <path> --format <format> [options]
|
|
8450
8455
|
imgc -y --source <path> --format <format>
|
|
8456
|
+
imgc update # Check for updates
|
|
8451
8457
|
|
|
8452
8458
|
Options:
|
|
8453
8459
|
--help, -h Show this help message
|
|
8454
8460
|
--yes, -y Non-interactive mode (use defaults for optional prompts)
|
|
8455
8461
|
--source, -s Source file path (required with -y)
|
|
8456
|
-
--format, -f Target format: webp, jpeg, or
|
|
8462
|
+
--format, -f Target format: webp, jpeg, jpg, or png (required with -y)
|
|
8457
8463
|
--dest, -d Destination path (optional, auto-generated if not provided)
|
|
8458
8464
|
--compress, -c Enable compression (optional, default: false)
|
|
8459
8465
|
|
|
8466
|
+
Commands:
|
|
8467
|
+
update Check for the latest version on npm
|
|
8468
|
+
|
|
8460
8469
|
Examples:
|
|
8461
8470
|
imgc # Interactive mode
|
|
8462
8471
|
imgc --help # Show help
|
|
8463
8472
|
imgc -y --source photo.png --format webp
|
|
8464
8473
|
imgc -y --source photo.jpg --format jpeg --compress
|
|
8474
|
+
imgc update # Check for updates
|
|
8465
8475
|
`);
|
|
8466
8476
|
}
|
|
8467
8477
|
showSettings(settings) {
|
|
@@ -8493,6 +8503,29 @@ function getDefaultDestinationPath(sourcePath, format) {
|
|
|
8493
8503
|
}
|
|
8494
8504
|
|
|
8495
8505
|
// src/cli.ts
|
|
8506
|
+
var NPM_REGISTRY_URL = "https://registry.npmjs.org/image-convert-cli/latest";
|
|
8507
|
+
async function handleUpdate(fetchVersion, currentVersion) {
|
|
8508
|
+
const fetcher = fetchVersion || (async () => {
|
|
8509
|
+
const response = await fetch(NPM_REGISTRY_URL);
|
|
8510
|
+
if (!response.ok) {
|
|
8511
|
+
throw new Error(`Failed to fetch version: ${response.statusText}`);
|
|
8512
|
+
}
|
|
8513
|
+
const data = await response.json();
|
|
8514
|
+
return data.version;
|
|
8515
|
+
});
|
|
8516
|
+
const version = currentVersion || process.env.npm_package_version || "1.1.0";
|
|
8517
|
+
try {
|
|
8518
|
+
const latestVersion = await fetcher();
|
|
8519
|
+
if (latestVersion === version) {
|
|
8520
|
+
console.log(`You are running the latest version: ${version}`);
|
|
8521
|
+
} else {
|
|
8522
|
+
console.log(`Update available: ${version} -> ${latestVersion}`);
|
|
8523
|
+
console.log("Run: bun update to upgrade");
|
|
8524
|
+
}
|
|
8525
|
+
} catch (error) {
|
|
8526
|
+
console.error(`Error checking for updates: ${error.message}`);
|
|
8527
|
+
}
|
|
8528
|
+
}
|
|
8496
8529
|
async function runCli(options, promptService) {
|
|
8497
8530
|
const prompts = promptService || new InteractivePromptService;
|
|
8498
8531
|
if (options.help) {
|
|
@@ -8532,6 +8565,10 @@ async function runCli(options, promptService) {
|
|
|
8532
8565
|
// bin/index.ts
|
|
8533
8566
|
var args = process.argv.slice(2);
|
|
8534
8567
|
var options = {};
|
|
8568
|
+
if (args[0] === "update") {
|
|
8569
|
+
await handleUpdate();
|
|
8570
|
+
process.exit(0);
|
|
8571
|
+
}
|
|
8535
8572
|
for (let i = 0;i < args.length; i++) {
|
|
8536
8573
|
const arg = args[i];
|
|
8537
8574
|
if (arg === "--help" || arg === "-h") {
|
|
@@ -8542,7 +8579,7 @@ for (let i = 0;i < args.length; i++) {
|
|
|
8542
8579
|
options.source = args[++i];
|
|
8543
8580
|
} else if (arg === "--format" || arg === "-f") {
|
|
8544
8581
|
const format = args[++i];
|
|
8545
|
-
if (format && ["webp", "jpeg", "jpg"].includes(format)) {
|
|
8582
|
+
if (format && ["webp", "jpeg", "jpg", "png"].includes(format)) {
|
|
8546
8583
|
options.format = format;
|
|
8547
8584
|
}
|
|
8548
8585
|
} else if (arg === "--dest" || arg === "-d") {
|
package/package.json
CHANGED
package/src/cli.ts
CHANGED
|
@@ -3,6 +3,37 @@ import { IPromptService, InteractivePromptService } from "./prompts";
|
|
|
3
3
|
import { convertImage, displayConversionResult } from "./converter";
|
|
4
4
|
import { getDefaultDestinationPath } from "./utils/path";
|
|
5
5
|
|
|
6
|
+
const NPM_REGISTRY_URL = "https://registry.npmjs.org/image-convert-cli/latest";
|
|
7
|
+
|
|
8
|
+
export async function handleUpdate(
|
|
9
|
+
fetchVersion?: () => Promise<string>,
|
|
10
|
+
currentVersion?: string,
|
|
11
|
+
): Promise<void> {
|
|
12
|
+
const fetcher = fetchVersion || (async () => {
|
|
13
|
+
const response = await fetch(NPM_REGISTRY_URL);
|
|
14
|
+
if (!response.ok) {
|
|
15
|
+
throw new Error(`Failed to fetch version: ${response.statusText}`);
|
|
16
|
+
}
|
|
17
|
+
const data = await response.json() as { version: string };
|
|
18
|
+
return data.version;
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
const version = currentVersion || process.env.npm_package_version || "1.1.0";
|
|
22
|
+
|
|
23
|
+
try {
|
|
24
|
+
const latestVersion = await fetcher();
|
|
25
|
+
|
|
26
|
+
if (latestVersion === version) {
|
|
27
|
+
console.log(`You are running the latest version: ${version}`);
|
|
28
|
+
} else {
|
|
29
|
+
console.log(`Update available: ${version} -> ${latestVersion}`);
|
|
30
|
+
console.log("Run: bun update to upgrade");
|
|
31
|
+
}
|
|
32
|
+
} catch (error) {
|
|
33
|
+
console.error(`Error checking for updates: ${(error as Error).message}`);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
6
37
|
export async function runCli(
|
|
7
38
|
options: ConvertOptions,
|
|
8
39
|
promptService?: IPromptService,
|
package/src/converter.ts
CHANGED
|
@@ -16,7 +16,7 @@ export async function convertImage(
|
|
|
16
16
|
|
|
17
17
|
// Determine output format for Sharp
|
|
18
18
|
let sharpFormat: keyof sharp.FormatEnum;
|
|
19
|
-
let options: sharp.JpegOptions | sharp.WebpOptions | sharp.OutputInfo;
|
|
19
|
+
let options: sharp.JpegOptions | sharp.WebpOptions | sharp.PngOptions | sharp.OutputInfo;
|
|
20
20
|
|
|
21
21
|
switch (format) {
|
|
22
22
|
case "webp":
|
|
@@ -30,6 +30,12 @@ export async function convertImage(
|
|
|
30
30
|
sharpFormat = "jpeg";
|
|
31
31
|
options = compress ? { quality: 100, mozjpeg: true } : { quality: 100 };
|
|
32
32
|
break;
|
|
33
|
+
case "png":
|
|
34
|
+
sharpFormat = "png";
|
|
35
|
+
options = compress
|
|
36
|
+
? { compressionLevel: 9 }
|
|
37
|
+
: { compressionLevel: 6 };
|
|
38
|
+
break;
|
|
33
39
|
default:
|
|
34
40
|
throw new Error(`Unsupported format: ${format}`);
|
|
35
41
|
}
|
package/src/index.ts
CHANGED
|
@@ -2,7 +2,7 @@ export * from "./types";
|
|
|
2
2
|
export * from "./utils/path";
|
|
3
3
|
export * from "./utils/format";
|
|
4
4
|
export { convertImage, displayConversionResult } from "./converter";
|
|
5
|
-
export { runCli } from "./cli";
|
|
5
|
+
export { runCli, handleUpdate } from "./cli";
|
|
6
6
|
export {
|
|
7
7
|
type IPromptService,
|
|
8
8
|
InteractivePromptService,
|
package/src/prompts.ts
CHANGED
|
@@ -125,6 +125,7 @@ export class InteractivePromptService implements IPromptService {
|
|
|
125
125
|
{ value: "webp", description: "WebP format (recommended for web)" },
|
|
126
126
|
{ value: "jpeg", description: "JPEG format" },
|
|
127
127
|
{ value: "jpg", description: "JPG format" },
|
|
128
|
+
{ value: "png", description: "PNG format (lossless)" },
|
|
128
129
|
],
|
|
129
130
|
});
|
|
130
131
|
}
|
|
@@ -150,20 +151,25 @@ Usage:
|
|
|
150
151
|
imgc [options]
|
|
151
152
|
imgc --source <path> --format <format> [options]
|
|
152
153
|
imgc -y --source <path> --format <format>
|
|
154
|
+
imgc update # Check for updates
|
|
153
155
|
|
|
154
156
|
Options:
|
|
155
157
|
--help, -h Show this help message
|
|
156
158
|
--yes, -y Non-interactive mode (use defaults for optional prompts)
|
|
157
159
|
--source, -s Source file path (required with -y)
|
|
158
|
-
--format, -f Target format: webp, jpeg, or
|
|
160
|
+
--format, -f Target format: webp, jpeg, jpg, or png (required with -y)
|
|
159
161
|
--dest, -d Destination path (optional, auto-generated if not provided)
|
|
160
162
|
--compress, -c Enable compression (optional, default: false)
|
|
161
163
|
|
|
164
|
+
Commands:
|
|
165
|
+
update Check for the latest version on npm
|
|
166
|
+
|
|
162
167
|
Examples:
|
|
163
168
|
imgc # Interactive mode
|
|
164
169
|
imgc --help # Show help
|
|
165
170
|
imgc -y --source photo.png --format webp
|
|
166
171
|
imgc -y --source photo.jpg --format jpeg --compress
|
|
172
|
+
imgc update # Check for updates
|
|
167
173
|
`);
|
|
168
174
|
}
|
|
169
175
|
|
package/src/types.ts
CHANGED