• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3var fs = require('fs');
4var LRU = require('lru-cache');
5var shebangCommand = require('shebang-command');
6
7var shebangCache = new LRU({ max: 50, maxAge: 30 * 1000 });  // Cache just for 30sec
8
9function readShebang(command) {
10    var buffer;
11    var fd;
12    var shebang;
13
14    // Check if it is in the cache first
15    if (shebangCache.has(command)) {
16        return shebangCache.get(command);
17    }
18
19    // Read the first 150 bytes from the file
20    buffer = new Buffer(150);
21
22    try {
23        fd = fs.openSync(command, 'r');
24        fs.readSync(fd, buffer, 0, 150, 0);
25        fs.closeSync(fd);
26    } catch (e) { /* empty */ }
27
28    // Attempt to extract shebang (null is returned if not a shebang)
29    shebang = shebangCommand(buffer.toString());
30
31    // Store the shebang in the cache
32    shebangCache.set(command, shebang);
33
34    return shebang;
35}
36
37module.exports = readShebang;
38