Last change
on this file since bdd6491 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
740 bytes
|
Rev | Line | |
---|
[6a3a178] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | const fs = require('fs');
|
---|
| 4 | const shebangCommand = require('shebang-command');
|
---|
| 5 |
|
---|
| 6 | function readShebang(command) {
|
---|
| 7 | // Read the first 150 bytes from the file
|
---|
| 8 | const size = 150;
|
---|
| 9 | let buffer;
|
---|
| 10 |
|
---|
| 11 | if (Buffer.alloc) {
|
---|
| 12 | // Node.js v4.5+ / v5.10+
|
---|
| 13 | buffer = Buffer.alloc(size);
|
---|
| 14 | } else {
|
---|
| 15 | // Old Node.js API
|
---|
| 16 | buffer = new Buffer(size);
|
---|
| 17 | buffer.fill(0); // zero-fill
|
---|
| 18 | }
|
---|
| 19 |
|
---|
| 20 | let fd;
|
---|
| 21 |
|
---|
| 22 | try {
|
---|
| 23 | fd = fs.openSync(command, 'r');
|
---|
| 24 | fs.readSync(fd, buffer, 0, size, 0);
|
---|
| 25 | fs.closeSync(fd);
|
---|
| 26 | } catch (e) { /* Empty */ }
|
---|
| 27 |
|
---|
| 28 | // Attempt to extract shebang (null is returned if not a shebang)
|
---|
| 29 | return shebangCommand(buffer.toString());
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | module.exports = readShebang;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.