all files / lib/theme/ processor-base.js

100% Statements 45/45
90% Branches 9/10
100% Functions 8/8
100% Lines 11/11
1 branch Ignored     
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     16×           16×                                                                 16×   16×   16× 16×             16×                              
import path from 'path';
import crypto from 'crypto';
 
export default class ProcessorBase {
  constructor(asset) {
    /**
     * The asset we're processing.
     * @type {Asset}
     * @private
     */
    this._asset = asset;
 
    /**
     * Path from where to read the asset.
     * @type {string}
     */
    this.assetSource = this._asset.config.source;
 
    /**
     * Path of where to write the asset.
     * @type {string}
     */
    this.assetDestination = this._asset.config.destination;
 
    /**
     * What plugins to apply to the processor.
     * @type {Object}
     */
    this.plugins = this._asset.config.processor.plugins;
 
    /**
     * Whether we should hash the file name.
     * @type {boolean}
     */
    this.shouldHash = this._asset.config.processor.hash;
  }
 
  /**
   * Public API that is used to process the asset.
   * @return {Promise} Returns a promise that resolves with the destination
   *  and actual content of the processed asset.
   */
  async process() {
    let asset = await this._getFile();
 
    let destination = this._getDestination(asset);
 
    Eif (this.shouldHash) {
      this.checksum = crypto.createHash('md5')
        .update(asset, 'utf8').digest('hex').slice(0, 10);
 
      // Get destination extension, i.e. `.css`.
      let extension = path.extname(destination);
 
      // Append checksum to path.
      destination = destination.replace(
        new RegExp(extension + '$'),
        `-${this.checksum}${extension}`
      );
    }
 
    return {
      destination,
      asset
    };
  }
 
  _getDestination() {
    return this.assetDestination;
  }
}