|
| 1 | +import fs from "node:fs"; |
| 2 | +import { Readable } from "stream"; |
| 3 | +import { finished } from "stream/promises"; |
| 4 | +import { ReadableStream } from "stream/web"; |
| 5 | +import { fileURLToPath, parse } from "url"; |
| 6 | +import path from "path"; |
| 7 | + |
| 8 | +import yargs from "yargs"; |
| 9 | +import { hideBin } from "yargs/helpers"; |
| 10 | +import { RuntimeError } from "run-time-error"; |
| 11 | + |
| 12 | +const TAG = "[tools/download-file-to-disk.ts]"; |
| 13 | + |
| 14 | +export interface IDownloadFileToDiskReq { |
| 15 | + readonly url: string; |
| 16 | + readonly outputFilePath: string; |
| 17 | +} |
| 18 | + |
| 19 | +export interface IDownloadFileToDiskRes { |
| 20 | + readonly url: string; |
| 21 | + readonly outputFilePath: string; |
| 22 | +} |
| 23 | + |
| 24 | +const nodePath = path.resolve(process.argv[1]); |
| 25 | +const modulePath = path.resolve(fileURLToPath(import.meta.url)); |
| 26 | +const isRunningDirectlyViaCLI = nodePath === modulePath; |
| 27 | + |
| 28 | +const main = async (argv: string[], env: NodeJS.ProcessEnv) => { |
| 29 | + const req = await createRequest(argv, env); |
| 30 | + await downloadFileToDisk(req); |
| 31 | +}; |
| 32 | + |
| 33 | +if (isRunningDirectlyViaCLI) { |
| 34 | + main(process.argv, process.env); |
| 35 | +} |
| 36 | + |
| 37 | +async function createRequest( |
| 38 | + argv: string[], |
| 39 | + env: NodeJS.ProcessEnv, |
| 40 | +): Promise<IDownloadFileToDiskReq> { |
| 41 | + if (!argv) { |
| 42 | + throw new RuntimeError(`Process argv cannot be falsy.`); |
| 43 | + } |
| 44 | + if (!env) { |
| 45 | + throw new RuntimeError(`Process env cannot be falsy.`); |
| 46 | + } |
| 47 | + |
| 48 | + const optOutputFilePath = |
| 49 | + "The absolute path on disk where the downloaded file will be streamed."; |
| 50 | + |
| 51 | + const optUrl = "The URL to download from."; |
| 52 | + |
| 53 | + const parsedCfg = await yargs(hideBin(argv)) |
| 54 | + .env("CACTI_") |
| 55 | + .option("url", { |
| 56 | + alias: "u", |
| 57 | + type: "string", |
| 58 | + demandOption: false, |
| 59 | + description: optUrl, |
| 60 | + default: hideBin(argv)[0], |
| 61 | + }) |
| 62 | + .option("output-file-path", { |
| 63 | + alias: "o", |
| 64 | + type: "string", |
| 65 | + description: optOutputFilePath, |
| 66 | + defaultDescription: "Defaults to the current working directory.", |
| 67 | + default: "./", |
| 68 | + }).argv; |
| 69 | + |
| 70 | + const url = parsedCfg.url; |
| 71 | + |
| 72 | + console.log("%s parsing URL '%s'", TAG, url); |
| 73 | + const { pathname } = parse(url); |
| 74 | + const pathnameOrDefault = pathname || "new_download_file"; |
| 75 | + const filename = path.basename(pathnameOrDefault); |
| 76 | + |
| 77 | + const endsWithDirSeparator = parsedCfg.outputFilePath.endsWith(path.sep); |
| 78 | + |
| 79 | + const outputFilePath = endsWithDirSeparator |
| 80 | + ? path.join(parsedCfg.outputFilePath, filename) |
| 81 | + : parsedCfg.outputFilePath; |
| 82 | + |
| 83 | + const req: IDownloadFileToDiskReq = { |
| 84 | + url, |
| 85 | + outputFilePath, |
| 86 | + }; |
| 87 | + |
| 88 | + return req; |
| 89 | +} |
| 90 | + |
| 91 | +export async function downloadFileToDisk( |
| 92 | + req: IDownloadFileToDiskReq, |
| 93 | +): Promise<unknown> { |
| 94 | + const { url, outputFilePath } = req; |
| 95 | + console.log("%s downloading %s into %s ...", TAG, url, outputFilePath); |
| 96 | + const stream = fs.createWriteStream(req.outputFilePath); |
| 97 | + const { body } = await fetch(req.url); |
| 98 | + if (!body) { |
| 99 | + throw new RuntimeError("fetching %s did not yield a response body.", url); |
| 100 | + } |
| 101 | + const bodyNodeJs = body as unknown as ReadableStream; |
| 102 | + await finished(Readable.fromWeb(bodyNodeJs).pipe(stream)); |
| 103 | + console.log("%s downloaded %s into %s OK", TAG, url, outputFilePath); |
| 104 | + return { outputFilePath, url }; |
| 105 | +} |
0 commit comments