Skip to content

Instantly share code, notes, and snippets.

@lowlighter
Last active March 14, 2021 21:58
Show Gist options
  • Select an option

  • Save lowlighter/0ae01bee4a1cc5aebd6f8e6352ad4851 to your computer and use it in GitHub Desktop.

Select an option

Save lowlighter/0ae01bee4a1cc5aebd6f8e6352ad4851 to your computer and use it in GitHub Desktop.
Licensed polyfill for metrics with deno projects
/* Copyright 2021 lowlighter. See https://github.com/lowlighter/deno-template
* This is a polyfill to make github/licensed works with deno and lets lowlighter/metrics generates licenses svg
*/
//Loads deno info
const deps = await Promise.all([...new Set<string>(JSON.parse(new TextDecoder().decode(await Deno.run({cmd:["/root/.deno/bin/vr", "info"], stdout:"piped"}).output())).modules
//Get specifier, transform it to url, filter out local deps and extract dependency name
.map(({specifier}:{specifier:string}) => new URL(specifier))
.filter((url:URL) => url.origin !== "null")
.map((url:URL) => `${url.origin}${url.pathname.match(/^(?<name>[/][\s\S]+@v?\d+[.]\d+[.]\d+[/])/)?.groups?.name}`))]
//Fetch licenses
.map(async url => ({dependency:url as string, license:await fetch(`${url}LICENSE`).then(response => response.text()).catch(() => null)})))
//Extract licenses
const licenses = await Promise.all(deps.map(async (result:{dependency:string, license:string|null}) => {
const {dependency, license} = await result
let key = null
if (license) {
const name = dependency.split("/").slice(-2)?.shift()
const tmp = await Deno.makeTempDir()
await Deno.mkdir(`${tmp}/${name}`)
const path = `${tmp}/${name}/LICENSE`
await Deno.writeTextFile(path, license)
const licensee = await Deno.run({cmd:["licensee", "detect", "--json", path], stdout:"piped"})
const {licenses:[matched]} = await JSON.parse(new TextDecoder().decode(await licensee.output()))
key = matched.key
}
return {dependency, license:key}
}))
//Format as licensed
console.log(JSON.stringify({apps:[{sources:[{dependencies:licenses}]}]}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment