为需要节点模块创build工厂

我想创build基于所需的操作系统的Windows / Linux的工厂/或类似的方法,例如,如果使用Linux

var isLinux = /^linux/.test(process.platform); var isWin = /^win/.test(process.platform); if(isLinux){ var spawn = require('child-process'); }{ elseif(isWin) var spawn = require('cross-spawn') } module.export = spawn; 

我知道交叉产卵也是为Windows …我的问题是有一个更好的方式来写在节点(ver4.4),而不是只是如果

 switch(process.platform){ case 'windows': // code break; case 'linux': // code break; default: return new Error('give us a real OS pls') } 

要么

 process.platform === 'windows' ? //code windows : //code linux 

这里有更多的想法。 切换语句的替代方法。