You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
33 lines
740 B
33 lines
740 B
4 weeks ago
|
'use strict';
|
||
|
|
||
|
const fs = require('fs');
|
||
|
const shebangCommand = require('shebang-command');
|
||
|
|
||
|
function readShebang(command) {
|
||
|
// Read the first 150 bytes from the file
|
||
|
const size = 150;
|
||
|
let buffer;
|
||
|
|
||
|
if (Buffer.alloc) {
|
||
|
// Node.js v4.5+ / v5.10+
|
||
|
buffer = Buffer.alloc(size);
|
||
|
} else {
|
||
|
// Old Node.js API
|
||
|
buffer = new Buffer(size);
|
||
|
buffer.fill(0); // zero-fill
|
||
|
}
|
||
|
|
||
|
let fd;
|
||
|
|
||
|
try {
|
||
|
fd = fs.openSync(command, 'r');
|
||
|
fs.readSync(fd, buffer, 0, size, 0);
|
||
|
fs.closeSync(fd);
|
||
|
} catch (e) { /* Empty */ }
|
||
|
|
||
|
// Attempt to extract shebang (null is returned if not a shebang)
|
||
|
return shebangCommand(buffer.toString());
|
||
|
}
|
||
|
|
||
|
module.exports = readShebang;
|