Is this right in the use case of exec method of child_process? is there away to cody the envirorment along with the require module too?
Posted
by
L2L2L
on Programmers
See other posts from Programmers
or by L2L2L
Published on 2014-08-22T22:37:48Z
Indexed on
2014/08/25
16:28 UTC
Read the original article
Hit count: 1557
node.js
I'm learning node. I am using child_process to move data to another script to be executed. But it seem that it does not copy the hold environment or I could be doing something wrong. To copy the hold environment --require modules too-- or is this when I use spawn, I'm not so clear or understanding spawn exec and execfile --although execfile is like what I'm doing at the bottom, but with exec... right?-- And I would just love to have some clarity on this matter. Please anyone? Thank you.
parent.js -
"use strict";
var fs, path, _err;
fs = require("fs"),
path = require("path"),
_err = require("./err.js");
var url;
url= process.argv[1];
var dirname, locate_r;
dirname = path.dirname(url);
locate_r = dirname + "/" + "test.json";//path.join(dirname,"/", "test.json");
var flag, str; flag = "r", str = "";
fs.open(locate_r, flag, function opd(error, fd){
if (error){_err(error, function(){
fs.close(fd,function(){
process.stderr.write("\n" +
"In Finally Block: File Closed!" + "\n");});})}
var readBuff, buffOffset, buffLength, filePos;
readBuff = new Buffer(15),
buffOffset = 0,
buffLength = readBuff.length,
filePos = 0;
fs.read(fd, readBuff, buffOffset, buffLength, filePos,
function rd(error, readBytes){
error&&_err(error, fd);
str = readBuff.toString("utf8");
process.env.str = str;
process.stdout.write("str: "+ str + "\n" + "readBuff: " + readBuff + "\n");
fs.close(fd, function(){process.stdout.write(
"Read and Closed File." + "\n"
)});
//write(str);
//run test for process.exec**
var env, varName, envCopy, exec;
env = process.env,
varName,
envCopy = {},
exec = require("child_process").exec;
for(varName in env){
envCopy[varName] = env[varName];
}
process.env.fs = fs,
process.env.path = path,
process.env.dirname = dirname,
process.env.flag = flag,
process.env.str = str,
process.env._err = _err;
process.env.fd = fd;
exec("node child.js", env, function(error, stdout, stderr){
if(error){throw (new Error(error));}
});
});
});
child.js -
"use strict";
var fs, path, _err;
fs = require("fs"),
path = require("path"),
_err = require("./err.js");
var fd, fs, flag, path, dirname, str, _err;
fd = process.env.fd,
//fs = process.env.fs,
//path = process.env.path,
dirname = process.env.dirname,
flag = process.env.flag,
str = process.env.str,
_err = process.env._err;
var url;
url= process.argv[1];
var locate_r;
dirname = path.dirname(url);
locate_r = dirname + "/" + "test.json";//path.join(dirname,"/", "test.json");
//function write(str){
var locate_a;
locate_a = dirname + "/" + "test.json"; //path.join(dirname,"/", "test.json");
flag = "a";
fs.open(locate_a, flag, function opd(error, fd){
error&&_err(error, fs, fd);
var writeBuff, buffPos, buffLgh, filePs;
writeBuff = new Buffer(str),
process.stdout.write(
"writeBuff: " + writeBuff + "\n" + "str: " + str + "\n"),
buffPos = 0,
buffLgh = writeBuff.length,
filePs = buffLgh;//null;
fs.write(fd, writeBuff, buffPos, buffLgh, filePs-3,
function(error, written){
error&&_err(error, function(){
fs.close(fd,function(){
process.stderr.write("\n" +
"In Finally Block: File Closed!" + "\n");
});
});
fs.close(fd,
function(){process.stdout.write(
"Written and Closed File." + "\n");});
});
});
//}
err.js -
"use strict";
var fs;
fs = require("fs");
module.exports = function _err(err, scp, cd){
try{
throw (new Error(err));
}catch(e){
process.stderr.write(e + "\n");
}finally{
cd;
}
}
© Programmers or respective owner