How do I set up a preload file for node? - node.js

Is there a way to preload some file before each time I run node (interactively), just like .vimrc, .bash_profile, etc.?
I use node mainly interactively, and I use the module CSV a lot, is there a way to avoid typing require('./csv') every time I start node?

Create an initialization file (for example ~/.noderc):
var csv = require('csv');
// put a blank line at the end of the file
Now add this line to your shell config (.bashrc / .zshrc / whatever shell you use):
alias nodei="cat ~/.noderc - | node -i"
Voilà!

#Ilan Frumer provided one way to do it. I think I'll give another choice here: build a REPL of your own.
From their documentation. You can find a way to write a repl of your own. You can add whatever scripts before and after the interations of it, and even use some advance API's.
For example, I created a file called .noderc.js under ~ as follows
repl = require('repl');
myFunc = function(){
console.log("Hello, there!");
};
repl.start("> ");
And you can go ahead and alias nodei="node ~/.noderc.js",
$ nodei
> myFunc()
Hello, there!
undefined

Related

Node.JS reading data from console command

I remember using something before in node.js that would allow me to run a command like
node appname.js text goes here
and then read the "text goes here" part with something like
console.log(console.text)
I can't remember what it is, and can't find it in any searches. Was this a real thing, or just me dreaming?
You can use process.argv to console the input from command line.
If you run below command in terminal/command line:
node appname.js text goes here.
You can print the command line arguments by:
console.log(process.argv)
Output of above console will be:
['node',
'/home/user/path/to/appname.js',
'text',
'goes',
'here' ]
If you dont want first two text, you can use:
console.log(process.argv.slice(2))
Output of above console will be:
['text',
'goes',
'here' ]
Read this link for more info.
Hope this helps you out!!!
Well there is lot's ways/packages around for reading from arguments.
the nodejs process is the base of it so check here
And also as i said lot's of packages there for parsing arguments.
yargs is one of them, minimist is also a populer one as far as i know.
If you don't want t use a package basicly it starts like this:
// inside node file
const args = process.argv.splice(2);
console.log(args);
// we are splice'ing from 2 cause
// process.argv[0] is your node-path
// process.argv[1] is your js file's full path
// Most of the time we are not using those so :)
So hope these would work for you ☺

Prereload script into node interactive mode

Is it possibille to run node.exe, pipe a text into it, and continue the interactive session?
I want to create a shortcut bat (or bash) file for editing my database.
Usually this is what I'm doing:
$ node
>var db=require('mydb')
>db.open('myserver')
>//Now I can start access the db
>db.query...
I want to do something like that:
$ node -i perDefinedDb.js
>db.query(.... //I don't want to define the DB each time I run the node.exe
I tried some like that:
echo console.log(a) | node.exe
This is the result:
3
And the program is Finish. I want to continue the node REPL after piping something into.
In Other Words:
I want to be able to use my DB from node REPL, without defining it each time.
Launch the REPL from your js file and you can give the context you want:
const repl = require('repl');
var db = require('mydb');
db.open('myserver');
repl.start('> ').context.db = db;
Now you just have to run this file (node myREPL.js) and you can REPL as usual.

How to create a Perl debugger

I wrote a bunch of functions for the Perl debugger, but every time I want to test them I need to do export PERL5DB="my code". Is there a way I can set it to a file and then use it?
I tried adding a file myperldb.pl in /usr/lib/perl/, but it doesn't allow me. That's because I am not the administrator user. So is there a workaround to it?
Make a copy of /usr/lib/perl/perl5db.pl in some other directory, change it to your heart's content, and debug your scripts with
perl -I/dir/that/contains/your/perl5db.pl/copy/ -d your_script.pl
The -I<dir> switch will get perl to look in the directory you choose to find libraries before it looks in its default directories.
That's what the .perldb file is for.
You may use this command:
PERL5DB="BEGIN{ require 'my_debugger.pm' }" perl -d script.pl
$cat my_debugger.pm
package DB;
sub DB {
}
sub sub {
print "$DB::sub called\n";
return &$DB::sub;
}
1;
You may find more information here.

Find command in vim gets incomplete path

In vim when I'm using :find to open another file, it misses the first component of the relative path.
For example, if I'm looking for a file that's in:
./foo/bar/file.txt
I'll type
:find **/file.txt
It finds the file but then tries to open
bar/file.txt
It works correctly if I type
./**/file.txt
But I'm lazy and don't want to type that much. Is there some config I'm missing that will correctly locate and open this path?
My Solution
I simply appended the main source code dir to my path
exec "set path^=src/**"
Is your 'path' set? That (IMO) is a pretty handy way to keep from even typing the **/ bit.
In my setup, there's an environment variable that defines which project I'm currently in so I use that and construct a path with that as the root. In a nutshell:
let s:rootdir = $PROJECT_DIR
let s:path = 'src/**;' . s:rootdir . ',scripts/**;' . s:rootdir
execute "set path=" . s:path
Then I can just :find a_file.txt and it searches my src hierarchy then my scripts hierarchy for the file.

How to use ARGV in cucumber?

How can i whrite argv. in cucumber-test?
I want, that my test run at localhost:3000 and than on productions (live).
I want this with argv. to define.
ARGV.each do |s|
if s == `-d`
#text = `localhost:3000`
elsif s == `-p`
#text = `www.world.com`
end
end
From a quick test, it looks like you can include your code as-is in your env.rb file. The #test variable would then be available in all your steps.
(I am not sure if it is a best practice, but it does work.)
I would recommend Fig Newton to solve that kind of problem. You can have many different environments that you can call at execution time. It's easy.
You can create bin directory and put there cucumber.rb file with your ARGV logic.
Then when you run smth like:
ruby bin/cucumber.rb -u 'http://www.ololo.com'
This script will :
setup you test environment ( set #url or write to env.yml to 'http://www.ololo.com')
run 'cucumber' command.
Or you can use this: How do I specify the browsers in my cucumber.yml files?

Resources