nodejs print file with child-process - javascript

I'm learning nodejs and I want to send some file to the printing queue.
I tried elctron-printer and node-printer modules but actually they do not work proprly (can't detect printer with printer.list command for example). Now I'm trying to make it with child_process module and I want to know is there any posibility to start file with its associated application with "print" argument like a python can do it?
For example, this is a code sample of file execution with nodejs:
var childProcess = require('child_process');
childProcess.exec('start printme.txt', function (err, stdout, stderr) {
if (err) {
console.error(err);
return;
}
console.log(stdout);
process.exit(0);// exit process once it is opened
})
Unfortunately it seems that "print" argument is invalid for this code.
And this is a code sample for python and it works fine on windows:
import os
os.startfile('printme.txt', 'print')
All in all I hope that there is possibility to emulate system commands with nodejs.
Otherwise I will have to execute python script via nodejs just for file printing, something like this:
let python = spawn('python', [path.join(app.getAppPath(), '..', 'python_scripts/print_file.py'])
But it is terrible way to do it.

Related

Can we call powershell command from browser extension in Javascript?

I know this is security-wise an absolute No-Go. But my customer is going to use a SAPUI5 developed application in the browser within the intranet upon which my preinstalled browser extension will be invoked (using "matches") which should simply collect the PRINTER names on the local workstation and supplies back to the application which is to be used for further processes. I have the below node js code which return the printernames but how to run this in browser engine.
const { exec } = require('child_process');
exec('get-printer | Select-Object Name | ConvertTo-Json', { 'shell': 'powershell.exe' }, (error, stdout, stderr) => {
console.log(stdout);
})
You can't. You will need to expose your nodeJS code as an API which can be called by the browser environment. You could try exposing it as a REST endpoint inside a nodejs express app.
Check this example: https://www.geeksforgeeks.org/how-to-connect-node-js-with-react-js/

How to run child_process.exec correctly on an ajax request?

There is a server, that I have an access to, but do not have ownership on. It serves a node js / express application on a default port 3000. There are several scripts, that are usually run either manually from the terminal or by cron job. What I want to do is to have a button on the client-side and make an ajax request to a certain route and execute a node js command with inline arguments. For example:
node script.js 123
All routes are set and working. I have a CliController file that handles requests and has to run the command above. Currently I am using the following code:
cp.exec(`node script.js ${ip}`, function (err, stdout, stderr) {
if (err) {
console.log(err);
}
console.log(stdout);
console.log(stderr);
});
The script.js file is in root folder of the project, but the project itself was built by using express-generator and is being served using node bin/www command. There is a service/process on the server that runs nodemon to restart this project if it fails as well. Therefore I do not have access to output of that particular process.
If I run the command above in the terminal (from the root folder of the project, to be precise), it works fine and I see the output of the script. But if I press the button on the webpage to make a request, I am pretty sure that the script does not execute, because it has to make an update to database and I do not see any changes. I also tried to use child_process.spawn and child_process.fork and failed to get it working.
I also tried to kill nodemon and quickly start the project again to the see console output. If I do this, everything works.
What am I doing wrong ?
The process invoked may be in a blocking state, hence the parent script is simply waiting for the children process to terminate, or return something.
We can avoid this behaviour right into the shell command, by adding & (ampersand control operator) at the end.
This makes a command running in the background. (Notice, you can still control the children(s) process using the PID's and POSIX signals, this is another subject, but very related and you might find it very handy pretty soon).
Also notice that killing/stopping the parent script will also kill the children(s). This can be avoided using nohup.
This is not linked to JavaScript or node.js, but to bash, and can be used with anything in the shell.
cp.exec(`node script.js ${ip} &`, function (err, stdout, stderr) {
if (err) {
console.log(err);
}
console.log(stdout);
console.log(stderr);
});
Bash reference manual

Integrate cli in vsCode terminal

For an extension that I am working on I would like to integrate a separate CMD window that is started by a different program in the terminal of vscode, the same way it is done currently in Emacs.
The way it currently works is that I can start the program from vsCode using the following code:
const { exec } = require('child_process');
//execCommand = "C:/.../.../gis.exe -a c:/.../.../alias start"
exec(execCommand, (err, stdout, stderr) => {
if (err)
return console.error(err);
else
console.log(stdout);
});
When this part of the code is run the program starts and then opens a separate CLI window. I would like this CLI window to be integrated in the native terminal of vscode.
Any way to "catch" the CLI window or redirect the output/input of the CLI window?
Not long ago I build a a little script that did the same thing. While I cannot share my code, the approach I took was to make my code to simulate JS key press, which will trigger the key command for opening the terminal (Ctr+`) and it worked. Hope that helps.

Executing python command line tools from NodeJS

What would be the best way to execute command line tools from nodejs to python? I'm working on a app that can generate blockchain certificates.
https://github.com/blockchain-certificates/cert-tools
This is a python based command line tool, where you can generate blockchain certificates. I have followed the steps and everything is working fine in the virtual env. But now I want to know how to implement this in to a app, where I can run the command line tools external from NodeJS. Is this possible? I have found some libraries where you can run python script from nodejs. Example I have used python shell. Whenever I run the setup script, I get missing file errors.
Can someone guide me what the best way will be to solve this problem. Thanks in advance.
You can use python shell in node, you would go about using it somewhat like the following:
var PythonShell = require('python-shell');
PythonShell.run('my_script.py', function (err) {
if (err) throw err;
console.log('finished');
});
for more documentation visit their github repository, hope i could help :)
You should just be able to execute the commands using the child_process.exec() function.
Make sure to only use the file once the python script has finished.
child_process.exec('py path/to/script.py arg1 arg2', (err, stdout, stderr) => {
if (err) {
console.error(err);
return;
}
console.log(stdout);
// check if generated file exists
let fileExists = require("fs").existsSync("/path/to/generated/file");
if (fileExists) {
// do something with the file
}
});

JavaScript - Write Variable to a .txt file and then load it

I'm working on a Discord Bot that has a variable 0-9999.
I need to be able to store that variable (let's say 9) into a .txt file,
and then whenever the Bot starts, it reads the file and sets the variable as
the first line of the text document.
So example:
Variable is 9. 9 is written to a text document on the first line.
Whenever I start the bot, Variable is set to the first line of text
document (9)
Not entirely sure how Discord bots work, but I know Discord is made with Electron and Node.js, so you could probably use the file system package (https://www.npmjs.com/package/fs)
Install it with:
npm install fs --save -g
You can read from the file like this:
// File system stuff.
var fs = require("fs");
// Get the text file and load it into a variable.
var file = fs.readFileSync("path/to/my/text/file.txt", "utf8");
And you can write to the file like this:
// Write the file
fs.writeFile("path/to/my/text/file.txt", myVariable, function (err) {
// Checks if there is an error
if (err) return console.log(err);
});
why not instead use cookies? If you're writing something for the browser you shouldn't store data in text files on the client's computer. If you are using nodejs for server code you could write text files but it would be better to use some sort of database.

Categories