EasyDSS流媒体解决方案之Nodejs调用bat或sh脚本

来源:互联网 发布:手机绘画的软件 编辑:程序博客网 时间:2024/06/05 14:08

Nodejs中如何调用bat或是sh脚本?

项目中会调用到bat和sh脚本,那么如何在Nodejs项目工程中使用呢?

1. 安装shelljs

npm install shelljs --save

2. 其他的相关依赖

npm install co --save

3. 封装异步方法

var shell = require('shelljs');exports.exec = function(command) {    return Promise.resolve({        then: function(onFulfill, onReject) {            shell.exec(command, function(code, stdout, stderr) {                onFulfill({ code: code, out: stdout, error: stderr });            })        }    });}

3. 同步调用执行

var ossshell = require('./shell.js');function demo(){    return Promise.resolve({    then: function(onFulfill, onReject) {            co(function*() {                    var command = "";                    if (platform.startsWith("win")) {                       //如果是windows                        command = `call trans_process.bat`;                    } else {                         //如果是linux                        command = `sh ./trans_process.sh`;                    }                    yield ossshell.exec(command);                    onFulfill();                }            }).catch(function(e) {                onReject(new Error(e));            })        }    });}

更多EasyDSS相关功能及版本下载

Web:www.easydss.com

EasyDSS技术交流群:560148162

阅读全文
0 0
原创粉丝点击