|
| 1 | +// @effect-diagnostics nodeBuiltinImport:off - Host-side Pen CLI and image export automation. |
| 2 | +import * as NodeChildProcess from "node:child_process"; |
| 3 | +import * as NodeFSP from "node:fs/promises"; |
| 4 | +import * as NodeOS from "node:os"; |
| 5 | +import * as NodePath from "node:path"; |
| 6 | +import * as NodeProcess from "node:process"; |
| 7 | +import * as NodeURL from "node:url"; |
| 8 | +import * as NodeUtil from "node:util"; |
| 9 | + |
| 10 | +import showcaseConfig, { type ShowcaseAppearance } from "./mobile-showcase.config.ts"; |
| 11 | +import { |
| 12 | + normalizeStorePng, |
| 13 | + parseShowcaseCliArgs, |
| 14 | + planShowcaseCaptures, |
| 15 | + validateStoreAsset, |
| 16 | +} from "./mobile-showcase.ts"; |
| 17 | + |
| 18 | +const TEMPLATE_DIRECTORY = NodeURL.fileURLToPath( |
| 19 | + new URL("./mobile-showcase-assets/", import.meta.url), |
| 20 | +); |
| 21 | +const ASSETS_DIRECTORY = "T3 Code AppStore Assets-assets"; |
| 22 | +export const PEN_LAYOUTS = { |
| 23 | + dark: ["VzYVc", "HTuJU", "aJlvZ", "xEv0b", "Fv2DI", "cPgee", "Uv0Se"], |
| 24 | + light: ["vLscN", "jvvlj", "E9ELUC", "R4sQ6I", "eYCoY", "E3y7Q", "mivY7"], |
| 25 | +} as const; |
| 26 | +export const PEN_SCENES = [ |
| 27 | + ["01-sessions", "threads"], |
| 28 | + ["02-environments", "environments"], |
| 29 | + ["03-chat", "thread"], |
| 30 | + ["04-terminal", "terminal"], |
| 31 | + ["05-diff", "review"], |
| 32 | + ["06-live-activity", "agent-activity"], |
| 33 | +] as const; |
| 34 | + |
| 35 | +export function penScreenshotFilename(asset: string, appearance: ShowcaseAppearance): string { |
| 36 | + return `${asset}-${appearance}${asset === "06-live-activity" ? "-v2" : "@hi2"}.png`; |
| 37 | +} |
| 38 | + |
| 39 | +export async function preparePenAssets( |
| 40 | + inputDirectory: string, |
| 41 | + temporaryDirectory: string, |
| 42 | + appearance: ShowcaseAppearance, |
| 43 | +): Promise<void> { |
| 44 | + const device = showcaseConfig.devices.find((candidate) => candidate.id === "iphone-6.9")!; |
| 45 | + await NodeFSP.cp(TEMPLATE_DIRECTORY, temporaryDirectory, { recursive: true }); |
| 46 | + for (const [asset, scene] of PEN_SCENES) { |
| 47 | + const bytes = await NodeFSP.readFile(NodePath.join(inputDirectory, `${scene}.png`)); |
| 48 | + validateStoreAsset(device.storeAsset, bytes, `${appearance}/${scene}`); |
| 49 | + await NodeFSP.writeFile( |
| 50 | + NodePath.join(temporaryDirectory, ASSETS_DIRECTORY, penScreenshotFilename(asset, appearance)), |
| 51 | + bytes, |
| 52 | + ); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +export async function renderPenAssets( |
| 57 | + inputDirectory: string, |
| 58 | + outputDirectory: string, |
| 59 | + appearance: ShowcaseAppearance, |
| 60 | +): Promise<void> { |
| 61 | + if (!NodeProcess.env.PEN_CLI_KEY) { |
| 62 | + throw new Error("Set PEN_CLI_KEY before exporting the Pen App Store layouts."); |
| 63 | + } |
| 64 | + const temporaryDirectory = await NodeFSP.mkdtemp( |
| 65 | + NodePath.join(NodeOS.tmpdir(), "t3-pen-assets-"), |
| 66 | + ); |
| 67 | + try { |
| 68 | + await preparePenAssets(inputDirectory, temporaryDirectory, appearance); |
| 69 | + const exportDirectory = NodePath.join(temporaryDirectory, "exports"); |
| 70 | + const command = `execute(${JSON.stringify({ |
| 71 | + input: `Export(${JSON.stringify(PEN_LAYOUTS[appearance])}, "png", ${JSON.stringify(exportDirectory)}, {scale: 1})`, |
| 72 | + })})\nexit()\n`; |
| 73 | + const result = NodeChildProcess.spawnSync( |
| 74 | + "pen", |
| 75 | + [ |
| 76 | + "interactive", |
| 77 | + "--in", |
| 78 | + NodePath.join(temporaryDirectory, "app-store.pen"), |
| 79 | + "--out", |
| 80 | + NodePath.join(temporaryDirectory, "render.pen"), |
| 81 | + ], |
| 82 | + { input: command, encoding: "utf8", timeout: 300_000, maxBuffer: 4 * 1024 * 1024 }, |
| 83 | + ); |
| 84 | + if ( |
| 85 | + result.error || |
| 86 | + result.status !== 0 || |
| 87 | + /\b(error|failed)\b/iu.test(result.stdout + result.stderr) |
| 88 | + ) { |
| 89 | + const detail = |
| 90 | + `${result.error?.message ?? ""}\n${result.stdout}\n${result.stderr}`.replaceAll( |
| 91 | + NodeProcess.env.PEN_CLI_KEY, |
| 92 | + "[REDACTED]", |
| 93 | + ); |
| 94 | + throw new Error(`Pen export failed: ${detail}`); |
| 95 | + } |
| 96 | + const images = await Promise.all( |
| 97 | + PEN_LAYOUTS[appearance].map(async (id, index) => { |
| 98 | + const bytes = normalizeStorePng( |
| 99 | + await NodeFSP.readFile(NodePath.join(exportDirectory, `${id}.png`)), |
| 100 | + ); |
| 101 | + validateStoreAsset( |
| 102 | + { |
| 103 | + store: "apple", |
| 104 | + directory: outputDirectory, |
| 105 | + width: 1290, |
| 106 | + height: 2796, |
| 107 | + minimumUploadCount: 7, |
| 108 | + maximumUploadCount: 7, |
| 109 | + }, |
| 110 | + bytes, |
| 111 | + id, |
| 112 | + ); |
| 113 | + return { filename: `${PEN_SCENES[index]?.[0] ?? "07-testimonials"}.png`, bytes }; |
| 114 | + }), |
| 115 | + ); |
| 116 | + await NodeFSP.mkdir(outputDirectory, { recursive: true }); |
| 117 | + for (const image of images) { |
| 118 | + await NodeFSP.writeFile(NodePath.join(outputDirectory, image.filename), image.bytes); |
| 119 | + } |
| 120 | + NodeProcess.stdout.write(`Exported 7 ${appearance} Pen layouts to ${outputDirectory}\n`); |
| 121 | + } finally { |
| 122 | + await NodeFSP.rm(temporaryDirectory, { recursive: true, force: true }); |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +async function main(): Promise<void> { |
| 127 | + const { values } = NodeUtil.parseArgs({ |
| 128 | + options: { |
| 129 | + input: { type: "string", default: "artifacts/app-store/screenshots/apple/iphone-6.9" }, |
| 130 | + output: { type: "string", default: "artifacts/app-store/pen" }, |
| 131 | + appearance: { type: "string", default: "both" }, |
| 132 | + theme: { type: "string", default: "t3-code" }, |
| 133 | + }, |
| 134 | + }); |
| 135 | + const options = parseShowcaseCliArgs([ |
| 136 | + "--device", |
| 137 | + "iphone-6.9", |
| 138 | + "--appearance", |
| 139 | + values.appearance, |
| 140 | + "--theme", |
| 141 | + values.theme, |
| 142 | + ]); |
| 143 | + for (const capture of planShowcaseCaptures(showcaseConfig, options)) { |
| 144 | + await renderPenAssets( |
| 145 | + NodePath.resolve(values.input, capture.appearance, capture.theme), |
| 146 | + NodePath.resolve(values.output, capture.appearance, capture.theme), |
| 147 | + capture.appearance, |
| 148 | + ); |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +if (import.meta.main) { |
| 153 | + main().catch((error: unknown) => { |
| 154 | + NodeProcess.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`); |
| 155 | + NodeProcess.exit(1); |
| 156 | + }); |
| 157 | +} |
0 commit comments