text: // Generated by CoffeeScript 1.6.1
  (function() {
    var Js, fs, stitch, uglify, _;
  
    _ = require('underscore');
  
    fs = require('fs');
  
    stitch = require('stitch-extra');
  
    uglify = require('uglify-js');
  
    Js = (function() {
  
      Js.prototype.config = null;
  
      function Js(singool) {
        this.singool = singool;
        this.config = this.singool.config;
        this.prepare();
      }
  
      Js.prototype.prepare = function() {
        this.ignore();
        this.vendors();
        return this.compilers;
      };
  
      Js.prototype.ignore = function() {
        var availablePlugin, availablePlugins, k, regex, _results;
        this.config.ignore.push(/\/vendors\//);
        if (!this.config.test) {
          this.config.ignore.push(/\/tests\//);
        }
        availablePlugins = fs.readdirSync(this.config.pluginsPath);
        _results = [];
        for (k in availablePlugins) {
          availablePlugin = availablePlugins[k];
          if (_.indexOf(this.config.plugins, availablePlugin) === -1) {
            regex = new RegExp('/' + this.config.pluginsPath + '/' + availablePlugin + '/', 'i');
            _results.push(this.config.ignore.push(regex));
          } else {
            _results.push(void 0);
          }
        }
        return _results;
      };
  
      Js.prototype.getVendorsByPath = function(path) {
        var k, pathVendor, pathVendors, vendors;
        vendors = {};
        if (fs.existsSync(path + '/config/vendors.json')) {
          pathVendors = JSON.parse(fs.readFileSync(path + '/config/vendors.json', 'utf8'));
          for (k in pathVendors) {
            pathVendor = pathVendors[k];
            vendors[pathVendor] = path + '/vendors/' + pathVendor;
          }
        }
        return vendors;
      };
  
      Js.prototype.vendors = function() {
        var filename, k, l, path, plugin, v, vendorsByFilename, _ref, _ref1, _ref2;
        _ref = this.config.paths;
        for (k in _ref) {
          path = _ref[k];
          this.config.vendors = _.extend(this.getVendorsByPath(path), this.config.vendors);
          _ref1 = this.config.plugins;
          for (l in _ref1) {
            plugin = _ref1[l];
            this.config.vendors = _.extend(this.getVendorsByPath(path + '/' + plugin), this.config.vendors);
          }
        }
        this.config.vendors = _.extend(this.getVendorsByPath(this.config.themesPath + '/' + this.config.theme), this.config.vendors);
        vendorsByFilename = {};
        _ref2 = this.config.vendors;
        for (k in _ref2) {
          v = _ref2[k];
          filename = _.last(v.split('/'));
          vendorsByFilename[filename] = v;
        }
        if (!this.config.init && _.has(vendorsByFilename, 'init.js')) {
          delete vendorsByFilename['init.js'];
        }
        return this.config.vendors = _.values(vendorsByFilename);
      };
  
      Js.prototype.compilers = function() {
        var Compiler, compilerFiles, compilerName, compilers, k, v;
        compilers = {};
        compilerFiles = fs.readdirSync(module.id.replace(_.last(module.id.split('/')), 'compilers/'));
        for (k in compilerFiles) {
          v = compilerFiles[k];
          compilerName = v.replace('.js', '');
          if (!compilerName) {
            continue;
          }
          Compiler = require('./compilers/' + compilerName);
          this._compilers[compilerName] = new Compiler(this);
          compilers[compilerName] = this._compilers[compilerName].run;
        }
        return this.config.compilers = compilers;
      };
  
      Js.prototype.generate = function(callback) {
        var pkg,
          _this = this;
        if (callback == null) {
          callback = false;
        }
        pkg = stitch.createPackage({
          dependencies: this.config.vendors,
          paths: this.config.paths,
          compilers: this.config.compilers,
          ignore: this.config.ignore
        });
        return pkg.compile(function(err, source) {
          var ast, jsp, pro;
          if (err) {
            throw err;
          }
          if (_this.config.compress) {
            jsp = uglify.parser;
            pro = uglify.uglify;
            ast = jsp.parse(source);
            ast = pro.ast_mangle(ast);
            ast = pro.ast_squeeze(ast);
            source = pro.gen_code(ast);
          }
          if (callback) {
            return callback(source);
          }
        });
      };
  
      Js.prototype.write = function(path) {
        var _this = this;
        if (path == null) {
          path = null;
        }
        if (!path) {
          path = this.config.jsPath + '/' + this.config.jsFile;
        }
        return this.generate(function(source) {
          fs.writeFileSync(path, source);
          return console.log('JS file written at: ' + path);
        });
      };
  
      Js.prototype.clear = function() {
        var file;
        file = this.config.jsPath + '/' + this.config.jsFile;
        if (fs.existsSync(file)) {
          fs.unlinkSync(file);
          return console.log(this.config.jsFile + ' deleted');
        }
      };
  
      return Js;
  
    })();
  
    module.exports = Js;
  
  }).call(this);
  