File: index.js

package info (click to toggle)
node-gulp-util 3.0.36%2B~cs7.1.15-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 708 kB
  • sloc: javascript: 1,591; makefile: 3
file content (91 lines) | stat: -rw-r--r-- 2,473 bytes parent folder | download | duplicates (2)
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
'use strict';

const Transform = require('stream').Transform;
const rs = require('replacestream');
const istextorbinary = require('istextorbinary');

const defaultOptions = {
  skipBinary: true
}

module.exports = function(search, _replacement, options = {}) {
  // merge options
  options = {
    ...defaultOptions,
    ...options
  }

  return new Transform({
    objectMode: true,
    /**
     * transformation
     * @param {import("vinyl")} file 
     * @param {BufferEncoding} enc 
     * @param {(error?: Error | null, data?: any) => void} callback 
     */
    transform(file, enc, callback) {
      if (file.isNull()) {
        return callback(null, file);
      }

      let replacement = _replacement;
      if (typeof _replacement === 'function') {
        // Pass the vinyl file object as this.file
        replacement = _replacement.bind({ file: file });
      }

      function doReplace() {
        if (file.isStream()) {
          file.contents = file.contents.pipe(rs(search, replacement));
          return callback(null, file);
        }

        if (file.isBuffer()) {
          if (search instanceof RegExp) {
            file.contents =  Buffer.from(String(file.contents).replace(search, replacement));
          } else {
            const chunks = String(file.contents).split(search);

            let result;
            if (typeof replacement === 'function') {
              // Start with the first chunk already in the result
              // Replacements will be added thereafter
              // This is done to avoid checking the value of i in the loop
              result = [ chunks[0] ];

              // The replacement function should be called once for each match
              for (let i = 1; i < chunks.length; i++) {
                // Add the replacement value
                result.push(replacement(search));

                // Add the next chunk
                result.push(chunks[i]);
              }

              result = result.join('');
            }
            else {
              result = chunks.join(replacement);
            }

            file.contents =  Buffer.from(result);
          }
          return callback(null, file);
        }

        callback(null, file);
      }

      if (options.skipBinary) {
        if (!istextorbinary.isText(file.path, file.contents)) {
          callback(null, file);
        } else {
          doReplace();
        }
        return;
      }

      doReplace();
    }
  });
};