Skip to content

Commit

Permalink
deno: support 2.0+
Browse files Browse the repository at this point in the history
update deprecated function calls + install script
  • Loading branch information
tomholford committed Oct 23, 2024
1 parent 2275263 commit 69e10d5
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ that match the filter.

## Prerequisites

- [Deno](https://deno.land) 1.27.0+
- [Deno](https://deno.land) 2.0+

## Usage

Expand Down
2 changes: 1 addition & 1 deletion bin/install
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/bash

deno install -f --allow-net --allow-write --allow-read --unstable --name media-downloader main.ts
deno install -f --global --allow-net --allow-write --allow-read --unstable --name media-downloader main.ts
18 changes: 13 additions & 5 deletions url_downloader.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { ensureDir, exists } from "https://deno.land/std/fs/mod.ts";
import { readerFromStreamReader } from "https://deno.land/[email protected]/io/mod.ts";
import { join } from "https://deno.land/std/path/mod.ts";
import { blue, bold, green } from "https://deno.land/std/fmt/colors.ts";
import { fmtFileSize } from "https://deno.land/x/getfiles/mod.ts";
import { copy } from "https://deno.land/[email protected]/streams/mod.ts";

class UrlDownloader {
url: string;
Expand Down Expand Up @@ -41,9 +39,19 @@ class UrlDownloader {
create: true,
write: true,
});
const reader = readerFromStreamReader(response.body!.getReader());
await copy(reader, newFile);
const stats = await Deno.fstat(newFile.rid);

const reader = response.body?.getReader();
if (reader) {
while (true) {
const { done, value } = await reader.read();
if (done) break;
if (value) {
await newFile.write(value);
}
}
}

const stats = await newFile.stat();
newFile.close();
console.log(blue(`Wrote ${fmtFileSize(stats.size)} to ${this.filepath}`));
}
Expand Down

0 comments on commit 69e10d5

Please sign in to comment.