github.com/Redstoneguy129/cli@v0.0.0-20230211220159-15dca4e91917/internal/utils/denos/extract.ts (about) 1 import * as path from "https://deno.land/std@0.127.0/path/mod.ts"; 2 import {readAll} from "https://deno.land/std@0.162.0/streams/conversion.ts"; 3 import {decompress} from "https://deno.land/x/brotli@0.1.7/mod.ts"; 4 5 import {Parser} from "https://deno.land/x/eszip@v0.30.0/mod.ts"; 6 7 function url2path(url: string) { 8 const srcPath = new URL(url).pathname.replace("/src", ""); 9 return path.join(...srcPath.split("/").filter(Boolean)); 10 } 11 12 async function write(p: string, content: string) { 13 await Deno.mkdir(path.dirname(p), { recursive: true }); 14 await Deno.writeTextFile(p, content); 15 } 16 17 async function loadEszip(bytes: Uint8Array) { 18 const parser = await Parser.createInstance(); 19 const specifiers = await parser.parseBytes(bytes); 20 await parser.load(); 21 return { parser, specifiers }; 22 } 23 24 async function extractEszip(dest: string, parser, specifiers) { 25 const imports = {}; 26 27 for (const specifier of specifiers) { 28 // skip remote dependencies 29 if (!specifier.startsWith("file:")) { 30 continue; 31 } 32 const module = await parser.getModuleSource(specifier); 33 await write(path.join(dest, url2path(specifier)), module); 34 } 35 } 36 37 async function extractSource(dest: string) { 38 const buf = await readAll(Deno.stdin); 39 // response is compressed with Brotli 40 const decompressed = decompress(buf); 41 const { parser, specifiers } = await loadEszip(decompressed); 42 await extractEszip(dest, parser, specifiers); 43 } 44 45 extractSource(Deno.args[0]);