How to add tab completion to a Nodejs CLI app - javascript

I want to add tab completion to a Nodejs CLI app (And preferably generate the tab completion dynamically).
I found a few npm modules but not sure how to really implement them:
https://github.com/hij1nx/complete
https://github.com/mklabs/node-tabtab
So what I am looking for is so I can have a nodejs file that is something like:
my-cmd create arg1 arg2
But then I might want to autocomplete like:
my-cmd cr<tab> -> create
Thanks!

Use omelette package that I built. If you have any questions, please contact me.

Edit - fast answer
After I answered, I kept reading tabtab source a bit and noticed that I can also run
pkgname completion install
to install the completion. since my environment was already dirty, I don't know if it actually did anything, but seems to me like it did..
Longer answer
#CameronLittle has given great documentation.
For the impatient, you can start by running
sudo bash -c 'pkgname completion > /etc/bash_completion.d/pkgname'
source /etc/bash_completion.d/pkgname
This will add completion to your current bash session.
As far as I know, new sessions will get the completion automatically.
To make the process seamless for user, you can use the install and postinstall hooks in package.json
https://docs.npmjs.com/misc/scripts
Make sure to not print anything by default. means running pkgname should result in no output, or otherwise it will not work.
important! install tabtab only from master
It seems tabtab has an annoying bug that was resolved in master but never got into a release..
The relevant commit to fix it is this:
https://github.com/mklabs/node-tabtab/commit/f8473555bf7278a300eae31cbe3377421e2eeb26
which handles completion for strings starting with --.
The commit if from february 2014, however the latest release as of (Jan. 2015) is 0.0.2 from Jan. 2014.. I assume there will not be more releases.
So if you want to get this fix, and you should(!), install tabtab only from master.
don't waste 2 hours figuring out what you did wrong like me :)
How did i reach this answer? TL;DR
While #CameronLittle's answer gives the explanation behind the scene, I would like to explain how to I reached the answer.
I tried using the package tabtab which has an explicit section about installing it. see https://www.npmjs.com/package/tabtab#completion-install
However, that didn't seem to work for me.
Looking at the code they instruct to add, I see the following process.argv.slice(2)[0] === 'completion' which made me run the command pkgname completion, which outputs something that starts with
###-begin-pkgname-completion-###
### credits to npm, this file is coming directly from isaacs/npm repo
#
# Just testing for now. (trying to learn this cool stuff)
#
# npm command completion script
#
# Installation: pkgname completion >> ~/.bashrc (or ~/.zshrc)
#
the words this file is coming directly from isaacs/npm repo made me wonder more. following the other answer here, I looked at /etc/bash_completion.d/npm - which showed the same exact content.. and so the comment.
I decided to run
pkgname completion > /etc/bash_completion.d/pkgname
however that requires sudo permissions and so becomes
sudo bash -c "pkgname completion > /etc/bash_completion.d/pkgname
and then, in order to apply it to current bash session I had to run
source /etc/bash_completion.d/pkgname
and voila! it works!
when I tried to open another terminal, it still worked, so I assume it will apply to all users. if not - you should add it to .bashrc or something..

I would just like to add that there is a
npm package yargs that enables bash-completion shortcuts for commands and options.
It has the option to output a .bashrc completion script. Bash completions are then enabled by sourcing the generated script.
It is currently an actively maintained package on npm with over a million downloads a month.

Related

How to debug/inspect hexo blog

I'd like to start a Nodejs debugger for my Hexo blog to understand how my theme works and possibly find a bug.
I needed 2 things to achieve this:
Install hexo-cli as a dev dependency rather than global. I used npm i hexo-cli --save-dev.
In package.json, under scripts, add a script called debug. I used this command: node --inspect=4300 ./node_modules/hexo-cli/bin/hexo server.
Then just use npm run debug and you're good to connect with a debugger to port 4300 (or whatever port you want to set in your command) and do line-to-line debugging etc.
One caveat is that with the --inspect setting, for some reason hexo is starting extremely slow (takes more than 2 minutes). I wonder what causes this.
Also, I haven't found a way to start hexo in a way that it generates pages dynamically. It would help with real time debugging.

How to run callback with jest --watch

I started to learn ReactJS yesterday (to be used in my next product), I am willing to set up my dev environment but now I'm stuck with Jest...
Having a bluetooth lightbulb on my desk (already op with scripts etc..), I want to get a red light when my tests launched with jest --watch fail (see create-react-app from FB devs here)
The problem is, I don't know how to run a callback after the tests, it seems like no one ran into this issue on the interwebz, no solution found yet for me.
Update:
I am currently using a log file to grep:
lamp.rb
def ci
if File.readlines("path/jest.log").grep(/failed/).any?
File.truncate('path/jest.log', 0)
fail_jest # This method updates my lightbulb :) (red blink)
end
rescue
puts 'No jest log found :('
end
Launching my jest tests like this: unbuffer npm run test |& tee tmp/jest.log
I am still looking for a better solution !
Thanks for your help
Your problem is not specific to React or Jest. When your run jest tests, you are basically running a Node/npm command and when the tests fail the process exists with an unsuccessful exit code. This is the same mechanism used to make automated CI builds fail when tests don't pass. So I'd suggest you start your research from there and depending on your lightbulb's API, it should be straight forward to make it fire events whenever the process fails regardless of the reason.

Can't generate webpack-assets.json with webpack-isomorphic-tools

I'm trying to learn React and the whole environment built around it. I do that by trying to construct my own dev-stack.
The problem I can't get across for a very long time is how to serve CSS/Images while not loosing a power of server rendering.
I've read a couple of tutorials and discovered webpack-isomorphic-tools
I've configured them and managed to get an images supported, sass (transformed to css) as well.
However, I came across an issue that my webpack-assets.json file is not generated, instead I see this output. ( I managed to get it generated on a 2nd run of npm start before this commit, but that was definitely not a way to go , but it showed that the plugin works when a file is present.)
$ npm start
> redux-universal-example#0.0.0 start /Users/janvorcak/learning2016
> node src/server/index.js
[webpack-isomorphic-tools] (waiting for the first Webpack build to finish)
[webpack-isomorphic-tools] (waiting for the first Webpack build to finish)
[webpack-isomorphic-tools] (waiting for the first Webpack build to finish)
[webpack-isomorphic-tools] (waiting for the first Webpack build to finish)
I understand the purpose of this file, but I can't really figure out why it's not generated at all.
Is there anything that I'm missing?
Here are the relevant files and a repository
https://github.com/jvorcak/universal-react-kit/tree/sass-loader
(sass-loader branch on my universal-react-kit repository)
configuration -
https://github.com/jvorcak/universal-react-kit/blob/sass-loader/webpack-isomorphic-tools-configuration.js
webpack.config.js -
https://github.com/jvorcak/universal-react-kit/blob/sass-loader/webpack.config.js
entry file when running a server https://github.com/jvorcak/universal-react-kit/blob/sass-loader/src/server/index.js
Could somebody please explain what is going on, I've read documentation, blogs, but I'm missing something here. Thank you.
The reason the assets file is not generated is because you have integrated webpack-dev-server into your server.js.
https://github.com/jvorcak/universal-react-kit/blob/master/src/server/server.js#L81
That's a wrong way to do it because in production you won't need webpack-dev-server and therefore its place is somewhere else.
In your case webpack-dev-server is meant to generate webpack-assets.json and this webpack-dev-server is being run after webpack-isomorphic-tools .server() method calls its callback, but it won't call its callback until it finds webpack-assets.json.
The answer is to run your webpack-dev-server in a separate process (you may want to refer to github.com/erikras/react-redux-universal-hot-example for an example of how to achieve that).
https://github.com/halt-hammerzeit/webpack-isomorphic-tools/issues/47
You may also like my very own boilerplate which can do all the fancy things
https://github.com/halt-hammerzeit/webapp

JSDoc setting up?

I am not that good at computers but am trying to use JSDoc for one of my projects,
The tutorial to get it up and running is here
http://usejsdoc.org/about-jsdoc3.html
I have downloaded the program from github, but now do not understand what I have to do. I have a bunch of files in a folder and dont know how to get it actually running for my project.
Could someone please give me a step by step instruction on how to actually get JSDoc working, how do I set it up, how do I use it etc.
I know this may be mundane to some of you, but hey we all gotta start somewhere right?
Well, are you using windows or GNU/Linux?
First, you have to follow the default tags to markup your source code, identifying your classes, methods, parameters, etc...
After that, you download the file here: https://github.com/jsdoc3/jsdoc
Extract it and then go to folder jsdoc-master.
Inside it, you have a script called jsdoc (with no extension). Only you have to do is execute it pointing the whole path to your .js file you want to create a documentation like this: ./jsdoc your_class.js
Then, in a couple minutes you'll have the output inside the 'out' folder. Open the .html file and bang! You have your documentation working pretty good.
Right here you can find some common tags to use in your code (as comments): http://usejsdoc.org/
This solution is for Unix based system. But, there is another one using nodejs (that you can run with windows, Linux, mac, etc...). First, download the nodejs here: http://nodejs.org/
Then go to this website to take a look at the package jsdoc: https://npmjs.org/
Then, go back to your terminal (in any operating system after installed node) and type: npm install -g jsdoc
The option -g means globally, so you have inside the main folder of your node packages and they are available for whatever project you have and you don't need to install it again and again...
Finally, you can just use this command: jsdoc path/to/your/file.js
And that's it! I hope it helps you.
Once you've extracted the file you downloaded off of github, navigate within the folder and run in a terminal:
./jsdoc
with the options you want.
If you want to display the help menu
./jsdoc --help
If you want to install the program on your system, assuming a mac or linux machine, use root user or sudo:
npm install --save -g [~/Downloads/jsdoc-3.2.2 or your path to the downloaded extracted files]

Is it possible to simulate keyboard/mouse event in NodeJS?

Imagine that a NodeJS module, when invoked from console, outputs some introductory messages and then waits for user input (click enter or esc). This module already has and does everything we require, except that - wait-for-user-input prompt. So we wonder (I'm personally very new to NodeJS) if it is possible to execute console module programmatically and trigger an input event on it, so that it doesn't wait and proceed with the job right away?
You could use possibly use RobotJS for this.
Example code:
var robot = require("robotjs");
// Type user's password or something.
robot.typeString("abc123");
As Jason mentioned you could use RobotJS for key simulation but there are couple of steps require to correctly build robotJS for Windows paltform:
You would need windows build tools so run npm install --global windows-build-tools (would take some time as it's around 120MB)
run npm install robotjs --save-dev
You're done!.
If this is for electron app then you would also require below 3rd step:
run npm rebuild --runtime=electron --target=1.7.9 --disturl=https://atom.io/download/atom-shell --abi=57
(1.7.9 is my electron --version and abi is for my corresponding node --version 8.7 installed, you can check abi version for node version here [look for NODE_MODULE_VERSION column])
node-key-sender library is an alternative to RobotJs if you just need to send keys to your operational system. It is cross platform and very small lib.
Install it with npm install --save-dev node-key-sender.
And send "enter" to the keyboard using:
var ks = require('node-key-sender');
ks.sendKey('enter');
Check out the documentation page: https://www.npmjs.com/package/node-key-sender.
Responding to #Venryx. They are right that robotjs is going to have a delay, especially if you have to load node first, however, if you already have node loaded, it may be worth trying out
robot.setKeyboardDelay(0)
The default setting for a delay is 10ms. This helped me tremendously.
I've tried robotjs and node-key-sender, but they cause a substantial amount of delay/stuttering per key-event. (especially noticeable when sending them frequently)
To resolve this, I found a way to use node-ffi-napi to call the Windows user32 SendInput function directly: https://stackoverflow.com/a/50412529/2441655
In my case at least, this achieved substantially better performance. (however, a drawback is that it only works on Windows, of course)

Categories

Resources