3 var fs = require('fs');
4 var LRU = require('lru-cache');
5 var shebangCommand = require('shebang-command');
7 var shebangCache = new LRU({ max: 50, maxAge: 30 * 1000 }); // Cache just for 30sec
9 function readShebang(command) {
14 // Check if it is in the cache first
15 if (shebangCache.has(command)) {
16 return shebangCache.get(command);
19 // Read the first 150 bytes from the file
20 buffer = new Buffer(150);
23 fd = fs.openSync(command, 'r');
24 fs.readSync(fd, buffer, 0, 150, 0);
26 } catch (e) { /* empty */ }
28 // Attempt to extract shebang (null is returned if not a shebang)
29 shebang = shebangCommand(buffer.toString());
31 // Store the shebang in the cache
32 shebangCache.set(command, shebang);
37 module.exports = readShebang;