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

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)

Related

How to deploy Javascript app that runs from command line?

I have a simple CLI application written in Javascript using Node that is for internal use by a small team. It runs in the Linux terminal as a CLI app. The app consists of a single ".js" file and requires a few Node packages. The problem I face now is how to deploy it to our internal team using a simple method that fits with our routine process of keeping end user computers updated.
Our app needs to be installed once per workstation / laptop and to be available to all users on that computer. Any user should be able to open a terminal and enter the command to run the app.
It seems a lot of people have discussed using Javascript for shell programming, but this issue of deploying the completed app is not widely discussed. I have not found anything on the topic. So far I have been recommended solutions that are appropriate for either development environments or web servers.
This app is not a web app and it is not deployed on a server. It needs to run offline. I am also not asking about developing or maintaining the app on a development workstation.
The installation process should ideally be as about simple as installing a shell script in /usr/local/bin and setting permissions so all permitted users on a computer can run it. We are looking for an installation method like this:
copy the Javascript file only once to each computer (to a location on the $PATH) and make sure the Node packages are available globally on that computer.
I specifically want to avoid having to do an npm install for each user account on each computer.
I also want to avoid having to update Node packages for each user account on each computer.
A developer will keep the app updated so it is always compatible with the latest version of the Node packages, and all computers it is deployed on will always have the latest versions of those packages installed.
One specific problem I encountered is discussed here, but the answers assume a different set of requirements (such as the need for "multiple applications running on different package versions").
For those requirements, if the actual problem is solving the EACCESS error (you should edit the question to include that information), then you should look at the permissions of all directories, and make sure that the user account that manages node packages on each computer has correct permissions.
One way to do that is to give /usr/local a special group, set the sticky bit with chmod (see man chmod), and use chgrp -R on the existing tree.
Then make the installing account a member of that group, and don't use sudo for npm install -g.
(Never using sudo for installations into /usr/local has the additional advantage that you can't accidentally install something somewhere else, for example because you didn't set paths in this local package source correctly.)
We are using these two approaches for similar deployments:
the programs live on a specific network mount. All users can run the same package from there. The developer only updates this package. No copying to local machines.
we use a simple deployment script which runs on all machines on logon. It pushes and copies the latest version to the local machine.

Sails.js doesn't work, 'sails' is not recognized

'sails' is not recognized as an internal or external command,
operable program or batch file.
So basically I use Windows 10 and I need Sails up and running. I saw on YT people working with that with no problems. I really wanna use Sails.js because it's amazing but I cannot use it.
I don't know what's wrong. I'm doing everything like they say to do. I've installed it globally through npm and of course, I have Node installed and still, I have that error...
What should I do?
It looks like you are missing the path to the sails script in the PATH environment variable. What you can do is find where your sails have been installed:
dir sails.cmd /s
After you find it, open windows menu and type directly 'env' and then select 'Edit the system environment variables'. In the opened window click on the 'Environment variables...' button and add the path to the 'Path' variable.
However, a more preferable way is to uninstall everything (node included) and use the nvm (Node Version Manager) program to get the node itself. You can find it here:
https://github.com/coreybutler/nvm-windows/releases
Then use it to get the node version you want. It is a very useful tool to quickly change between node versions. After that install sails as usual and you should be good to go.

Is Yarn a wrapper over npm?

I'm aware of the benefits that Yarn brings about and what it does that npm doesn't. I do have some doubts however:
Does Yarn use npm under the hood for some of the other commands that retain the same behaviour as npm?
If no, is there a reason why they were re-implemented in yarn and are there downsides to this approach?
No, it is a rewrite.
Rather than continue building infrastructure around the npm client, we
decided to try looking at the problem more holistically. What if
instead we attempted to build a new client that addressed the core
issues we were experiencing? Sebastian McKenzie in our London office
started hacking on this idea and we quickly became excited about its
potential.
-- https://code.facebook.com/posts/1840075619545360
It offers the same API (without some shortcuts if you notice). This is (I am guessing) because the original interface was simple enough and to ensure adoption.
It is much faster, solves typical problems of npm (undeterministic deploys mainly). This can be only achieved with a top-down rewrite, they also changed how deps are installed (fetch, calculate, download, link) so the logic is different.
The downsides of this approach is that it requires a huge amount of work. Normally you would just PR over the main repo, but given they wanted a whole different thing and Facebook has the means, they decided going solo.
Another big downside is that its benefits won't be immediately available for all the npm older users, since it is not an upgrade and it does not come installed with node.
I had a look at Yarn's source code and found that there was custom code written for each command: https://github.com/yarnpkg/yarn/tree/master/src/cli/commands.
I guess the downside to this approach is that in future when npm adds new commands, the Yarn team would have to track the changes and manually implement them.

Cannot run Meteor js 1.1 on windows 8.1

I have tried to install meteor js 1.1 on windows 8.1 with the official installer:
https://github.com/meteor/meteor/wiki/Preview-of-Meteor-on-Windows
But even after restarting my computer, when I type the command meteor in the command prompt I get the error command not found.
Even after a search on my computer to find the installation folder of meteor, I am unable to find the answer. However, in the add/remove program utility, I can see meteor.exe.
As a note, I already successfully installed meteor before, and it used to run smoothly. In the meantime, I have removed and re-installed node, and it seems that since this moment the command meteor does not work anymore. Hence my attempt to re-install it.
Does anyone have any idea about how to troubleshoot my issue ?
I just solved my issue by using the windows command prompt instead of the git bash
It appears that only windows command prompt is supported so far.
You can try to manually add Meteor to your Path.
Go to "Environment variables" (you can find it by doing a simple search in your welcome screen) and add the following entry:
C:\\Users\\**your_user_name**\\AppData\\Local\\.meteor
Do not forget to separate this entry from other entries by a semicolon ;.

How to add tab completion to a Nodejs CLI app

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.

Categories

Resources