Last active
October 29, 2024 08:29
-
-
Save guest271314/5c7489b5bce942ee8e9c35846d1acde1 to your computer and use it in GitHub Desktop.
Fetch non-entrypoint, non-TypeScript files from jsr:, write files to local vendor directory containing enttrypoint .ts file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Fetch non-entrypoint, non-TypeScript files from jsr:, | |
// write files to local vendor directory containing enttrypoint .ts file | |
// Usage: deno -A fetch-jsr-files.js @scope mod | |
const [scope, mod] = Deno.args; | |
const { latest: version } = (await import( | |
`https://jsr.io/${scope}/${mod}/meta.json`, | |
{ | |
with: { | |
type: "json", | |
}, | |
} | |
)).default; | |
const url = `https://jsr.io/${scope}/${mod}/${version}_meta.json`; | |
const { manifest } = (await import(url, { | |
with: { | |
type: "json", | |
}, | |
})).default; | |
for (const filePath of Object.keys(manifest)) { | |
const jsrRemoteUrl = new URL( | |
`.${filePath}`, | |
url.split(/(?<!:\/)\//).toSpliced(-1, 0, version).join("/"), | |
); | |
const { hostname, pathname } = jsrRemoteUrl; | |
const jsrLocalPath = new URL( | |
`./vendor/${hostname}${pathname}`, | |
import.meta.url, | |
); | |
const mode = /((\.(md|json))|LICENSE)$/.test(jsrRemoteUrl.href) | |
? 0o777 | |
: 0o764; | |
console.log( | |
`Fetching ${jsrRemoteUrl}, writing file to ${jsrLocalPath.pathname}, mode ${mode}`, | |
); | |
const file = await (await fetch(jsrRemoteUrl)).bytes(); | |
await Deno.writeFile(jsrLocalPath.pathname, file, { | |
mode, | |
}); | |
} | |
console.log(`Done fetching files from ${url}`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment