adamrenklint/asimov-static

View on GitHub
lib/render/ScriptRenderJob.js

Summary

Maintainability
A
1 hr
Test Coverage
var Base = require('../core/Base');
var _ = require('lodash');
var handlebars = require('handlebars');
var uglify = require('uglify-js');
var _super = Base.prototype;
var npath = require('path');

module.exports = Base.extend({

  'namespace': 'Render',

  'run': function (model) {

    var self = this;
    var deferred = self.deferred();

    self.mediator.trigger('pre:render:script', model);

    var attributes = model.attributes;
    var path = attributes.path;

    if (!self.filesystem.pathExists(path)) {

      throw new Error('Cannot find script @ ' + path);
    }

    function save (raw) {

      var script = uglify.minify(raw, {
        'fromString': true
      });

      model.set({
        'rendered': script.code,
        'raw': raw
      }, {
        'silent': true
      });

      self.mediator.trigger('post:render:script', model);
      model.trigger('change:rendered', model);

      deferred.resolve(model);
    }

    var outPath = npath.join(self.options.outputPath, attributes.url);
    var outDir = outPath.split('/');
    outDir.pop();
    outDir = outDir.join('/');

    self.filesystem.forceExists(outDir);

    var browserifyCommand = npath.join(self.options.frameworkDir, 'node_modules/browserify/bin/cmd.js '+ attributes.path + ' > ' + outPath);

    self.child.execute(browserifyCommand)
      .done(function () {

        var file = self.filesystem.readFile(outPath);

        if (attributes.insertConstructor) {
          file = file.replace('{1:[function(require,module,exports){', '{1:[function(require,module,exports){setTimeout(function(){new module.exports(' + JSON.stringify(attributes.data || {}) + ');},1);');
        }

        save(file);
      })
      .fail(function (err) {

        throw new Error('Failed to render script bundle @ ' + attributes.url + ' > ' + err);
      });

    return deferred;
  }
});