簡體   English   中英

Windows和Ubuntu的NodeJS exec()命令

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

使用NodeJS,NPM和Gulp。

我想構建一個gulp任務來運行適用於Ubuntu和Windows的JSDoc。

這適用於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);
  });
};

這適用於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);
  });
};

不用說,兩者都不起作用。 其他人如何解決這類問題?

嘗試使用path.resolve ,它應該為您提供文件的完整路徑,而不管平台如何。

Node有process.platform ,其中......“返回一個字符串,標識運行Node.js進程的操作系統平台。例如darwinfreebsdlinuxsunoswin32

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
  }
};

使用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