Skip to content

Instantly share code, notes, and snippets.

@rauschma
Created December 4, 2022 18:58
Show Gist options
  • Save rauschma/57f51bc50a2526f75d4e6e2c06e9f78f to your computer and use it in GitHub Desktop.
Save rauschma/57f51bc50a2526f75d4e6e2c06e9f78f to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
// Use curl to download a URL to a file whose name is URL-decoded
// Related: https://github.com/curl/curl/issues/2573
import {execSync} from 'node:child_process';
// Documentation for node:child_process – https://exploringjs.com/nodejs-shell-scripting/ch_nodejs-child-process.html
export const WEB_PATH_SEP = '/';
export function getWebBasename(webPath) {
const sepIndex = webPath.lastIndexOf(WEB_PATH_SEP);
if (sepIndex < 0) {
return webPath;
}
return webPath.slice(sepIndex + WEB_PATH_SEP.length);
}
const urlStr = process.argv[2];
const basename = getWebBasename(new URL(urlStr).pathname);
// --location: follow redirects
execSync(
`curl '${urlStr}' --location --output '${decodeURIComponent(basename)}'`,
{stdio: 'inherit'}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment