github.com/supabase/cli@v1.168.1/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 { Parser } from "https://deno.land/x/eszip@v0.30.0/mod.ts";
     4  
     5  async function write(p: string, content: string) {
     6    await Deno.mkdir(path.dirname(p), { recursive: true });
     7    await Deno.writeTextFile(p, content);
     8  }
     9  
    10  async function loadEszip(bytes: Uint8Array) {
    11    const parser = await Parser.createInstance();
    12    const specifiers = await parser.parseBytes(bytes);
    13    await parser.load();
    14    return { parser, specifiers };
    15  }
    16  
    17  async function extractEszip(
    18    destPath: string,
    19    entrypointUrl: string,
    20    parser: Parser,
    21    specifiers: string[],
    22  ) {
    23    const entrypointPath = path.fromFileUrl(entrypointUrl);
    24    const basePath = path.dirname(entrypointPath);
    25    for (const specifier of specifiers) {
    26      // skip remote dependencies
    27      if (!specifier.startsWith("file:")) {
    28        continue;
    29      }
    30      console.error(specifier);
    31      const module = await parser.getModuleSource(specifier);
    32      const absPath = path.fromFileUrl(specifier);
    33      const relPath = path.relative(basePath, absPath);
    34      const dest = path.join(destPath, relPath);
    35      console.info(path.resolve(dest));
    36      await write(dest, module);
    37    }
    38  }
    39  
    40  async function extractSource(destPath: string, entrypointUrl: string) {
    41    const buf = await readAll(Deno.stdin);
    42  
    43    const { parser, specifiers } = await loadEszip(buf);
    44    await extractEszip(destPath, entrypointUrl, parser, specifiers);
    45  }
    46  
    47  extractSource(...Deno.args);