2017-12-07 5 views
0

私のnodejsスクリプトからインストールされているものはどれもgccまたはclangでなければなりません。両方ともインストールされている場合は、両方を見つける必要があります。どうやってするか?インストールされているgccまたはclangまたはその両方を見つけよう

bashでは通常、which gccと入力してください。私はprocess.env.PATH.split(':').map(......fs.stat()......)を列挙できると知っていますが、組み込みの方が簡単ですか?

+0

あなたが呼び出すことができるシステムは次のようにコマンド 'のNode.jsから使用which' [' child_process'(https://nodejs.org/api/child_process.html) API。 –

+0

[** command-exists **](https://www.npmjs.com/package/command-exists)モジュールを確認してください – codtex

答えて

0
var fs = require('fs') 
var path = require('path') 

function find_binaries(binaries) { 
    var dirs = process.env.PATH.split(path.delimiter) 
    var result = {} 
    binaries.forEach(b=>result[b] = false) 
    dirs.forEach(dir => { 
     try { 
      binaries.map(binary => { 
       var f = dir + path.sep + binary 
       var s = fs.statSync(f) 
       result[binary] = true 
      }) 
     } 
     catch (e) { 
      return 
     } 
    }) 
    return result 
} 

exports = find_binaries 

console.log(find_binaries(['clang', 'gcc'])) 

戻り値:

{clang: true, gcc: false} 
関連する問題