2016-06-30 71 views
1

使用NodeJS,NPM和Gulp。Windows和Ubuntu的NodeJS exec()命令

我想構建一個運行JSDoc的gulp任務,它可以在Ubuntu和Windows上運行。

這工作在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); }); };

不用說,無論是工作在另一個。其他人如何解決這類問題?

回答

2

節點具有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 } };