var exec = require('child_process').exec,path = require('path'),os = require('os'); fs = require('fs'); // HACK: to make our calls to exec() testable,// support using a mock sHell instead of a real sHell var sHell = process.env.SHelL || 'sh'; // support for Win32 outside Cygwin if (os.platform() === 'win32' && process.env.SHelL === undefined) { sHell = process.env.COMSPEC || 'cmd.exe'; } // Merges the current environment variables and custom params for the environment used by child_process.exec() function createEnv(params) { var env = {}; var item; for (item in process.env) { env[item] = process.env[item]; } for(item in params) { env[item] = params[item]; } return env; } // scriptFile must be a full path to a sHell script exports.exec = function (scriptFile,workingDirectory,environment,callBACk) { var cmd; if (!workingDirectory) { callBACk(new Error('workingDirectory cAnnot be null'),null,null); } if (!fs.existsSync(workingDirectory)) { callBACk(new Error('workingDirectory path not found - "' + workingDirectory + '"'),null); } if (scriptFile === null) { callBACk(new Error('scriptFile cAnnot be null'),null); } if (!fs.existsSync(scriptFilE)) { callBACk(new Error('scriptFile file not found - "' + scriptFile + '"'),null); } // transform windows BACkslashes to forWARD slashes for use in cygwin on windows if (path.sep === '\\') { scriptFile = scriptFile.replace(/\\/g,'/'); } // TODO: consider building the command line using a sHell with the -c argument to run a command and exit cmd = '"' + sHell + '" "' + scriptFile + '"'; // execute script within given project workspace exec(cmd,{ cwd: workingDirectory,env: createEnv(environment) },function (error,stdout,stderr) { // TODO any optional processing before invoking the callBACk callBACk(error,stderr); } ); };
以上是大佬教程为你收集整理的node.js – 从nodejs执行批处理文件全部内容,希望文章能够帮你解决node.js – 从nodejs执行批处理文件所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。