From a8052fc100e0b8d7a63d435a4b0c8197d88585d2 Mon Sep 17 00:00:00 2001 From: dylan <> Date: Wed, 27 Mar 2024 19:17:12 -0700 Subject: [PATCH] try execFile --- src/server/api/release.ts | 9 +++------ src/server/util/pico8.ts | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 6 deletions(-) create mode 100644 src/server/util/pico8.ts diff --git a/src/server/api/release.ts b/src/server/api/release.ts index 3215dc2..f518198 100644 --- a/src/server/api/release.ts +++ b/src/server/api/release.ts @@ -1,12 +1,11 @@ import { Type } from "@sinclair/typebox"; import { FirRouteInput, FirRouteOptions } from "../util/routewrap.js"; -import {execa} from 'execa'; import fs from "fs"; import path from "path"; import git from "isomorphic-git"; import http from "isomorphic-git/http/node"; import {fileURLToPath} from 'url'; -const __filename = fileURLToPath(import.meta.url); +import { pico8 } from "../util/pico8.js"; const __dirname = fileURLToPath(new URL('.', import.meta.url)); const method = "POST"; @@ -19,8 +18,6 @@ const url = "/api/release"; const payloadT = Type.Any(); const repoPath = path.resolve(__dirname, "..", "..", "..", "repo"); -const picoDirPath = path.resolve(__dirname, "..", "..", "..", "pico8"); -const picoBinPath = path.resolve(picoDirPath, "pico8"); // const {stdout, } = await execa(picoBinPath, ["/home/dylan/.lexaloffle/pico-8/carts/candles/candles.p8", "-export", path.join(__dirname, "result.js")]); // const {stdout, } = await execa("ls", ["-la", picoDirPath]); @@ -53,12 +50,12 @@ const handler = async ({payload}: FirRouteInput) => { }); console.log("cloned"); console.log("read local manifest"); - console.log("pico exists: ", fs.existsSync(picoBinPath)); console.log("manifest exists: ", fs.existsSync(path.join(repoPath, "picobook.json"))); console.log("main exists: ", fs.existsSync(path.join(repoPath, manifest.main))); // const {stdout} = await execa("ls", ["-la", picoDirPath], {shell: true}); // console.log(stdout); - await execa(picoBinPath, [path.join(repoPath, manifest.main), "-export", path.join(repoPath, "result.js")]); + await pico8.export(path.join(repoPath, manifest.main), path.join(repoPath, "result.js")); + // await execa(picoBinPath, [path.join(repoPath, manifest.main), "-export", path.join(repoPath, "result.js")]); // await execa(picoBinPath, [path.join(repoPath, manifest.main), "-export", path.join(repoPath, "result.png")]); const js = await fs.promises.readFile(path.join(repoPath, "result.js"), "utf8"); // const png = Buffer.from(await fs.promises.readFile(path.join(repoPath, "result.png"))).toString("base64"); diff --git a/src/server/util/pico8.ts b/src/server/util/pico8.ts new file mode 100644 index 0000000..a5cfee8 --- /dev/null +++ b/src/server/util/pico8.ts @@ -0,0 +1,27 @@ +import fs from "fs"; +import path from "path"; +import {fileURLToPath} from 'url'; +import {execFile} from "child_process"; +const __dirname = fileURLToPath(new URL('.', import.meta.url)); + +const picoDirPath = path.resolve(__dirname, "..", "..", "..", "pico8"); +const picoBinPath = path.resolve(picoDirPath, "pico8"); + +const execPico = (args: string[]) => { + return new Promise((resolve, reject) => { + const options = {}; + execFile(picoBinPath, args, options, (error, stdout, stderr) => { + if (error) { + reject({error, stderr}); + } else { + resolve({stdout}); + } + }) + }); +} + +export const pico8 = { + async export(fileIn: string, fileOut: string) { + return await execPico([fileIn, "-export", fileOut]); + } +} \ No newline at end of file