wget can write to file

This commit is contained in:
b1ek 2023-03-17 16:09:14 +10:00
parent 34aaa678b1
commit b41f37efa7
Signed by: blek
GPG Key ID: 14546221E3595D0C
1 changed files with 8 additions and 6 deletions

View File

@ -26,8 +26,7 @@ Usage: ${argv[0]} [URL] [-O out.file] [-V] [-Q] [-s]
terminal.writeln(`${argv[0]}: missing output file`); terminal.writeln(`${argv[0]}: missing output file`);
return; return;
} }
filepath = argv[argv.indexOf('-O') + 1] filepath = argv[argv.indexOf('-O') + 1];
return;
} }
function progress(p) { function progress(p) {
@ -35,11 +34,15 @@ Usage: ${argv[0]} [URL] [-O out.file] [-V] [-Q] [-s]
if (total == 0) if (total == 0)
total = '?' total = '?'
terminal.write(`\rDownloading... ${p.loaded}/${total}`); // terminal.write(`\rDownloading... ${p.loaded}/${total}`);
} }
function write(file) { function write(file) {
if (filepath == '-') {
terminal.write(file);
} else {
fs.writeFileSync(filepath, file);
}
} }
let file; let file;
@ -50,8 +53,7 @@ Usage: ${argv[0]} [URL] [-O out.file] [-V] [-Q] [-s]
req.onprogress = progress; req.onprogress = progress;
req.onload = (e) => { req.onload = (e) => {
if (e.type == 'load') { if (e.type == 'load') {
let enc = new TextDecoder("utf-8"); write(e.response);
terminal.write(enc.decode(req.response));
} }
}; };
req.send(); req.send();