RobLoach / node-raylib

Node.js bindings for Raylib
https://robloach.github.io/node-raylib/
Other
237 stars 20 forks source link

Do we need a CLI? Should it work different? #116

Closed konsumer closed 2 years ago

konsumer commented 2 years ago

Personally, I would probably not use the standalone CLI, I'd just use node, and use pkg or some other builder to make my own thing, if I needed to make it easier for non-node people.

If we do want to support it, we might be able to use pkg (or similar) to make a pre-built CLI that loads a specific path by default (like EXE_DIR/data/index.js) so you can rename the executable and get a ready-to-distro game. I imagine an entry-point, sort of like this, which would allow named files, but default to a predefined path:

const f = require('fs')
const path = require('path')
const raylib = require('raylib') // this triggers preload

// maybe make it a global?
global.raylib = raylib

let file = path.join(__dirname, 'data', 'index.js')
if (process.argv.length > 2){
  file = path.resolve(process.argv[2])
}

const source = fs.readFileSync(file)
process.chdir(path.dirname(file))
eval(source)
RobLoach commented 2 years ago

There's this one? https://github.com/RobLoach/node-raylib/blob/master/bin/node-raylib

node-raylib myapp.js
konsumer commented 2 years ago

I didn't realize that was just a script (I thought it was built, and had node embedded), so actually that works fine. Personally, I just require raylib, and use a regular project, then build with other tools (like pkg, etc) if needed, so the idea of a full build of a binary runtime seemed not great, but this totally resolves that (for posix.) It's not going to work on Windows, but I'm not really sure how to resolve that (other than making the bin in js and exposing in package.json's bin.) Oh, nevermind, I need to read a bit closer. It is already in node. That probly works ok on windows, too. I noticed the windows CLI tests are failing, but maybe that is a separate thing.

konsumer commented 2 years ago

I guess the only thing I might add is a default filename to that, so it works like love (main.lua) or other runtimes, if you have a certain-named file in a known-location (index.js/app.js/game.js or whatever) it will run it. That would make it so you can pkg it as a standalone game using the file as entry-point, or just distro the runtime + entry file, and to run it, you just run the script, as long as it has the right name.

I am a big fan of this structure:

node-raylib (the script)
data/
  index.js
  assets/

then you can rename node-raylib to mycoolgame and it kinda looks like a regular game. If you pkg (or whatever you use) it, and don't use require (file-read and eval) then it can be mycoolgame.exe and node doesn't need to be installed. I will setup a POC using that entry-point as a start. I am trying to think of it more like love, where you can kind of make it look like not a lua app, and just distro everything the end-user needs to run it.

I do see maybe the original value of this setup though. I kept rewriting demos to use ../../index.js instead of raylib for testing, but I could just use this. Running it like this makes that work, but the example doesn't have a relative path.

RobLoach commented 2 years ago

I think that's a good idea. If no input is given, try loading index.js, followed by data/index.js?

The other thing we could do in this is change the current working directory to the loaded file too, so that file loading resolves okay.

konsumer commented 2 years ago

The other thing we could do in this is change the current working directory to the loaded file too, so that file loading resolves okay.

Yep, agreed. it also will help follow the "folder is the root of the app structure" kind of thing, in a really simple way.

konsumer commented 2 years ago

I think this is good to close, if everyone is happy with it.

RobLoach commented 2 years ago

Like the updates!