Code coverage report for api/import/GitImport.js

Statements: 70.59% (36 / 51)      Branches: 33.33% (2 / 6)      Functions: 60% (6 / 10)      Lines: 70.59% (36 / 51)      Ignored: none     

All files » api/import/ » GitImport.js
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 108 109 110 111 112    1 1 1 1                               1 2 2   2 2     1   1             1 1   1   1   1   1 1               1 1 1                               1           1 1   1 1   1 1   1 1 1 11     1 1                 1         1  
"use strict";
 
var Import = require("./Import").Import;
var shellPromise = require("./../shellPromise");
var Q = require("q");
var directoryTree = require("directory-tree");
 
/**
 * @class The git import class helps import data from a git repository into a corpus, or create a new corpus.
 *
 * @property {FileList} files These are the file(s) that were dragged in.
 * @property {String} dbname This is the corpusid wherej the data should be imported
 * @property {DatumFields} fields The fields array contains titles of the data columns.
 * @property {DataList} datalist The datalist imported, to hold the data before it is saved.
 * @property {Event} event The drag/drop event.
 *
 * @description The initialize serves to bind import to all drag and drop events.
 *
 * @extends FieldDBObject
 * @tutorial tests/CorpusTest.js
 */
var GitImport = function GitImport(options) {
  Eif (!this._fieldDBtype) {
    this._fieldDBtype = "GitImport";
  }
  this.debug(" new import ", options);
  Import.apply(this, arguments);
};
 
GitImport.IMPORT_DIR = "imported_corpora";
 
GitImport.prototype = Object.create(Import.prototype, /** @lends GitImport.prototype */ {
  constructor: {
    value: GitImport
  },
 
  clone: {
    value: function(options) {
      var deferred = Q.defer();
      var self = this;
 
      var baseDir = [GitImport.IMPORT_DIR, self.corpus.id].join("/");
 
      var cdCommand = "mkdir -p " + baseDir + "; cd " + baseDir + "; cd ../; ";
      // var cloneCommand = "echo \n | echo \n | git clone " + options.remoteUri;
      var cloneCommand = " git clone " + options.remoteUri;
 
      self.debug("executing " + cdCommand + cloneCommand);
      shellPromise.execute(cdCommand + cloneCommand)
        .then(function(result) {
          self.debug("result", result);
 
          // TODO add a .fielddb.json file with metadata
          options.cloneMessage = result;
          deferred.resolve(options);
        }, function(err) {
          Eif (err.message.indexOf("already exists and is not an empty directory") > -1) {
            options.cloneMessage = err.message;
            return deferred.resolve(options);
          }
          if (err.message.indexOf("syntax error near unexpected token `|'") > -1) {
            options.cloneMessage = err.message;
            err.message = self.corpus.url + " not found";
            err.status = 404;
            return deferred.reject(err);
          }
 
          deferred.reject(err);
        })
        .fail(function(err) {
          self.debug("Error cloning", err);
          deferred.reject(err);
        });
 
      return deferred.promise;
    }
  },
 
  findFiles: {
    value: function(options) {
      var deferred = Q.defer();
      var self = this;
 
      var treeCommand = "tree -f " + GitImport.IMPORT_DIR + "/" + self.corpus.id;
      var grepCommand = "egrep \"(" + options.fileExtensions.join("$|").replace(/\./, "\\.") + "$)\"";
 
      self.debug("executing " + treeCommand + " | " + grepCommand);
      shellPromise.execute(treeCommand + " | " + grepCommand)
        .then(function(result) {
          self.debug("result", result);
          options.findFilesMessage = result;
          options.fileList = result.trim().split("\n").map(function(filePath) {
            return filePath.replace(new RegExp(".*" + GitImport.IMPORT_DIR), GitImport.IMPORT_DIR);
            // return filePath.replace(new RegExp(".*" + self.corpus.id + "/"), "");
          });
          options.fileTree = directoryTree(GitImport.IMPORT_DIR + "/" + self.corpus.id, options.fileExtensions);
          deferred.resolve(options);
        }, function(err) {
          options.findFilesMessage = err;
          deferred.reject(err);
        })
        .fail(function(err) {
          self.debug("Error cloning", err);
          deferred.reject(err);
        });
      return deferred.promise;
    }
  }
});
 
exports.GitImport = GitImport;