Comparing version
#!/usr/bin/env node | ||
import{a as i,b as p,c}from"./chunk-SGMKDPTA.js";import g from"cli-progress";import u from"meow";import f from"fs";import b from"path";var d=u(` | ||
import{a as r,b,c as w}from"./chunk-AMIM6PVT.js";import x from"cli-progress";import y from"meow";import $ from"fs";import S from"path";import m from"chalk";var c=y(` | ||
USAGE | ||
@@ -34,5 +34,8 @@ $ imgdl <url> ... [OPTIONS] | ||
$ imgdl https://example.com/image.jpg --header="User-Agent: Mozilla/5.0" --header="Cookie: foo=bar" | ||
`,{importMeta:import.meta,booleanDefault:void 0,flags:{dir:{shortFlag:"d",type:"string"},ext:{shortFlag:"e",type:"string"},header:{shortFlag:"H",type:"string",isMultiple:!0},increment:{shortFlag:"i",type:"boolean"},interval:{type:"number"},start:{type:"number"},end:{type:"number"},name:{shortFlag:"n",type:"string"},maxRetry:{type:"number"},silent:{type:"boolean"},step:{type:"number"},timeout:{shortFlag:"t",type:"number"}}});async function w(){let t=d.input,{flags:e}=d;if(t.length||d.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:r=0,end:m}=e;t=[];for(let s=r;s<=m;s+=1)t.push(n.replace("{i}",s.toString()))}e.silent||console.log(` | ||
Downloading...`);let o=new g.SingleBar({format:"{percentage}% [{bar}] {value}/{total} | Success: {success} | ETA: {eta_formatted} | Elapsed: {duration_formatted}",hideCursor:null,barsize:24}),l=0,a=0;!e.silent&&t.length>1&&o.start(t.length,0,{success:l});let h={};e.header&&e.header.forEach(n=>{let[r,m]=n.split(":");if(!r||!m)throw new i("Invalid header format");h[r.trim()]=m.trim()}),await c(t.length===1?t[0]:t,{directory:e.dir,name:e.name,extension:e.ext,headers:h,interval:e.interval,onSuccess:()=>{e.silent||(l+=1,o.increment({success:l}))},onError:(n,r)=>{if(a+=1,e.silent||o.increment(),n instanceof i||n instanceof p)throw n;f.appendFileSync(b.resolve(e.dir||process.cwd(),"error.log"),`${new Date().toISOString()} failed download from ${r}, ${n.name}: ${n.message} | ||
`)},maxRetry:e.maxRetry,step:e.step,timeout:e.timeout}),e.silent||(o.stop(),console.log("Done!"),a&&console.log(`${a} image${a>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,booleanDefault:void 0,flags:{dir:{shortFlag:"d",type:"string"},ext:{shortFlag:"e",type:"string"},header:{shortFlag:"H",type:"string",isMultiple:!0},increment:{shortFlag:"i",type:"boolean"},interval:{type:"number"},start:{type:"number"},end:{type:"number"},name:{shortFlag:"n",type:"string"},maxRetry:{type:"number"},silent:{type:"boolean"},step:{type:"number"},timeout:{shortFlag:"t",type:"number"}}}),T=m.bold.green,g=m.bold.red,v=m.yellow,s=m.dim;async function D(){let t=c.input,{flags:e}=c;if(t.length||c.showHelp(),e.increment){if(t.length>1)throw new r("Only one URL is allowed in increment mode");let n=t[0];if(!n.includes("{i}"))throw new r("The URL must contain {i} placeholder for the index");if(!e.end)throw new r("The end index is required in increment mode");if(e.start&&e.start>e.end)throw new r("The start index cannot be greater than the end index");let{start:i=0,end:l}=e;t=[];for(let p=i;p<=l;p+=1)t.push(n.replace("{i}",p.toString()))}e.silent||console.log(` | ||
${s("Downloading...")} | ||
${v("Press Ctrl+C to abort")}`);let d=s("|"),a=new x.SingleBar({format:`{percentage}% [{bar}] {value}/{total} ${d} ${T("\u2705 {success}")} ${d} ${g("\u274C {errorCount}")} ${d} ETA: {eta_formatted} ${s("/ {duration_formatted}")}`,hideCursor:null,barsize:24}),h=0,o=0;!e.silent&&t.length>1&&a.start(t.length,0,{success:h,errorCount:o});let u={};e.header&&e.header.forEach(n=>{let[i,l]=n.split(":");if(!i||!l)throw new r("Invalid header format");u[i.trim()]=l.trim()});let f=new AbortController;process.on("SIGINT",()=>{a.stop(),console.log(s(` | ||
Aborting...`)),f.abort()}),await w(t.length===1?t[0]:t,{directory:e.dir,name:e.name,extension:e.ext,headers:u,interval:e.interval,onSuccess:()=>{e.silent||(h+=1,a.increment({success:h}))},onError:(n,i)=>{if(o+=1,e.silent||a.increment({errorCount:o}),n instanceof r||n instanceof b)throw n;$.appendFileSync(S.resolve(e.dir||process.cwd(),"error.log"),`${new Date().toISOString()} failed download from ${i}, ${n.name}: ${n.message} | ||
`)},maxRetry:e.maxRetry,step:e.step,timeout:e.timeout,signal:f.signal}),e.silent||(a.stop(),console.log(s("Done!")),o&&console.log(g(`${o} image${o>1?"s":""} failed to download. See ./error.log for details.`)))}D().catch(t=>{console.error(g(` | ||
${t.name}: ${t.message}`)),process.exit(1)}); | ||
//# sourceMappingURL=cli.js.map |
@@ -42,2 +42,6 @@ type DownloadOptions = { | ||
timeout?: number; | ||
/** | ||
* The signal which can be used to abort requests. | ||
*/ | ||
signal?: AbortSignal; | ||
}; | ||
@@ -122,2 +126,6 @@ | ||
interval?: number; | ||
/** | ||
* The signal which can be used to abort requests. | ||
*/ | ||
signal?: AbortSignal; | ||
}; | ||
@@ -128,2 +136,2 @@ declare function imgdl(url: string, options?: Options): Promise<Image>; | ||
export { Image, Options, imgdl as default }; | ||
export { type Image, type Options, imgdl as default }; |
@@ -1,2 +0,2 @@ | ||
import{c as a}from"./chunk-SGMKDPTA.js";export{a as default}; | ||
import{c as a}from"./chunk-AMIM6PVT.js";export{a as default}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "img-dl", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "Download image(s), by command or programmatically", | ||
@@ -43,15 +43,16 @@ "type": "module", | ||
"devDependencies": { | ||
"@types/cli-progress": "^3.11.4", | ||
"@types/node": "^20.8.6", | ||
"@typescript-eslint/eslint-plugin": "^6.8.0", | ||
"@typescript-eslint/parser": "^6.8.0", | ||
"@vitest/coverage-v8": "^0.34.6", | ||
"eslint": "^8.51.0", | ||
"@types/cli-progress": "^3.11.5", | ||
"@types/node": "^20.10.4", | ||
"@typescript-eslint/eslint-plugin": "^6.13.2", | ||
"@typescript-eslint/parser": "^6.13.2", | ||
"@vitest/coverage-v8": "^1.0.2", | ||
"eslint": "^8.55.0", | ||
"eslint-config-airbnb-typescript": "^17.1.0", | ||
"execa": "^8.0.1", | ||
"tsup": "^7.2.0", | ||
"typescript": "^5.2.2", | ||
"vitest": "^0.34.6" | ||
"tsup": "^8.0.1", | ||
"typescript": "^5.3.3", | ||
"vitest": "^1.0.2" | ||
}, | ||
"dependencies": { | ||
"chalk": "^5.3.0", | ||
"cli-progress": "^3.12.0", | ||
@@ -58,0 +59,0 @@ "got": "^13.0.0", |
@@ -153,2 +153,3 @@ # img-dl | ||
| `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. | | ||
| `signal` | `AbortSignal \| undefined` | The signal to abort the request. | | ||
| `step` | `number` | 5 | The number of requests to make at the same time when downloading multiple images. | | ||
@@ -155,0 +156,0 @@ | `timeout` | `number` | `undefined` | Set timeout for each request in milliseconds. |
Sorry, the diff of this file is not supported yet
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
47538
8.57%188
7.43%178
0.56%6
20%+ Added
+ Added