all files / lib/theme/ asset.js

91.21% Statements 83/91
92.86% Branches 26/28
100% Functions 11/11
68% Lines 17/25
8 statements, 5 branches 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 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                 16×         16×                               16× 16× 16×   16×                                                               16×             16×                                                    
import fs from 'fs-extra';
import isUndefined from 'lodash/isUndefined';
 
import log from '../log';
import browserify from './processor/browserify';
import less from './processor/less';
import sass from './processor/sass';
 
let processors = {
  browserify,
  less,
  sass,
};
 
export default class Asset {
  constructor(assetType, assetConfig) E{
    /**
     * ID of asset, right now an alias to its type.
     * @type {string}
     */
    this.id = assetType;
 
    /**
     * What type of asset this is. css/js/font etc.
     * @type {string}
     */
    this.type = assetType;
 
    /**
     * The asset config object.
     * @type {Object}
     */
    this.config = assetConfig;
 
    // If this asset has a processor, instantiate the class that handles how to
    // process the asset.
    if (this.config.processor && this.config.processor.name) {
      let processorName = this.config.processor.name;
      let ProcessorClass = processors[processorName];
 
      Iif (isUndefined(ProcessorClass)) {
        let msg = `Could not load processer '${processorName}'. Aborting.`;
        log.error(msg);
        throw new Error(msg);
      }
 
      /**
       * Asset processor.
       * @type {Processor}
       */
      this.processor = new ProcessorClass(this);
 
      /**
       * Processed file.
       * @type {Object}
       */
      this._processedFile;
    }
 
    /**
     * Data accessible to templates.
     * @type {Object}
     */
    this.data = Object.create(null);
  }
 
  async process(destination) {
    if (!this.processor) {
      return;
    }
 
    try {
      this._processedFile = await this.processor.process();
    } catch (e) {
      log.error('Could not process file.');
      throw e;
    }
 
    // Expose relative url to file.
    this.data.url = this._processedFile.destination.replace(destination, '');
  }
 
  async write() {
    // If there is a processor let it handle writing the file.
    if (this.processor) {
      if (!this._processedFile) {
        log.warn(`Asset: ${this.id} found no processed file when about ` +
          'to write asset to disk. Processing now.');
        await this.process();
      }
 
      fs.outputFileSync(
        this._processedFile.destination,
        this._processedFile.asset,
        'utf8'
      );
    } else {
      // If there's no processor then just copy the source to the destination.
      try {
        fs.copySync(this.config.source, this.config.destination);
      } catch (e) {
        log.error(`Asset: Could not copy '${this.type}' assets.`);
      }
    }
  }
}