简体   繁体   English

Windows和Ubuntu的NodeJS exec()命令

[英]NodeJS exec() command for both Windows and Ubuntu

Using NodeJS, NPM, and Gulp. 使用NodeJS,NPM和Gulp。

I want to build a gulp task to run JSDoc that works on Ubuntu and Windows. 我想构建一个gulp任务来运行适用于Ubuntu和Windows的JSDoc。

This works on Ubuntu... 这适用于Ubuntu ...

var exec = require('child_process').exec;

return function(cb) {
  exec('node node_modules/.bin/jsdoc -c jsdoc-conf.json', function(err, stdout, stderr) {
    cb(err);
  });
};

And this works on Windows... 这适用于Windows ...

var exec = require('child_process').exec;

return function(cb) {
  exec('node_modules\\.bin\\jsdoc -c jsdoc-conf.json', function(err, stdout, stderr) {
    cb(err);
  });
};

Needless to say, neither works on the other. 不用说,两者都不起作用。 How do others solve this type of problem? 其他人如何解决这类问题?

尝试使用path.resolve ,它应该为您提供文件的完整路径,而不管平台如何。

Node has process.platform , which... "returns a string identifying the operating system platform on which the Node.js process is running. For instance darwin , freebsd , linux , sunos or win32 " Node有process.platform ,其中......“返回一个字符串,标识运行Node.js进程的操作系统平台。例如darwinfreebsdlinuxsunoswin32

https://nodejs.org/api/process.html#process_process_platform https://nodejs.org/api/process.html#process_process_platform

var exec = require('child_process').exec;

return function(cb) {
  if (process.platform === 'win32') {
    // Windows OS
  } else {
    // everything else
  }
};

Using path.resolve : 使用path.resolve

const exec = require('child_process').exec;
const path = require('path');

return function(cb) {
  let command = `node ${path.resolve('node_modules/.bin/jsdoc')} -c jsdoc-conf.json`;

  exec(command, function(err, stdout, stderr) {
    cb(err);
  });
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM