Including and running scripts/binaries in a compiled binary #27115
-
|
Since it is possible to embed assets files in Demo setup: └── bundle-bin/
├── shell-script.sh # Simple shell script containing `echo "Hello from shell script!"`
├── deno-bin.ts # Source file containing `console.log("Hello from Deno bin!");`
├── deno-bin # Deno compiled binary printing the console log
└── main.ts # See below
import { mergeReadableStreams } from "jsr:@std/streams/merge-readable-streams";
console.log(import.meta.dirname);
// => /var/folders/wn/50d349pn6zn5y2snzf_87nhw0000gn/T/deno-compile-bundled-bin/bundled-bin
for (const file of Deno.readDirSync(`${import.meta.dirname}/`)) {
console.log(JSON.stringify(file));
}
// => {"name":"deno-bin","isFile":true,"isDirectory":false,"isSymlink":false}
// => {"name":"main.ts","isFile":true,"isDirectory":false,"isSymlink":false}
// => {"name":"shell-script.sh","isFile":true,"isDirectory":false,"isSymlink":false}
const shellScript = `${import.meta.dirname}/shell-script.sh`;
const denoBin = `${import.meta.dirname}/deno-bin`;
const process = new Deno.Command(`./${denoBin}`, { // or `shellScript`
stdout: "piped",
stderr: "piped",
}).spawn();
mergeReadableStreams(process.stdout).pipeTo(Deno.stdout.writable);
mergeReadableStreams(process.stderr).pipeTo(Deno.stderr.writable);deno compile -A --include deno-bin --include shell-script.sh -o bundled-bin main.ts
./bundled-bin
# => error: Uncaught (in promise) NotFound: Failed to spawn '/.../bundled-bin/var/folders/wn/50d349pn6zn5y2snzf_87nhw0000gn/T/deno-compile-bundled-bin/bundled-bin/deno-bin': No such file or directory (os error 2): No such file or directory (os error 2): No such file or directory (os error 2)
# => at spawnChildInner (ext:runtime/40_process.js:184:17)
# => at spawnChild (ext:runtime/40_process.js:208:10)
# => at Command.spawn (ext:runtime/40_process.js:483:12)
# => at file:///var/folders/wn/50d349pn6zn5y2snzf_87nhw0000gn/T/deno-compile-bundled-bin/bundled-bin/main.ts:10:4My goal would be to compose multiple, independently compiled applications into a single binary, and maybe go even further by adding some kind of supervision logic for the embedded applications. I don't really know how Deno binaries are actually crafted or if this is even remotely possible. Am I completely out of my mind with this idea or is there a way to make this work? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
I had a similar need for embedding executables as I am experimenting bundling a small webview executable with my Deno application. The workaround I came up with is to first write the executable to the filesystem before running const file = Deno.readFileSync(import.meta.dirname + "/webview/build/bin/webview-linux-x86_64")
Deno.writeFileSync(`/tmp/webview`, file, { mode: 0o755 })
new Deno.Command(`/tmp/webview`).spawn()Alternative example using file stream const file = await Deno.open(import.meta.dirname + "/webview/build/bin/webview-linux-x86_64")
await Deno.writeFile("/tmp/webview", file.readable, { mode: 0o755 })
new Deno.Command("/tmp/webview").spawn()Reminder to deno compile --allow-read --allow-write --allow-run --include "webview/build/bin/webview-linux-x86_64" main.tsYou could look into |
Beta Was this translation helpful? Give feedback.
I had a similar need for embedding executables as I am experimenting bundling a small webview executable with my Deno application. The workaround I came up with is to first write the executable to the filesystem before running
Deno.Command.Alternative example using file stream
Reminder to
--include…