'sed' Command doesn't work with java script - javascript

I want to exectue following Shell Command with the Execute Option form Javascript:
"sed -i 4r<(sed '1,5!d' /etc/wpa_supplicant/wpa_template.template) /etc/wpa_supplicant/wpa_supplicant.conf"
Then i try this command in the shell console it works perfektly. But after starting it with the .js programm nothing is happen. I just want to copy the first 5 lines in template and add them to the config file.

You can do with exec from child_process
const exec = require('child_process').exec
exec("sed -i 4r<(sed '1,5!d' /etc/wpa_supplicant/wpa_template.template) /etc/wpa_supplicant/wpa_supplicant.conf", (err, stdout, stderr) => {
if (err) {
return console.error(err)
}
console.log(`stdout: ${stdout}`)
console.log(`stderr: ${stderr}`)
})

You can use the below code to execute any shell command and replace the o/p to a new file(create a new file) everytime you run it. And, you should use the line-reader module to get the line from the files.
npm install --save line-reader
const fs = require('fs');
const lineReader = require('line-reader');
const { exec } = require('child_process');
const cmd = "sed -i 4r<(sed '1,5!d' /etc/wpa_supplicant/wpa_template.template) /etc/wpa_supplicant/wpa_supplicant.conf"
exec(cmd, (err, stdout, stderr) => {
if (err) {
//some err occurred
console.error(err)
} else {
// the *entire* stdout and stderr (buffered)
fs.writeFile('file path', stdout, (err) => {
// throws an error, you could also catch it here
if (err) throw err;
// success case, the file was saved
console.log('file saved');
readFile1();
});
}
});
const readFile1 = () => lineReader.eachLine('file path', function(line) {
//make logic to take number of lines you want to take.
console.log(line);
});

Related

Node.js exec to build Marp markdowns doesn't work

I wrote this simple node.js script to build Marp markdown and convert them to PDF. But it doesn't do anything and doesn't terminate. I added a console.log("test") at the end I see that node.js doesn't wait for all the command executions to finish but exec also doesn't run the Marp build command.
If I run the "cmd" string from the terminal it all works. I think something is wrong with the way I use exec
var glob = require("glob");
var cp = require("child_process");
glob("lab*/*.marp.md", {}, function (err, files) {
files.forEach((file) => {
var destination = file.replace(".md", "");
var cmd = `marp ${file} --allow-local-files --pdf -o ${destination}.pdf`;
var dir = cp.exec(cmd, (err, stdout, stderr) => {
if (err) {
console.log("node couldn't execute the command");
return;
}
// the *entire* stdout and stderr (buffered)
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
});
dir.on("exit", function (code) {
// exit code is code
console.log(`finished building: ${file}`);
});
});
});
you can try the execSync() function It'll solve the issue
var glob = require("glob");
var cp = require("child_process");
glob("lab*/*.marp.md", {}, function (err, files) {
files.forEach((file) => {
var destination = file.replace(".md", "");
var cmd = `marp ${file} --allow-local-files --pdf -o ${destination}.pdf`;
var dir = cp.execSync(cmd, (err, stdout, stderr) => {
if (err) {
console.log("node couldn't execute the command");
return;
}
// the *entire* stdout and stderr (buffered)
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
});
dir.on("exit", function (code) {
// exit code is code
console.log(`finished building: ${file}`);
});
});
});

How to run a jar file in javascript and have it return a json

I have a jar file that returns a json. How can I run it and take the output through javascript?
There is a no browser side java compiler i guess, so you must use javascript on the backend side, i suggest use nodejs
const { exec } = require('child_process');
exec('java -jar /file/to/path.jar',(error, stdout, stderr) => {
console.log(stdout);
console.log(stderr);
if (error !== null) {
console.log(`exec error: ${error}`);
}
});

LSOpenURLsWithRole() failed for the application /location/name.app with error -10810

I'm trying to open application using nodejs (I'd like this process to be workable in cross-platform , MAC/Windows ..etc
here's the code
const { exec } = require('child_process')
const fs = require('fs')
// giving the required permissions , not really sure if 777 is the right choice
fs.chmodSync(appPath,777);
exec("open -a " + path , (err, stdout, stderr) => {
console.log(err);
console.log(stdout);
console.log(stderr);
});
console.log(err); // logs this error
{ Error: Command failed: open -a /location.app
LSOpenURLsWithRole() failed for the application /location.app with error -10810
when executing the same command in terminal (out of node) , it fires the same issue

Run an installer inside my program

How can I run an installer of an independent program (exe file) from inside of my code?
The idea in general is program which install other programs.
Thanks!
Barak
use exec of node.js
const { exec } = require('child_process');
exec('path/to/exe', (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
});

Running a command with gulp to start Node.js server

So I am using gulp-exec (https://www.npmjs.com/package/gulp-exec) which after reading some of the documentation it mentions that if I want to just run a command I shouldn't use the plugin and make use of the code i've tried using below.
var exec = require('child_process').exec;
gulp.task('server', function (cb) {
exec('start server', function (err, stdout, stderr) {
.pipe(stdin(['node lib/app.js', 'mongod --dbpath ./data']))
console.log(stdout);
console.log(stderr);
cb(err);
});
})
I'm trying to get gulp to start my Node.js server and MongoDB. This is what i'm trying to accomplish. In my terminal window, its complaining about my
.pipe
However, I'm new to gulp and I thought that is how you pass through commands/tasks. Any help is appreciated, thank you.
gulp.task('server', function (cb) {
exec('node lib/app.js', function (err, stdout, stderr) {
console.log(stdout);
console.log(stderr);
cb(err);
});
exec('mongod --dbpath ./data', function (err, stdout, stderr) {
console.log(stdout);
console.log(stderr);
cb(err);
});
})
For future reference and if anyone else comes across this problem.
The above code fixed my problem. So basically, I found out that the above is its own function and therefore, doesn't need to:
.pipe
I thought that this code:
exec('start server', function (err, stdout, stderr) {
was the name of the task I am running however, it is actually what command I will be running. Therefore, I changed this to point to app.js which runs my server and did the same to point to my MongoDB.
EDIT
As #N1mr0d mentioned below with having no server output a better method to run your server would be to use nodemon. You can simply run nodemon server.js like you would run node server.js.
The below code snippet is what I use in my gulp task to run my server now using nodemon :
// start our server and listen for changes
gulp.task('server', function() {
// configure nodemon
nodemon({
// the script to run the app
script: 'server.js',
// this listens to changes in any of these files/routes and restarts the application
watch: ["server.js", "app.js", "routes/", 'public/*', 'public/*/**'],
ext: 'js'
// Below i'm using es6 arrow functions but you can remove the arrow and have it a normal .on('restart', function() { // then place your stuff in here }
}).on('restart', () => {
gulp.src('server.js')
// I've added notify, which displays a message on restart. Was more for me to test so you can remove this
.pipe(notify('Running the start tasks and stuff'));
});
});
Link to install Nodemon : https://www.npmjs.com/package/gulp-nodemon
This solution has stdout/stderr shown as they occur and does not use 3rd party libs:
var spawn = require('child_process').spawn;
gulp.task('serve', function() {
spawn('node', ['lib/app.js'], { stdio: 'inherit' });
});
You can also create gulp node server task runner like this:
gulp.task('server', (cb) => {
exec('node server.js', err => err);
});
If you want your console to output everything that the child process outputs, as well as pass to the child process all environment variables you already have set:
const exec = require('child_process').exec;
function runCommand(command, cb) {
const child = exec(command, { env: process.env }, function (err) {
cb(err);
})
child.stdout.on('data', (data) => {
process.stdout.write(data);
});
child.stderr.on('data', (data) => {
process.stdout.write(`Error: [${data}]`);
});
}
Note that both out and err write to stdout, this is intentional for my case but you can adapt to whatever you need.

Categories