Front end of the Slack clone application.

readShebang.js 894B

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