readShebang.js 549 B

1234567891011121314151617181920212223
  1. 'use strict';
  2. const fs = require('fs');
  3. const shebangCommand = require('shebang-command');
  4. function readShebang(command) {
  5. // Read the first 150 bytes from the file
  6. const size = 150;
  7. const buffer = Buffer.alloc(size);
  8. let fd;
  9. try {
  10. fd = fs.openSync(command, 'r');
  11. fs.readSync(fd, buffer, 0, size, 0);
  12. fs.closeSync(fd);
  13. } catch (e) { /* Empty */ }
  14. // Attempt to extract shebang (null is returned if not a shebang)
  15. return shebangCommand(buffer.toString());
  16. }
  17. module.exports = readShebang;