All files / src iconsdir.js

93.18% Statements 41/44
86.36% Branches 19/22
100% Functions 8/8
93.18% Lines 41/44

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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      4x 4x 4x   4x         45x 45x 45x 45x   45x 4x   4x 4x       45x   45x   45x 45x 222x     222x 222x     222x                         222x   222x   45x 183x     45x   45x                     51x 222x 222x 222x       222x 6x     45x     51x 41x 41x     41x   41x   10x 6x         4x  
/* eslint-disable prefer-template,no-confusing-arrow */
'use strict';
 
const fs = require('fs');
const fileSorter = require('./filesorter');
const initMetadataService = require('../src/metadata');
 
const Readable = require('stream').Readable;
 
// Constructor
class SVGIconsDirStream extends Readable {
  constructor(dir, options) {
    super({ objectMode: true });
    this.getMetadata = options.metadataProvider || initMetadataService(options);
    this.gotFilesInfos = false;
    this.dir = dir;
 
    if (dir instanceof Array) {
      const dirCopy = this.dir;
 
      this.dir = '';
      this._getFilesInfos(dirCopy);
    }
  }
  _getFilesInfos(files) {
    let filesProcessed = 0;
 
    this.fileInfos = [];
    // Ensure prefixed files come first
    files = files.slice(0).sort(fileSorter);
    files.forEach((file) => {
      this.getMetadata(
        (this.dir ? this.dir + '/' : '') + file,
        (err, metadata) => {
          filesProcessed++;
          Iif (err) {
            this.emit('error', err);
          } else {
            Iif (metadata.renamed) {
              this.options.log(
                'Saved codepoint: ' +
                  'u' +
                  metadata.unicode[0]
                    .codePointAt(0)
                    .toString(16)
                    .toUpperCase() +
                  ' for the glyph "' +
                  metadata.name +
                  '"'
              );
            }
            this.fileInfos.push(metadata);
          }
          if (files.length === filesProcessed) {
            // Reorder files
            this.fileInfos.sort((infosA, infosB) =>
              infosA.unicode[0] > infosB.unicode[0] ? 1 : -1
            );
            // Mark directory as processed
            this.gotFilesInfos = true;
            // Start processing
            this._pushSVGIcons();
          }
        }
      );
    });
  }
 
  _pushSVGIcons() {
    let fileInfo;
    let svgIconStream;
 
    while (this.fileInfos.length) {
      fileInfo = this.fileInfos.shift();
      svgIconStream = fs.createReadStream(fileInfo.path);
      svgIconStream.metadata = {
        name: fileInfo.name,
        unicode: fileInfo.unicode,
      };
      if (!this.push(svgIconStream)) {
        return;
      }
    }
    this.push(null);
  }
  _read() {
    if (!this.fileInfos) {
      fs.readdir(this.dir, (err, files) => {
        Iif (err) {
          this.emit('error', err);
        }
        this._getFilesInfos(files);
      });
      return;
    }
    if (this.gotFilesInfos) {
      this._pushSVGIcons();
    }
  }
}
 
module.exports = SVGIconsDirStream;