Comparing version 0.1.0 to 0.2.0
#!/usr/bin/env node | ||
import{a as i,b as d,c as g}from"./chunk-4WBFE7PX.js";import p from"cli-progress";import f from"meow";import h from"fs";import u from"path";var s=f(` | ||
import{a as i,b as d,c}from"./chunk-HYWEAAWU.js";import p from"cli-progress";import f from"meow";import h from"fs";import u from"path";var s=f(` | ||
USAGE | ||
@@ -18,4 +18,6 @@ $ imgdl <url> ... [OPTIONS] | ||
-n, --name=<filename> The filename. Default: original filename or timestamp | ||
--max-retry=<number> Set the maximum number of times to retry the request if it fails | ||
--silent Disable logging | ||
--start=<number> The start index for increment mode. Default: 0 | ||
-t, --timeout=<number> Set timeout for each request in milliseconds | ||
-v, --version Show the version number | ||
@@ -29,5 +31,5 @@ | ||
$ imgdl https://example.com/image-{i}.jpg --increment --start=1 --end=10 | ||
`,{importMeta:import.meta,description:"Download an image from a URL",booleanDefault:void 0,flags:{dir:{shortFlag:"d",type:"string"},ext:{shortFlag:"e",type:"string"},increment:{shortFlag:"i",type:"boolean"},start:{type:"number"},end:{type:"number"},name:{shortFlag:"n",type:"string"},silent:{type:"boolean"}}});async function w(){let t=s.input,{flags:e}=s;if(t.length||s.showHelp(),e.increment){if(t.length>1)throw new i("Only one URL is allowed in increment mode");let n=t[0];if(!n.includes("{i}"))throw new i("The URL must contain {i} placeholder for the index");if(!e.end)throw new i("The end index is required in increment mode");if(e.start&&e.start>e.end)throw new i("The start index cannot be greater than the end index");let{start:l=0,end:c}=e;t=[];for(let m=l;m<=c;m+=1)t.push(n.replace("{i}",m.toString()))}e.silent||console.log(` | ||
Downloading...`);let o=new p.SingleBar({format:"{percentage}% [{bar}] {value}/{total} | Success: {success} | ETA: {eta_formatted} | Elapsed: {duration_formatted}",hideCursor:null,barsize:24}),a=0,r=0;!e.silent&&t.length>1&&o.start(t.length,0,{success:a}),await g(t.length===1?t[0]:t,{directory:e.dir,name:e.name,extension:e.ext,onSuccess:()=>{e.silent||(a+=1,o.increment({success:a}))},onError:(n,l)=>{if(r+=1,e.silent||o.increment(),n instanceof i||n instanceof d)throw n;h.appendFileSync(u.resolve(e.dir||process.cwd(),"error.log"),`${new Date().toISOString()} failed download from ${l}, ${n.name}: ${n.message} | ||
`)}}),e.silent||(o.stop(),console.log("Done!"),r&&console.log(`${r} image${r>1?"s":""} failed to download. See ./error.log for details.`))}w().catch(t=>{console.error(`${t.name}: ${t.message}`),process.exit(1)}); | ||
`,{importMeta:import.meta,description:"Download an image from a URL",booleanDefault:void 0,flags:{dir:{shortFlag:"d",type:"string"},ext:{shortFlag:"e",type:"string"},increment:{shortFlag:"i",type:"boolean"},start:{type:"number"},end:{type:"number"},name:{shortFlag:"n",type:"string"},maxRetry:{type:"number"},silent:{type:"boolean"},timeout:{shortFlag:"t",type:"number"}}});async function x(){let t=s.input,{flags:e}=s;if(t.length||s.showHelp(),e.increment){if(t.length>1)throw new i("Only one URL is allowed in increment mode");let n=t[0];if(!n.includes("{i}"))throw new i("The URL must contain {i} placeholder for the index");if(!e.end)throw new i("The end index is required in increment mode");if(e.start&&e.start>e.end)throw new i("The start index cannot be greater than the end index");let{start:m=0,end:g}=e;t=[];for(let l=m;l<=g;l+=1)t.push(n.replace("{i}",l.toString()))}e.silent||console.log(` | ||
Downloading...`);let o=new p.SingleBar({format:"{percentage}% [{bar}] {value}/{total} | Success: {success} | ETA: {eta_formatted} | Elapsed: {duration_formatted}",hideCursor:null,barsize:24}),a=0,r=0;!e.silent&&t.length>1&&o.start(t.length,0,{success:a}),await c(t.length===1?t[0]:t,{directory:e.dir,name:e.name,extension:e.ext,onSuccess:()=>{e.silent||(a+=1,o.increment({success:a}))},onError:(n,m)=>{if(r+=1,e.silent||o.increment(),n instanceof i||n instanceof d)throw n;h.appendFileSync(u.resolve(e.dir||process.cwd(),"error.log"),`${new Date().toISOString()} failed download from ${m}, ${n.name}: ${n.message} | ||
`)},maxRetry:e.maxRetry,timeout:e.timeout}),e.silent||(o.stop(),console.log("Done!"),r&&console.log(`${r} image${r>1?"s":""} failed to download. See ./error.log for details.`))}x().catch(t=>{console.error(`${t.name}: ${t.message}`),process.exit(1)}); | ||
//# sourceMappingURL=cli.js.map |
@@ -21,2 +21,8 @@ type DownloadOptions = { | ||
/** | ||
* Set the maximum number of times to retry the request if it fails. | ||
* | ||
* @default 2 | ||
*/ | ||
maxRetry?: number; | ||
/** | ||
* The extension of the image. | ||
@@ -29,2 +35,6 @@ * | ||
extension?: string; | ||
/** | ||
* Set timeout for each request in milliseconds. | ||
*/ | ||
timeout?: number; | ||
}; | ||
@@ -31,0 +41,0 @@ |
@@ -1,2 +0,2 @@ | ||
import{c as a}from"./chunk-4WBFE7PX.js";export{a as default}; | ||
import{c as a}from"./chunk-HYWEAAWU.js";export{a as default}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "img-dl", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Download public image(s), by command or programmatically", | ||
@@ -56,2 +56,3 @@ "type": "module", | ||
"cli-progress": "^3.12.0", | ||
"got": "^13.0.0", | ||
"meow": "^12.1.1", | ||
@@ -58,0 +59,0 @@ "sanitize-filename": "^1.6.3" |
# img-dl | ||
Downloade image(s), by command or programmatically | ||
Downloade image(s), by command or programmatically. The alternative for `image-downloader` package (see the [comparison](#comparison)). | ||
[![MIT license](https://img.shields.io/github/license/fityannugroho/img-dl.svg)](https://github.com/fityannugroho/img-dl/blob/main/LICENSE) | ||
[![npm version](https://img.shields.io/npm/v/img-dl.svg)](https://www.npmjs.com/package/img-dl) | ||
[![npm downloads](https://img.shields.io/npm/dm/img-dl.svg)](https://www.npmjs.com/package/img-dl) | ||
[![install size](https://packagephobia.com/badge?p=img-dl)](https://packagephobia.com/result?p=img-dl) | ||
## Prerequisites | ||
@@ -50,4 +56,6 @@ | ||
-n, --name=<filename> The filename. Default: original filename or timestamp | ||
--max-retry=<number> Set the maximum number of times to retry the request if it fails | ||
--silent Disable logging | ||
--start=<number> The start index for increment mode. Default: 0 | ||
-t, --timeout=<number> Set timeout for each request in milliseconds | ||
-v, --version Show the version number | ||
@@ -129,3 +137,3 @@ | ||
Type: `Options` <br> | ||
Type: [`Options`](https://github.com/fityannugroho/img-dl/blob/main/src/index.ts#L35) <br> | ||
Required: `false` | ||
@@ -135,6 +143,30 @@ | ||
| --- | --- | --- | --- | | ||
| `options.directory` | `string` | `process.cwd()` | The output directory | | ||
| `options.extension` | `string` | `'jpg'` | The file extension. If not specified, the original extension will be used. If the original extension is not available, `'jpg'` will be used. | | ||
| `options.name` | `string` | `'image'` | The filename. If not specified, the original filename will be used. If the original filename is not available, `'image'` will be used. <br>When downloading multiple images, `-index` will be appended to the end of the name (suffix). `index` will start from 1. For example: `image-1` | | ||
| `options.onSuccess` | `(image: Image) => void` | `undefined` | The callback function to be called when the image is successfully downloaded. Only available when downloading multiple images. | | ||
| `options.onError` | `(error: Error, url: string) => void` | `undefined` | The callback function to be called when the image fails to download. Only available when downloading multiple images. | | ||
| `directory` | `string` | `process.cwd()` | The output directory | | ||
| `extension` | `string` | jpg | The file extension. If not specified, the original extension will be used. If the original extension is not available, 'jpg' will be used. | | ||
| `name` | `string` | image | The filename. If not specified, the original filename will be used. If the original filename is not available, 'image' will be used. <br>When downloading multiple images, `-index` will be appended to the end of the name (suffix). `index` will start from 1. For example: 'image-1' | | ||
| `maxRetry` | `number` | 2 | Set the maximum number of times to retry the request if it fails. | ||
| `onSuccess` | `(image: Image) => void` | `undefined` | The callback function to be called when the image is successfully downloaded. Only available when downloading multiple images. | | ||
| `onError` | `(error: Error, url: string) => void` | `undefined` | The callback function to be called when the image fails to download. Only available when downloading multiple images. | | ||
| `timeout` | `number` | `undefined` | Set timeout for each request in milliseconds. | ||
## Comparison | ||
| Features | **img-dl** | [image-downloader][p1] | | ||
| --- | :---: | :---: | | ||
| Download single image | ✅ | ✅ | | ||
| Download multiple images | ✅ | ❌ | | ||
| CLI | ✅ | ❌ | | ||
| Increment download | ✅ | ❌ | | ||
| Custom filename | ✅ | ✅ | | ||
| Custom extension | ✅ | ❌ | | ||
| Request timeout | ✅ | ✅ | | ||
| Retry failed request | ✅ | ❌ | | ||
<!-- Project links --> | ||
[p1]: https://www.npmjs.com/package/image-downloader | ||
## Support This Project | ||
Give a ⭐️ if this project helped you! | ||
You can support this project by donating via [GitHub Sponsors](https://github.com/sponsors/fityannugroho), [Trakteer](https://trakteer.id/fityannugroho/tip), or [Saweria](https://saweria.co/fityannugroho). |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
39529
154
170
0
4
+ Addedgot@^13.0.0
+ Added@sindresorhus/is@5.6.0(transitive)
+ Added@szmarczak/http-timer@5.0.1(transitive)
+ Added@types/http-cache-semantics@4.0.4(transitive)
+ Addedcacheable-lookup@7.0.0(transitive)
+ Addedcacheable-request@10.2.14(transitive)
+ Addeddecompress-response@6.0.0(transitive)
+ Addeddefer-to-connect@2.0.1(transitive)
+ Addedform-data-encoder@2.1.4(transitive)
+ Addedget-stream@6.0.1(transitive)
+ Addedgot@13.0.0(transitive)
+ Addedhttp-cache-semantics@4.1.1(transitive)
+ Addedhttp2-wrapper@2.2.1(transitive)
+ Addedjson-buffer@3.0.1(transitive)
+ Addedkeyv@4.5.4(transitive)
+ Addedlowercase-keys@3.0.0(transitive)
+ Addedmimic-response@3.1.04.0.0(transitive)
+ Addednormalize-url@8.0.1(transitive)
+ Addedp-cancelable@3.0.0(transitive)
+ Addedquick-lru@5.1.1(transitive)
+ Addedresolve-alpn@1.2.1(transitive)
+ Addedresponselike@3.0.0(transitive)