Alright, well this has had me going crazy but here is the bit hitch. I am working on a cli tool but would like to piggy back off of nodes shell to use their javascript parser. I would like to have something that starts a node shell with my npm package pre loaded and can be initialized with a file like lets say
$> myModule <fileName>
>
Once that is there I want to be able to run normal inputs like
> let a = 3;
> console.log(a);
3
But also be able to use custom funcitons from my module as well without having to require it into node.
> let a = myCustomeModule.class.function(input)
> console.log(a)
{
some: 'json',
object: {}
}
This is why it needs to pre load it. The issue is I can't seem to find any examples of anyone doing this.
Am I just going about this the wrong way, the main thing is to avoid having to write an entire node cli for this package.
I haven't tried this but I suspect this will work.
I believe this was originally created so you could interact with Node while a web server was running, so you could do live inspection of the contents.
You would need a utility to read/write to the socket, such as socat or nc
You could also add this to a shell script:
#! /bin/sh
node app.js &
nc -U /tmp/repl/realtime-101.sock
For more info: https://github.com/dshaw/replify
Sébastien Portebois
Software architect at Ubisoft
Hello there!
I'm not sure I got the question right, but I think I understood the main parts, and hopefully what follows should help you getting started. The main part is the REPL API from NodeJs.
In the sample below, I'll create 2 dumb modules in which I define custom functions, then I write some repl bootstrap to customize the way the interactive NodejS (its repl) is launched, and we'll see my modules are loaded automatically:
Let's start with the modules we want to load:
'use strict'; exports.echo = (a) => console.log(`echo from A ${a}`);'use strict'; exports.add = (a, b) => a + b;Then the magic happens here, in the bootstrap repl:
const repl = require('repl').start({useGlobal: true}); for (let myModule of ['moduleA', 'moduleB']) { repl.context[myModule] = require(`./${myModule}`); }(To keep things simple all these files are in the same folder, but you're free to complexify this at will)
Now, rather than just typing
$ nodeto enter the repl console, use that bootstrap script:$ node repl.jsAnd now you're in the interactive shell, as usual, but moduleA and moduleB are alreayd loaded :> moduleA.echo('test') echo from A test > moduleB.add(2, 3) 5You can even add a
#!/usr/env/bin nodeand do achmod +x repl.jsto call./repl.jsdirectly and enter your customized interactive shell.I hope I understood your question correctly, Hope this helps, Séb