Blame view

node_modules/shelljs/src/head.js 2.7 KB
aaac7fed   liuqimichale   add
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
  var common = require('./common');
  var fs = require('fs');
  
  common.register('head', _head, {
    canReceivePipe: true,
    cmdOptions: {
      'n': 'numLines',
    },
  });
  
  // This reads n or more lines, or the entire file, whichever is less.
  function readSomeLines(file, numLines) {
    var buf = common.buffer();
    var bufLength = buf.length;
    var bytesRead = bufLength;
    var pos = 0;
    var fdr = null;
  
    try {
      fdr = fs.openSync(file, 'r');
    } catch (e) {
      common.error('cannot read file: ' + file);
    }
  
    var numLinesRead = 0;
    var ret = '';
    while (bytesRead === bufLength && numLinesRead < numLines) {
      bytesRead = fs.readSync(fdr, buf, 0, bufLength, pos);
      var bufStr = buf.toString('utf8', 0, bytesRead);
      numLinesRead += bufStr.split('\n').length - 1;
      ret += bufStr;
      pos += bytesRead;
    }
  
    fs.closeSync(fdr);
    return ret;
  }
  //@
  //@ ### head([{'-n': \<num\>},] file [, file ...])
  //@ ### head([{'-n': \<num\>},] file_array)
  //@ Available options:
  //@
  //@ + `-n <num>`: Show the first `<num>` lines of the files
  //@
  //@ Examples:
  //@
  //@ ```javascript
  //@ var str = head({'-n': 1}, 'file*.txt');
  //@ var str = head('file1', 'file2');
  //@ var str = head(['file1', 'file2']); // same as above
  //@ ```
  //@
  //@ Read the start of a file.
  function _head(options, files) {
    var head = [];
    var pipe = common.readFromPipe();
  
    if (!files && !pipe) common.error('no paths given');
  
    var idx = 1;
    if (options.numLines === true) {
      idx = 2;
      options.numLines = Number(arguments[1]);
    } else if (options.numLines === false) {
      options.numLines = 10;
    }
    files = [].slice.call(arguments, idx);
  
    if (pipe) {
      files.unshift('-');
    }
  
    var shouldAppendNewline = false;
    files.forEach(function (file) {
      if (file !== '-') {
        if (!fs.existsSync(file)) {
          common.error('no such file or directory: ' + file, { continue: true });
          return;
        } else if (fs.statSync(file).isDirectory()) {
          common.error("error reading '" + file + "': Is a directory", {
            continue: true,
          });
          return;
        }
      }
  
      var contents;
      if (file === '-') {
        contents = pipe;
      } else if (options.numLines < 0) {
        contents = fs.readFileSync(file, 'utf8');
      } else {
        contents = readSomeLines(file, options.numLines);
      }
  
      var lines = contents.split('\n');
      var hasTrailingNewline = (lines[lines.length - 1] === '');
      if (hasTrailingNewline) {
        lines.pop();
      }
      shouldAppendNewline = (hasTrailingNewline || options.numLines < lines.length);
  
      head = head.concat(lines.slice(0, options.numLines));
    });
  
    if (shouldAppendNewline) {
      head.push(''); // to add a trailing newline once we join
    }
    return head.join('\n');
  }
  module.exports = _head;