如何下载产物

从 SDK 将生成的 HTML 与图像保存到磁盘。

每个 Stitch 屏幕都会产出两个可下载的产物:一个 HTML 文件(内联 Tailwind CSS 的完整文档)和一张截图图像。SDK 会提供下载 URL——你只需抓取并保存。

获取下载 URL

import { stitch } from "@google/stitch-sdk";

const projects = await stitch.projects();
const screens = await projects.at(0).screens();
const screen = screens.at(0);

const htmlUrl = await screen.getHtml();
const imageUrl = await screen.getImage();

把 HTML 保存到磁盘

import fs from "fs/promises";

const response = await fetch(htmlUrl);
const html = await response.text();
await fs.writeFile(`${screen.id}.html`, html);

在浏览器中打开这个文件,你会看到带有 Tailwind 类、完整渲染的设计。

保存截图图像

图像以二进制数据形式返回。请使用 arrayBuffer() 而不是 text():

const imgResponse = await fetch(imageUrl);
const buffer = await imgResponse.arrayBuffer();
await fs.writeFile(`${screen.id}.jpeg`, Buffer.from(buffer));

批量下载项目中的所有屏幕

import fs from "fs/promises";
import path from "path";

const outDir = "./designs";
await fs.mkdir(outDir, { recursive: true });

const project = (await stitch.projects()).at(0);
const screens = await project.screens();

for (const screen of screens) {
  const htmlUrl = await screen.getHtml();
  if (htmlUrl) {
    const res = await fetch(htmlUrl);
    await fs.writeFile(path.join(outDir, `${screen.id}.html`), await res.text());
  }

  const imgUrl = await screen.getImage();
  if (imgUrl) {
    const res = await fetch(imgUrl);
    await fs.writeFile(path.join(outDir, `${screen.id}.jpeg`), Buffer.from(await res.arrayBuffer()));
  }
}

console.log(`Downloaded ${screens.length} screens to ${outDir}`);

原文: https://stitch.withgoogle.com/docs/sdk/download-artifacts/

本页翻译自 Google Stitch 官方文档,原文版权归 Google LLC 所有。本站为独立第三方翻译,与 Google 无关联。