Skip to content

Commit 92c281a

Browse files
committed
Refactor animation code to use async function and add script path retrieval
1 parent be80d61 commit 92c281a

2 files changed

Lines changed: 28 additions & 9 deletions

File tree

cli.ts

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,7 @@ async function animateImageWithLightbox(
791791

792792
const result = await runPlaywrightCliCodeJson<LightboxAnimateResult>(
793793
sessionName,
794-
buildCustomAnimateCode(animationPrompt, image.id),
794+
await buildCustomAnimateCode(animationPrompt, image.id),
795795
);
796796

797797
if (!result.ok || !result.videoId) {
@@ -816,13 +816,11 @@ async function animateImageWithLightbox(
816816
};
817817
}
818818

819-
function buildCustomAnimateCode(prompt: string, sourceMediaId: string): string {
820-
const scriptPath = fromFileUrl(
821-
new URL(
822-
"./lib/meta_lightbox_animate.js",
823-
import.meta.url,
824-
),
825-
);
819+
async function buildCustomAnimateCode(
820+
prompt: string,
821+
sourceMediaId: string,
822+
): Promise<string> {
823+
const scriptPath = await getCustomAnimateScriptPath();
826824
const input = { prompt, sourceMediaId };
827825

828826
return `async (page) => {
@@ -833,6 +831,27 @@ function buildCustomAnimateCode(prompt: string, sourceMediaId: string): string {
833831
}`;
834832
}
835833

834+
async function getCustomAnimateScriptPath(): Promise<string> {
835+
const scriptUrl = new URL("./lib/meta_lightbox_animate.js", import.meta.url);
836+
if (scriptUrl.protocol === "file:") {
837+
return fromFileUrl(scriptUrl);
838+
}
839+
840+
const response = await fetch(scriptUrl);
841+
if (!response.ok) {
842+
throw new Error(
843+
`Could not load Meta lightbox automation script: ${response.status} ${response.statusText}`,
844+
);
845+
}
846+
847+
const scriptPath = await Deno.makeTempFile({
848+
prefix: "meta-ai-lightbox-animate-",
849+
suffix: ".js",
850+
});
851+
await Deno.writeTextFile(scriptPath, await response.text());
852+
return scriptPath;
853+
}
854+
836855
async function pickFirstCompletedVideo(
837856
client: MetaAiClient,
838857
variants: GeneratedVideo[],

deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@cliat/meta-ai",
3-
"version": "0.3.3",
3+
"version": "0.3.4",
44
"license": "MIT",
55
"exports": {
66
".": "./mod.ts",

0 commit comments

Comments
 (0)