<commit_before>const Promise = require('bluebird');
const fs = Promise.promisifyAll(require('fs'));
const path = require('path');
const IbuErrorDoc = require('./schema');
/*eslint-disable*/
const db = require('../config/db');
let index = 100;
let totalProcessed = 0;
let readDirrectories = 0;
let error = [];

import config from '../config/index';

let gravity = config.development.rootPath;

/*eslint-enable*/
/**
 * Find and Update database document Or create a new one
 * @param  {Object} file - File(s) from fs
 * @return {*}      - None
 */
function saveToDB(file) {
    const ext = path.extname(file);
    const FILENAME = { filename: path.basename(file, path.extname(file)) };
    if (ext !== '') {
      /**
       * Using Mongoose count to see if file exist
       * @param  {string|object|*} { 'filename': path.basename(file,
       * path.extname(file)) - Searching for the current file's name
       * @fires exec - Numbers function to eval if update/save is needed and runs
       * @param {number} - number of files found with this same filename (1 or 0)
       * @return {*}  - No Return
       */
       IbuErrorDoc.count(FILENAME)
        .exec((err, number) => {
          if (number === 0) {
            if (ext === '.xml') {
              const xmlfileFoundInFolder = new IbuErrorDoc({
                filename: path.basename(file, path.extname(file)),
                filePathXML: path.resolve(file),
                filePathIMG: '',
              });
              xmlfileFoundInFolder.save((xmlSaveErr) => {
                if (xmlSaveErr) {
                  // Recursive Function for data crashes
                  setTimeout(saveToDB(file), 2000);
                } else {
                  totalProcessed++;
                }
              });
            } else if ((ext === '.tif') || (ext === '.jp2')) {
              const tiffileFoundInFolder = new IbuErrorDoc({
                filename: path.basename(file, path.extname(file)),
                filePathIMG: path.resolve(file),
                filePathXML: '',
              }, { upsert: true });
              tiffileFoundInFolder.save((tifUpdateErr) => {
                if (tifUpdateErr) {
                  // Recursive Function for data crashes
                  setTimeout(saveToDB(file), 2000);
                } else {
                  totalProcessed++;
                }
              });
            }
          } else {
          /**
           * Find Existing db object(s) and stram a stream
           * @param  {string|object|*} { 'filename': path.basename(file,
           * path.extname(file)) - Searching for the current file's name
           * @lends .stream -Creates a stream
           */
            const stream = IbuErrorDoc.find(FILENAME)
            .select('_id filename filePathXML filePathIMG').stream();
            /**
             * Stream is
             * @constructor
             * @param  {object} 'data' -Sent from IbuErrorDoc.find
             * @param  {function} streamOn -Seperates MODS & IMG for Updating
             * @param  {function} close -Closes stream
             * @return {function} saveToDB -Recursive Call in case of a data crash
             */
            stream.on('data', (doc) => {
              if ((doc.filePathXML === '') && (ext === '.xml')) {
                IbuErrorDoc.findByIdAndUpdate(doc._id, { $set: { filePathXML: path.resolve(file) } },
                  (err) => {
                    console.log('Find ID Update xml', err);
                    // Recursive Function for data crashes
                    setTimeout(saveToDB(file), 2000);
                  });
              } else if ((doc.filePathIMG === '') && ((ext === '.tif')) || (ext === '.jp2')) {
                IbuErrorDoc.findByIdAndUpdate(doc._id, { $set: { filePathIMG: path.resolve(file) } },
                  (err) => {
                    console.log('Find ID Update img', err);
                    // Recursive Function for data crashes
                    setTimeout(saveToDB(file), 2000);
                  });
              }
            }).on('error', (err) => {
              throw new Error(error);
              // throw err;
              // console.warn(err);
            }).on('close', () => {
              // console.log('Closing: ', index, totalProcessed);
              totalProcessed++;
              if (index === totalProcessed) {
                // console.log('Triggered');
              }
            });
          }
        });
    }
}

/**
 * Recersive Index of a specified directory and sends to db save function
 * @param  {string} gravity - Directory passed by controller.js
 * @constructor fs.readir -Recursive files/folders list
 * @param  {object} !files.length - Evaluate if the folders are empty
 * @param  {string} path.join(gravity, file) - Returns Full path of file
 * @param  {string} onlyFiles - Returns only files (no directories)
 * @fires .forEach - SaveToDB
 */
function filelistings() {
  fs.readdirAsync(gravity, (err, files) => {
    if (err) {
      return console.log(err);
    }
    index = files.length;
    if (!files.length) {
      return console.log('No files to show');
    }
    return files.map((file) => { //eslint-disable-line
      return path.join(gravity, file);
    }).filter((file) => {
      if (fs.statSync(file).isFile() === false) {
        readDirrectories++;
        totalProcessed++;
      }
      return fs.statSync(file).isFile();
    }).forEach((file) => {
      if (path.extname(file) === '.tif' || path.extname(file) === '.jp2' ||
      path.extname(file) === '.xml') {
        saveToDB(file);
      }
    });
  });
}

function checkIfDone() {
  console.log('checking');
  if (error.length > 0) {
    console.log('errors everywhere!');
    return Promise.reject(error);
   //  throw new Error('Errors Everywhere in Abduction');
  }
  console.log(index, totalProcessed);
  if (index !== totalProcessed) {
    console.log('Again');
    setTimeout(checkIfDone, 1000);
    // console.log('success');
  } else {
    console.log('Gottem haha');
    return 'success';
  }
}
/**
 * Serch directory for Image & MODS file and saves to db
 * @param  {[type]} gravity - The directory to search
 * @return {[type]} db      - ported to the database
 */
module.exports = function abduction(resolve) {
     filelistings();
    let answer = setTimeout(checkIfDone, 3000);
    return answer;
};
<commit_msg>Working but no callback<commit_after>const Promise = require('bluebird');
const fs = Promise.promisifyAll(require('fs'));
const path = require('path');
const IbuErrorDoc = require('./schema');
/*eslint-disable*/
const db = require('../config/db');
let indexed = -10;
let totalProcessed = 0;
let error = [];

import config from '../config/index';
let gravity = config.development.rootPath;
/*eslint-enable*/
/**
 * Find and Update database document Or create a new one
 * @param  {Object} file - File(s) from fs
 * @return {*}      - None
 */
function saveToDB(file) {
  const ext = path.extname(file);
  let filename = path.basename(file, ext);
  let FILEPATH = path.resolve(file);
  let FILENAMESTRING = { filename: filename };
  let filePathIMG;
  let filePathXML;
  // console.log(ext, filename, FILEPATH, FILENAMESTRING);
  if (ext !== '') {
      /**
       * Using Mongoose count to see if file exist
       * @param  {string|object|*} { 'filename': path.basename(file,
       * path.extname(file)) - Searching for the current file's name
       * @fires exec - Numbers function to eval if update/save is needed and runs
       * @param {number} - number of files found with this same filename (1 or 0)
       * @return {*}  - No Return
       */
       IbuErrorDoc.count(FILENAMESTRING)
        .exec((err, number) => {
          if (number === 0) {
            if (ext === '.xml') {
              const xmlfileFoundInFolder = new IbuErrorDoc({
                filename: filename,
                filePathXML: FILEPATH,
                indexed,
                filePathIMG,
              });
              xmlfileFoundInFolder.save((xmlSaveErr) => {
                if (xmlSaveErr) {
                  setTimeout(saveToDB(file), 2000);
                } else {
                  totalProcessed++;
                }
              });
            } else if ((ext === '.tif') || (ext === '.jp2')) {
              const tiffileFoundInFolder = new IbuErrorDoc({
                filename: filename,
                filePathIMG: FILEPATH,
                indexed,
                filePathXML: null,
              });
              tiffileFoundInFolder.save((tifUpdateErr) => {
                if (tifUpdateErr) {
                  // Recursive Function for data crashes
                  setTimeout(saveToDB(file), 2000);
                } else {
                  totalProcessed++;
                }
              });
            } else {
              // Formats outside of scope
              indexed--;
              totalProcessed++;
            }
          } else {
          /**
           * Find Existing db object(s) and stram a stream
           * @param  {string|object|*} { 'filename': path.basename(file,
           * path.extname(file)) - Searching for the current file's name
           * @lends .stream -Creates a stream
           */
            const stream = IbuErrorDoc.find(FILENAMESTRING)
            .select('_id filename filePathXML filePathIMG').stream();
            /**
             * Stream is
             * @constructor
             * @param  {object} 'data' -Sent from IbuErrorDoc.find
             * @param  {function} streamOn -Seperates MODS & IMG for Updating
             * @param  {function} close -Closes stream
             * @return {function} saveToDB -Recursive Call in case of a data crash
             */
            stream.on('data', (doc) => {
              if ((doc.filePathXML === '') && (ext === '.xml')) {
                IbuErrorDoc.findByIdAndUpdate(doc._id, { $set: {
                  filePathXML: FILEPATH, indexed,
                } },
                  (err) => {
                    // console.log('Find ID Update xml', err);
                    // Recursive Function for data crashes
                    setTimeout(saveToDB(file), 2000);
                  });
                totalProcessed++;
              } else if ((doc.filePathIMG === '') && ((ext === '.tif')) || (ext === '.jp2')) {
                IbuErrorDoc.findByIdAndUpdate(doc._id, { $set: {
                  filePathIMG: FILEPATH, indexed,
                } },
                  (err) => {
                    // console.log('Find ID Update img', err);
                    // Recursive Function for data crashes
                    setTimeout(saveToDB(file), 2000);
                  });
                totalProcessed++;
              }
            }).on('error', (err) => {
              throw new Error(error);
              // throw err;
              // console.warn(err);
            }).on('close', () => {
              console.log('Closing: ', indexed, totalProcessed);
              // totalProcessed++;
              // if (indexed === totalProcessed) {
              //   // console.log('Triggered');
              // }
            });
          }
        });
    }
}

/**
 * Recersive Index of a specified directory and sends to db save function
 * @param  {string} gravity - Directory passed by controller.js
 * @constructor fs.readir -Recursive files/folders list
 * @param  {object} !files.length - Evaluate if the folders are empty
 * @param  {string} path.join(gravity, file) - Returns Full path of file
 * @param  {string} onlyFiles - Returns only files (no directories)
 * @fires .forEach - SaveToDB
 */
function filelistings() {
  fs.readdirAsync(gravity, (err, files) => {
    let fileLength = files.length;
    if (err) {
      return console.log(err);
    }
    indexed = files.length;
    if (!files.length) {
      return console.log('No files to show');
    }
    return files.map((file) => { //eslint-disable-line
      return path.join(gravity, file);
    }).filter((file) => {
      if (fs.statSync(file).isFile() === false) {
        totalProcessed++;
      }
      return fs.statSync(file).isFile();
    }).forEach((file, fileLength) => {
      if (path.extname(file) === '.tif' || path.extname(file) === '.jp2' ||
      path.extname(file) === '.xml') {
        saveToDB(file);
      }
    });
  });
}

function checkIfDone(resolve) {
  if (error.length > 0) {
    let errors = error.toString();
    return Promise.reject();
  }
  if (indexed !== totalProcessed) {
    // console.log('Again');
    setTimeout(checkIfDone, 1000);
    // console.log('success');
  } else {
    return Promise.resolve('success');
  }
}

module.exports = function abduction() {
  filelistings();
  let answer = checkIfDone();
  return Promise.resolve(answer);
};
<|endoftext|>


<commit_before>var CURRENT_VERSION = 'v2';
var CACHE_URLS = {
  'https://cdn.polyfill.io/v2/polyfill.min.js?features=fetch': 1,
  'https://api.cosmicjs.com/v1/blog-cb/objects': 1,
  'https://api.cosmicjs.com/v1/blog-cb/objects?bustcache=true': 2
};

function cacheOpen (event, response) {
  return function (cache) {
    cache
      .put(event.request, response.clone())
      .then(function () {
        console.info('successfully cached', event.request.url, response);
      })
      .catch(function (err) {
        console.error('cache error', err);
      });
    return response;
  }
}

function fetchSuccess (event) {
  return function (response) {
    console.info('fetch success', event.request.url);
    return caches
      .open(CURRENT_VERSION)
      .then(cacheOpen(event, response));
  }
}

function noCache (event) {
  return function () {
    console.info('no cache', event.request.url);
    return fetch(event.request)
      .then(fetchSuccess(event));
  }
}

function checkCacheResponse (response) {
  if (!response) {
    console.info('no cache response');
    throw new Error();
  }
  // console.info('cache response', response);
  return response;
}

this.addEventListener('fetch', function (event) {
  var cacheType = CACHE_URLS[event.request.url];
  if (!cacheType) return;

  // console.info('fetch', event.request.url);
  if (cacheType === 1) {
    event.respondWith(
      caches
        .match(event.request)
        .then(checkCacheResponse)
        .catch(noCache(event))
        .catch(function noCacheFail () {
          console.error('no cache fail');
        })
    );
  } else {
    var bustRequest = new Request('https://api.cosmicjs.com/v1/blog-cb/objects');
    fetch(bustRequest).then(fetchSuccess({
      request: bustRequest
    }));
  }
});

this.addEventListener('activate', function (event) {
  event.waitUntil(
    caches.keys().then(function (keyList) {
      return Promise.all(keyList.map(function (key) {
        if (key !== CURRENT_VERSION) {
          return caches.delete(key);
        }
      }));
    })
  );
});
<commit_msg>Remove redundant logging<commit_after>var CURRENT_VERSION = 'v2';
var CACHE_URLS = {
  'https://cdn.polyfill.io/v2/polyfill.min.js?features=fetch': 1,
  'https://api.cosmicjs.com/v1/blog-cb/objects': 1,
  'https://api.cosmicjs.com/v1/blog-cb/objects?bustcache=true': 2
};

function cacheOpen (event, response) {
  return function (cache) {
    cache
      .put(event.request, response.clone())
      .catch(function (err) {
        console.error('cache error', err);
      });
    return response;
  }
}

function fetchSuccess (event) {
  return function (response) {
    // console.info('fetch success', event.request.url);
    return caches
      .open(CURRENT_VERSION)
      .then(cacheOpen(event, response));
  }
}

function noCache (event) {
  return function () {
    console.info('no cache', event.request.url);
    return fetch(event.request)
      .then(fetchSuccess(event));
  }
}

function checkCacheResponse (response) {
  if (!response) {
    console.info('no cache response');
    throw new Error();
  }
  // console.info('cache response', response);
  return response;
}

this.addEventListener('fetch', function (event) {
  var cacheType = CACHE_URLS[event.request.url];
  if (!cacheType) return;

  // console.info('fetch', event.request.url);
  if (cacheType === 1) {
    event.respondWith(
      caches
        .match(event.request)
        .then(checkCacheResponse)
        .catch(noCache(event))
        .catch(function noCacheFail () {
          console.error('no cache fail');
        })
    );
  } else {
    var bustRequest = new Request('https://api.cosmicjs.com/v1/blog-cb/objects');
    fetch(bustRequest).then(fetchSuccess({
      request: bustRequest
    }));
  }
});

this.addEventListener('activate', function (event) {
  event.waitUntil(
    caches.keys().then(function (keyList) {
      return Promise.all(keyList.map(function (key) {
        if (key !== CURRENT_VERSION) {
          return caches.delete(key);
        }
      }));
    })
  );
});
<|endoftext|>


<commit_before>/**
 * ingestion.js
 * expects:
 * 1. path of directory (target)
 * 2. parentpid
 * 3. namespace
 * 4. model
 *   (note: basic image,large image,audio,video, collection, pdf, binary -- all require
 * the "ibsp" in the command string.)
 * output:
 *  log of drush run
 * errors:
 *  if drush not installed
 *  if parameters missing
 *  if first command did not run
 *  if first command did run but did not prep ingest
 *  if first command ran with a good ingest but second command did not run
 *   (only if first command ran successfully)if second ran but did not ingest
 *
 * @param target directory path
 * @param parentpid
 * @param namespace
 * @param model
 * @return $message
 *
 */
// for testing
//
var param = process.argv;
console.log('hi');
target = String(param[2]);
parentpid = String(param[3]);
namespace = String(param[4]);
model = String(param[5]);

var status=[];

function ingestion(target,parentpid,namespace,model) {
  // build command pieces
  // two drupalhomes one for testing on vagrant and one for server installation
  var drupalhome = '/var/www/drupal';
  //var drupalhome = '/vhosts/dlwork/web/collections';
  console.log('drupalhome = ',drupalhome);
  // serveruri is the location of the drupal_home on the drupal server
  var serveruri = 'http://localhost';

  //var serveruri = 'http://dlwork.lib.utk.edu/dev/';
  console.log('serveruri = ',serveruri);
  console.log('parentpid = ',parentpid);
  // namespace
  console.log('namespace = ',namespace);
  // target is the local directory holding the ingest files
  console.log('target = ',target);
  // make mongo connection
  /*
  var mongoose = require('mongoose');
  mongoose.connect('mongodb://localhost/ibu');
  var conn = mongoose.connection;
  */
  //
  var $message = 'ingest did not happen';
  var contentmodel = '';
  if ((model)&& (model==='basic')) {
    contentmodel = 'islandora:sp_basic_image';
  }
  if ((model)&&(model==='large')) {
    contentmodel = 'islandora:sp_Large_image';
  }
  console.log('model = ',model);
  // execute first drush command
  var exec = require('child_process').exec;
  // test for drush existance
  var cmdtest = String('whereis drush')
  exec(cmdtest, function(error, stdout, stderr) {
     // command output is in stdout
     var output = `stdout:${stdout}`;
     //console.log(`stdout:${stdout}`);
     // test command log, stdout, for success indication
     if(output.indexOf('bin/drush') > -1) {
       $message = 'drush installed';
       console.log($message);
       //status.push("$message");
     }// end if
     else {
       $message = 'drush not installed';
       //console.log($message);
       status.push("$message");
       //exit if drush not installed
       return $message;
     }//end else
  });//end exec
  var cmd = String('drush -r '+drupalhome+' -v -u=1 --uri='+serveruri+' ibsp --content_models='+contentmodel+' --type=directory --parent='+parentpid+' --namespace='+namespace+' --target='+target );
  // show assembled command
  console.log('cmd=',cmd);
  if ((target !='')&&(contentmodel !='')&&(parentpid !='')&&(namespace !='')) {
    exec(cmd, function(error, stdout, stderr) {
     // command output is in stdout
     var output1 = `stdout:${stdout}`;
     //console.log(`stdout:${stdout}`);
     // test command log, stdout, for success indication
     if(output1.indexOf('SetId:') > -1) {
       $message = 'ingest prep drush command success';
       console.log($message);
       //status.push("$message");
       //return $message;
     }// end if
     else {
       $message = 'first ingest command failed!';
       console.log($message);
       status.push("$message");
       return $message;
     }//end else
    });// end exec
  }// end if
  else {
     $message = 'parameters for first command missing, ingest not started.';
     console.log($message);
     status.push("$message");
     return $message;
  }// end else
  // exec second drush command
  var cmd2 = String('drush -r '+drupalhome+' -v -u=1 --uri='+serveruri+' islandora_batch_ingest');
  console.log('cmd2=',cmd2);
  //$message = 'hold';
  if ($message = 'ingest prep drush command success') {
    exec(cmd2, function(error, stdout, stderr) {
     // command output is in stdout
     var output2 = `stdout:${stdout}`;
     //console.log(`stdout:${stdout}`);
     // test command log, stdout, for success indication
     if(output2.indexOf('Processing complete;') > -1) {
       $message = 'ingest drush command success';
       console.log($message);
       status.push("$message");
       //return $message;
     }// end if
     else {
       $message = 'first ingest command failed!';
       console.log($message);
       status.push("$message");
       return $message;
     }//end else
    });
  }// end if
  return $message;
}// end function
//export default ingestion;
ingestion(target,parentpid,namespace,model);

<commit_msg>remove colon in test string<commit_after>/**
 * ingestion.js
 * expects:
 * 1. path of directory (target)
 * 2. parentpid
 * 3. namespace
 * 4. model
 *   (note: basic image,large image,audio,video, collection, pdf, binary -- all require
 * the "ibsp" in the command string.)
 * output:
 *  log of drush run
 * errors:
 *  if drush not installed
 *  if parameters missing
 *  if first command did not run
 *  if first command did run but did not prep ingest
 *  if first command ran with a good ingest but second command did not run
 *   (only if first command ran successfully)if second ran but did not ingest
 *
 * @param target directory path
 * @param parentpid
 * @param namespace
 * @param model
 * @return $message
 *
 */
// for testing
//
var param = process.argv;
console.log('hi');
target = String(param[2]);
parentpid = String(param[3]);
namespace = String(param[4]);
model = String(param[5]);

var status=[];

function ingestion(target,parentpid,namespace,model) {
  // build command pieces
  // two drupalhomes one for testing on vagrant and one for server installation
  var drupalhome = '/var/www/drupal';
  //var drupalhome = '/vhosts/dlwork/web/collections';
  console.log('drupalhome = ',drupalhome);
  // serveruri is the location of the drupal_home on the drupal server
  var serveruri = 'http://localhost';

  //var serveruri = 'http://dlwork.lib.utk.edu/dev/';
  console.log('serveruri = ',serveruri);
  console.log('parentpid = ',parentpid);
  // namespace
  console.log('namespace = ',namespace);
  // target is the local directory holding the ingest files
  console.log('target = ',target);
  // make mongo connection
  /*
  var mongoose = require('mongoose');
  mongoose.connect('mongodb://localhost/ibu');
  var conn = mongoose.connection;
  */
  //
  var $message = 'ingest did not happen';
  var contentmodel = '';
  if ((model)&& (model==='basic')) {
    contentmodel = 'islandora:sp_basic_image';
  }
  if ((model)&&(model==='large')) {
    contentmodel = 'islandora:sp_Large_image';
  }
  console.log('model = ',model);
  // execute first drush command
  var exec = require('child_process').exec;
  // test for drush existance
  var cmdtest = String('whereis drush')
  exec(cmdtest, function(error, stdout, stderr) {
     // command output is in stdout
     var output = `stdout:${stdout}`;
     //console.log(`stdout:${stdout}`);
     // test command log, stdout, for success indication
     if(output.indexOf('bin/drush') > -1) {
       $message = 'drush installed';
       console.log($message);
       //status.push("$message");
     }// end if
     else {
       $message = 'drush not installed';
       //console.log($message);
       status.push("$message");
       //exit if drush not installed
       return $message;
     }//end else
  });//end exec
  var cmd = String('drush -r '+drupalhome+' -v -u=1 --uri='+serveruri+' ibsp --content_models='+contentmodel+' --type=directory --parent='+parentpid+' --namespace='+namespace+' --target='+target );
  // show assembled command
  console.log('cmd=',cmd);
  if ((target !='')&&(contentmodel !='')&&(parentpid !='')&&(namespace !='')) {
    exec(cmd, function(error, stdout, stderr) {
     // command output is in stdout
     var output1 = `stdout:${stdout}`;
     //console.log(`stdout:${stdout}`);
     // test command log, stdout, for success indication
     if(output1.indexOf('SetId') > -1) {
       $message = 'ingest prep drush command success';
       console.log($message);
       //status.push("$message");
       //return $message;
     }// end if
     else {
       $message = 'first ingest command failed!';
       console.log($message);
       status.push("$message");
       return $message;
     }//end else
    });// end exec
  }// end if
  else {
     $message = 'parameters for first command missing, ingest not started.';
     console.log($message);
     status.push("$message");
     return $message;
  }// end else
  // exec second drush command
  var cmd2 = String('drush -r '+drupalhome+' -v -u=1 --uri='+serveruri+' islandora_batch_ingest');
  console.log('cmd2=',cmd2);
  //$message = 'hold';
  if ($message = 'ingest prep drush command success') {
    exec(cmd2, function(error, stdout, stderr) {
     // command output is in stdout
     var output2 = `stdout:${stdout}`;
     //console.log(`stdout:${stdout}`);
     // test command log, stdout, for success indication
     if(output2.indexOf('Processing complete;') > -1) {
       $message = 'ingest drush command success';
       console.log($message);
       status.push("$message");
       //return $message;
     }// end if
     else {
       $message = 'first ingest command failed!';
       console.log($message);
       status.push("$message");
       return $message;
     }//end else
    });
  }// end if
  return $message;
}// end function
//export default ingestion;
ingestion(target,parentpid,namespace,model);

<|endoftext|>


<commit_before>/**
 * ingestion.js
 * expects:
 * 1. path of directory (target)
 * 2. parentpid
 * 3. namespace
 * 4. model
 *   (note: basic image,large image,audio,video, collection, pdf, binary -- all require
 * the "ibsp" in the command string, book requires the "ibbp" part in the drush command.)
 *
 * output:
 *  if first command is successful: "ingest prep drush command success"
 *  if second command is successful: "ingest drush command success"
 *
 * errors:
 *  if parameters missing
 *  if first command did not run
 *  if first command did run but did not prep ingest
 *  if first command ran with a good ingest but second command did not run
 *   (only if first command ran successfully)if second ran but did not ingest
 *
 * @param target directory path
 * @param parentpid
 * @param namespace
 * @param model
 * @return $message
 *
 */
// for testing
//
var param = process.argv;
console.log('hi');
target = String(param[3]);
parentpid = String(param[4]);
namespace = String(param[5]);
model = String(param[6]);

var status=[];

function ingestion(target,parentpid,namespace,model) {
  // build command pieces
  // serveruri is the location of the drupal_home on the drupal server
  var drupalhome = '/vhosts/dlwork/web/collections';
  console.log('drupalhome = ',drupalhome);
  var serveruri = 'http://dlwork.lib.utk.edu/dev/';
  console.log('serveruri = ',serveruri);
  console.log('parentpid = ',parentpid);
  // namespace
  console.log('namespace = ',namespace);
  // target is the local directory holding the ingest files
  console.log('target = ',target);
  // make mongo connection
  /*
  var mongoose = require('mongoose');
  mongoose.connect('mongodb://localhost/ibu');
  var conn = mongoose.connection;
  */
  //
  var $message = 'ingest did not happen';
  var contentmodel = '';
  if ((model)&& (model==='basic')) {
    contentmodel = 'islandora:sp_basic_image';
  }
  if ((model)&&(model==='large')) {
    contentmodel = 'islandora:sp_Large_image';
  }
  console.log('model = ',model);
  // execute first drush command
  var exec = require('child_process').exec;
  var cmd = String('drush -r '+drupalhome+'-v -u=1 --uri='+serveruri+' ibsp --content_models='+contentmodel+' --type=directory --parent='+parentpid+' --namespace='+namespace+' --target='+target );
  // show assembled command
  console.log('cmd=',cmd);
  if ((target !='')&&(contentmodel !='')&&(parentpid !='')&&(namespace !='')) {
    exec(cmd, function(error, stdout, stderr) {
     // command output is in stdout
     console.log(stdout);
     // test command log for success indication
     // test for substr in stdout
     if(stdout.indexOf('SetID:') > -1) {
       $message = 'ingest prep drush command success';
       console.log($message);
       status.push("$message");
       //return $message;
     }// end if
     else {
       $message = 'first ingest command failed!';
       console.log($message);
       status.push("$message");
       return $message;
     }//end else
    });// end exec
  }// end if
  else {
     // this could be broken down here into individual error messages for the actual parameters that are missing,
     // but since the parameter passing is handled programatically, this might work.
     console.log('parameters for first command missing, ingest not started.\n');
     $message = 'parameters for first command missing, ingest not started.';
     status.push("$message");
     return $message;
  }// end else
  // exec second drush command
  var cmd2 = String('drush -r '+drupalhome+'-v -u=1 --uri='+serveruri+' islandora_batch_ingest');
  console.log('cmd2=',cmd2);
  if ($message = 'ingest prep drush command success') {
    exec(cmd2, function(error, stdout, stderr) {
     // command output is in stdout
     console.log(stdout);
     // test command log for success indication
     // test for substr in stdout
     if(stdout.indexOf('Processing Complete:') > -1) {
       $message = 'ingest drush command success';
       console.log($message);
       status.push("$message");
       //return $message;
     }// end if
     else {
       $message = 'second ingest command failed!';
       console.log($message);
       status.push("$message");
       return $message;
     }//end else
    });
  }// end if
  return $message;
}// end function
//export default ingestion;
ingestion(target,parentpid,namespace,model);

<commit_msg>add correct error messages in comments<commit_after>/**
 * ingestion.js
 * expects:
 * 1. path of directory (target)
 * 2. parentpid
 * 3. namespace
 * 4. model
 *   (note: basic image,large image,audio,video, collection, pdf, binary -- all require
 * the "ibsp" in the command string, book requires the "ibbp" part in the drush command.)
 *
 * output:
 *  if second command is successful: "ingest drush command success"
 *
 * errors:
 *  if parameters missing
 *  if first command did not return success
 *  (only if first command ran successfully)if second ran but did not ingest
 *
 * @param target directory path
 * @param parentpid
 * @param namespace
 * @param model
 * @return $message
 *
 */
// for testing
//
var param = process.argv;
console.log('hi');
target = String(param[3]);
parentpid = String(param[4]);
namespace = String(param[5]);
model = String(param[6]);

var status=[];

function ingestion(target,parentpid,namespace,model) {
  // build command pieces
  // serveruri is the location of the drupal_home on the drupal server
  var drupalhome = '/vhosts/dlwork/web/collections';
  console.log('drupalhome = ',drupalhome);
  var serveruri = 'http://dlwork.lib.utk.edu/dev/';
  console.log('serveruri = ',serveruri);
  console.log('parentpid = ',parentpid);
  // namespace
  console.log('namespace = ',namespace);
  // target is the local directory holding the ingest files
  console.log('target = ',target);
  // make mongo connection
  /*
  var mongoose = require('mongoose');
  mongoose.connect('mongodb://localhost/ibu');
  var conn = mongoose.connection;
  */
  //
  var $message = 'ingest did not happen';
  var contentmodel = '';
  if ((model)&& (model==='basic')) {
    contentmodel = 'islandora:sp_basic_image';
  }
  if ((model)&&(model==='large')) {
    contentmodel = 'islandora:sp_Large_image';
  }
  console.log('model = ',model);
  // execute first drush command
  var exec = require('child_process').exec;
  var cmd = String('drush -r '+drupalhome+'-v -u=1 --uri='+serveruri+' ibsp --content_models='+contentmodel+' --type=directory --parent='+parentpid+' --namespace='+namespace+' --target='+target );
  // show assembled command
  console.log('cmd=',cmd);
  if ((target !='')&&(contentmodel !='')&&(parentpid !='')&&(namespace !='')) {
    exec(cmd, function(error, stdout, stderr) {
     // command output is in stdout
     console.log(stdout);
     // test command log for success indication
     // test for substr in stdout
     if(stdout.indexOf('SetID:') > -1) {
       $message = 'ingest prep drush command success';
       console.log($message);
       status.push("$message");
       //return $message;
     }// end if
     else {
       $message = 'first ingest command failed!';
       console.log($message);
       status.push("$message");
       return $message;
     }//end else
    });// end exec
  }// end if
  else {
     // this could be broken down here into individual error messages for the actual parameters that are missing,
     // but since the parameter passing is handled programatically, this might work.
     console.log('parameters for first command missing, ingest not started.\n');
     $message = 'parameters for first command missing, ingest not started.';
     status.push("$message");
     return $message;
  }// end else
  // exec second drush command
  var cmd2 = String('drush -r '+drupalhome+'-v -u=1 --uri='+serveruri+' islandora_batch_ingest');
  console.log('cmd2=',cmd2);
  if ($message = 'ingest prep drush command success') {
    exec(cmd2, function(error, stdout, stderr) {
     // command output is in stdout
     console.log(stdout);
     // test command log for success indication
     // test for substr in stdout
     if(stdout.indexOf('Processing Complete:') > -1) {
       $message = 'ingest drush command success';
       console.log($message);
       status.push("$message");
       //return $message;
     }// end if
     else {
       $message = 'second ingest command failed!';
       console.log($message);
       status.push("$message");
       return $message;
     }//end else
    });
  }// end if
  return $message;
}// end function
//export default ingestion;
ingestion(target,parentpid,namespace,model);

<|endoftext|>


<commit_before>var fs = require('fs');

var chai = require('chai');
var lupa = require('../lupa');

var expect = chai.expect;

describe("Lupa", function () {
    // TODO promises and chai are not working together very well
    // when error there is a timeout rather than proper text message
    // maybe try Chai-as-promised or something else.
    var datasets = [
        {
            name: 'mocks',
            disabled: false,
            verify: function verify(data) {
                console.log("Output data: ", JSON.stringify(data, null, 2));

                var name1 = 'mocks/funcMock.js';
                var name2 = 'mocks/1/objMock.js';
                expect(data).to.have.property(name1);
                expect(data).to.have.property(name2);
                expect(data[name1].file).to.equal(name1);
                expect(data[name1].abc).to.exist();
                expect(data[name1].abc.length).to.equal(2);

                expect(data[name2].file).to.equal(name2);
                expect(data[name2].abc).to.have.length(0);
                return data;
            },
            filePattern: "mocks/**/*.js",
            get plugins() {
                var plugins = [];
                plugins.push(require('../plugins/RegExpPlugin')(/function +(\w+).*\(.*?\)/g, 'abc'));
                return plugins;
            },
        },
        {
            disabled: true,
            name: 'phaser',
            verify: function(data) {
                console.log("Output data: ", JSON.stringify(data, null, 2));

                //TODO this is temporary
                data.root = data["../../resources/phaser/src/core/State.js"];

                var out ='templates/data.json';
                //console.log("Write data to " +  out);
                fs.writeFileSync(out, JSON.stringify(data,null,2), 'utf8');

                // TODO make proper test case
                expect(data).to.have.property("LinkedList");
                expect(data).to.have.property("ArrayUtils");
                expect(data.ArrayUtils[1]).to.have.length(2);

                return data;
            },
            filePattern: '../../resources/phaser/src/**/*.js',
            get plugins() {
                var plugins = [];
                plugins.push(require('../plugins/RegExpDependencyPlugin')(/Phaser\.(\w+)/g, 1));
                return plugins;
            },
            //mappers: ['../mappers/HumanReadableMapper', '../mappers/GraphMapper']
        },
        {
            name: 'commonjs mocks',
            //mappers: ['../mappers/CommonJSGraphMapper'],
            verify: function (data) {
                console.log("DATA", JSON.stringify(data,null,2));
                //TODO this is temporary. Move this code to Gruntfile
                // and make grunt tasks
                // but now: run from src directory, and check manually
                // what is produced
                var out ='templates/data2.json';
                //console.log("Write data to " +  out);
                fs.writeFileSync(out, JSON.stringify(data,null,2), 'utf8');

            },
            filePattern: 'mocks/commonJS/*.js',
            get plugins() {
                var plugins = [].map(require).map(function (Constr) {
                    return Constr();
                });
                plugins.push(require('../plugins/RegExpDependencyPlugin')(/require.*\( *(['"])(.*?)\1 *?\)/g, 2) )
                return plugins;
            },
        }
    ];

    function describeDataset (dataset) {
        if (dataset.disabled) {
            return;
        }

        describe("dataset `" + dataset.name + "`", function () {
            it("should return correct data structure (" + dataset.name + ")", function (done) {
                var promise = lupa.run(dataset.filePattern, dataset.plugins);

                if (dataset.mappers) {

                    dataset.mappers.map(require).map(function (factory) {
                        return factory();
                    })
                    .forEach(function (mapper) {
                        promise = promise.then(mapper);
                    })
                }

                promise
                .then(dataset.verify)
                .then(function (data) {
                    done();
                })

            });

        });
    }

    datasets.forEach(describeDataset);

});
<commit_msg>add test for RegExpDependencyPlugin<commit_after>var fs = require('fs');

var chai = require('chai');
var lupa = require('../lupa');

var expect = chai.expect;

describe("Lupa", function () {
    // TODO promises and chai are not working together very well
    // when error there is a timeout rather than proper text message
    // maybe try Chai-as-promised or something else.
    var datasets = [
        {
            name: 'mocks',
            disabled: false,
            verify: function verify(data) {
                console.log("Output data: ", JSON.stringify(data, null, 2));

                var name1 = 'mocks/funcMock.js';
                var name2 = 'mocks/1/objMock.js';
                expect(data).to.have.property(name1);
                expect(data).to.have.property(name2);
                expect(data[name1].file).to.equal(name1);
                expect(data[name1].abc).to.exist();
                expect(data[name1].abc.length).to.equal(2);

                expect(data[name2].file).to.equal(name2);
                expect(data[name2].abc).to.have.length(0);
                return data;
            },
            filePattern: "mocks/**/*.js",
            get plugins() {
                var plugins = [];
                plugins.push(require('../plugins/RegExpPlugin')(/function +(\w+).*\(.*?\)/g, 'abc'));
                return plugins;
            },
        },
        {
            disabled: true,
            name: 'phaser',
            verify: function(data) {
                console.log("Output data: ", JSON.stringify(data, null, 2));

                //TODO this is temporary
                data.root = data["../../resources/phaser/src/core/State.js"];

                var out ='templates/data.json';
                //console.log("Write data to " +  out);
                fs.writeFileSync(out, JSON.stringify(data,null,2), 'utf8');

                // TODO make proper test case
                expect(data).to.have.property("LinkedList");
                expect(data).to.have.property("ArrayUtils");
                expect(data.ArrayUtils[1]).to.have.length(2);

                return data;
            },
            filePattern: '../../resources/phaser/src/**/*.js',
            get plugins() {
                var plugins = [];
                plugins.push(require('../plugins/RegExpDependencyPlugin')(/Phaser\.(\w+)/g, 1));
                return plugins;
            },
            //mappers: ['../mappers/HumanReadableMapper', '../mappers/GraphMapper']
        },
        {
            name: 'commonjs mocks',
            //mappers: ['../mappers/CommonJSGraphMapper'],
            verify: function (data) {
                console.log("DATA", JSON.stringify(data,null,2));
                var name = "mocks/commonJS/cat.js";
                expect(data[name]).to.exist().and.have.property('children');
                var children = data[name].children;
                expect(children).to.have.property('length').equal(4);
                expect(children).to.have.deep.property('[0].name').equal('milk');
                expect(children).to.have.deep.property('[1].name').equal('meat');
                expect(children).to.have.deep.property('[2].name').equal('water');
                expect(children).to.have.deep.property('[3].name').equal('fs');

                var out ='templates/data2.json';
                //console.log("Write data to " +  out);
                fs.writeFileSync(out, JSON.stringify(data,null,2), 'utf8');

            },
            filePattern: 'mocks/commonJS/*.js',
            get plugins() {
                var plugins = [].map(require).map(function (Constr) {
                    return Constr();
                });
                plugins.push(require('../plugins/RegExpDependencyPlugin')(/require.*\( *(['"])(.*?)\1 *?\)/g, 2) )
                return plugins;
            },
        }
    ];

    function describeDataset (dataset) {
        if (dataset.disabled) {
            return;
        }

        describe("dataset `" + dataset.name + "`", function () {
            it("should return correct data structure (" + dataset.name + ")", function (done) {
                var promise = lupa.run(dataset.filePattern, dataset.plugins);

                if (dataset.mappers) {

                    dataset.mappers.map(require).map(function (factory) {
                        return factory();
                    })
                    .forEach(function (mapper) {
                        promise = promise.then(mapper);
                    })
                }

                promise
                .then(dataset.verify)
                .then(function (data) {
                    done();
                })

            });

        });
    }

    datasets.forEach(describeDataset);

});
<|endoftext|>


<commit_before>/*jshint esnext:true*/
/*global QUnit*/

import {mix, createFactory, extendFactory, createdBy} from 'js/oop';

QUnit.module('oop', {
	beforeEach: function () {
		class Base {
			constructor (...args) {
				this.cArgs = args;
			}
			meth (...args) {
				this.methArgs = args;
			}
		}

		const mixO = {
			mixOM (...args) {
				this.mixOMArgs = args;
			}
		};

		function MixF (...args) {
			this.MixFCArgs = args;
		}
		MixF.prototype = {
			mixFM (...args) {
				this.mixFMArgs = args;
			}
		};

		this.Base = Base;
		this.mixO = mixO;
		this.MixF = MixF;
	}
});

QUnit.test('mix an object into a class', function (assert) {
	mix(this.Base.prototype, this.mixO);

	assert.ok(this.Base.prototype.meth);	// original Base method
	assert.ok(this.Base.prototype.mixOM);	// mixed-in method from mixO

	let bi = new this.Base(1337);

	assert.deepEqual([1337], bi.cArgs);

	bi.meth(1338);
	assert.deepEqual([1338], bi.methArgs);

	bi.mixOM(1339);
	assert.deepEqual([1339], bi.mixOMArgs);
});

QUnit.test('mix a Function into a class', function (assert) {
	mix(this.Base.prototype, this.MixF.prototype);

	assert.ok(this.Base.prototype.meth);	// original Base method
	assert.ok(this.Base.prototype.mixFM);	// mixed-in method from MixF

	let bi = new this.Base(1337);

	assert.deepEqual([1337], bi.cArgs);
	assert.notOk(bi.mixFCArgs);	// MixF constructor not called

	bi.meth(1338);
	assert.deepEqual([1338], bi.methArgs);

	bi.mixFM(1339);
	assert.deepEqual([1339], bi.mixFMArgs);
});

QUnit.test('a Function and an Object walk into a bar...', function (assert) {
	mix(this.Base.prototype, this.mixO, this.MixF.prototype, {
		order (drink) {
			this.order = drink;
		}
	});

	assert.ok(this.Base.prototype.meth);	// original Base method
	assert.ok(this.Base.prototype.mixOM);	// mixed-in method from mixO
	assert.ok(this.Base.prototype.mixFM);	// mixed-in method from MixF
	assert.ok(this.Base.prototype.order);	// mixed-in method from new object

	let bi = new this.Base(1337);

	assert.deepEqual([1337], bi.cArgs);
	assert.notOk(bi.mixFCArgs);	// MixF constructor not called

	bi.meth(1338);
	assert.deepEqual([1338], bi.methArgs);

	bi.mixOM(1339);
	assert.deepEqual([1339], bi.mixOMArgs);

	bi.mixFM(8008);
	assert.deepEqual([8008], bi.mixFMArgs);

	bi.order('coffee');
	assert.equal('coffee', bi.order);
});

QUnit.test('createFactory', function (assert) {
	// region base factory
	const createBase = createFactory({
		baseM () {
			return 'baseM ' + this.baseInit.length;
		},
		other () {
			return 'other';
		}
	}, function (...args) {
		this.baseInit = args;
	});

	const bi = createBase('base');

	assert.ok(createdBy(bi, createBase), 'createdBy recognizes object\'s factory');

	assert.ok(Object.getPrototypeOf(bi) === createBase.prototype);
	assert.deepEqual(['base'], bi.baseInit);
	assert.equal('baseM 1', bi.baseM());
	assert.equal('other', bi.other());
	// endregion

	// region now extend base factory (without init)
	const createSub = extendFactory(createBase, {
		baseM () {
			return 'sub' + createBase.prototype.baseM.apply(this);
		}
	});

	const si = createSub('sup', 'dude');

	assert.ok(createdBy(si, createSub), 'createdBy recognizes object\'s extended factory');
	assert.ok(createdBy(si, createBase), 'createdBy recognizes object\'s base factory');

	assert.ok(Object.getPrototypeOf(si) === createSub.prototype);
	assert.deepEqual(['sup', 'dude'], si.baseInit);
	assert.equal('subbaseM 2', si.baseM());
	assert.equal('other', si.other());
	// endregion

	// region check relations between base and sub factories
	assert.ok(Object.getPrototypeOf(createSub.prototype) === createBase.prototype);
	assert.ok(Object.getPrototypeOf(Object.getPrototypeOf(si)) === Object.getPrototypeOf(bi));
	// endregion

	// region try two init functions
	const createSub2 = extendFactory(createSub, {}, function (superInit, ...args) {
		superInit();
		this.sub2Init = args;
	});
	const createSub3 = extendFactory(createSub2, {}, function (superInit, ...args) {
		superInit();
		this.sub3Init = args;
	});

	const s3i = createSub3('init');
	assert.deepEqual(['init'], s3i.baseInit);
	assert.deepEqual(s3i.baseInit, s3i.sub2Init);
	assert.deepEqual(s3i.baseInit, s3i.sub3Init);
	// endregion
});

QUnit.test('factory.mix', function (assert) {
	const base = createFactory({
		f: 'f'
	}).mix({
		m1: 'm1'
	}, {
		m2: 'm2'
	});

	const bo = base();
	assert.equal(bo.f, 'f');
	assert.equal(bo.m1, 'm1');
	assert.equal(bo.m2, 'm2');

	// extend and mix again
	const ext = extendFactory(base, {
		e: 'e'
	}).mix({
		m3: 'm3'
	});

	const eo = ext();
	assert.equal(eo.f, 'f');
	assert.equal(eo.m1, 'm1');
	assert.equal(eo.m2, 'm2');
	assert.equal(eo.e, 'e');
	assert.equal(eo.m3, 'm3');
});
<commit_msg>Extended test for oop/factory.mix<commit_after>/*jshint esnext:true*/
/*global QUnit*/

import {mix, createFactory, extendFactory, createdBy} from 'js/oop';

QUnit.module('oop', {
	beforeEach: function () {
		class Base {
			constructor (...args) {
				this.cArgs = args;
			}
			meth (...args) {
				this.methArgs = args;
			}
		}

		const mixO = {
			mixOM (...args) {
				this.mixOMArgs = args;
			}
		};

		function MixF (...args) {
			this.MixFCArgs = args;
		}
		MixF.prototype = {
			mixFM (...args) {
				this.mixFMArgs = args;
			}
		};

		this.Base = Base;
		this.mixO = mixO;
		this.MixF = MixF;
	}
});

QUnit.test('mix an object into a class', function (assert) {
	mix(this.Base.prototype, this.mixO);

	assert.ok(this.Base.prototype.meth);	// original Base method
	assert.ok(this.Base.prototype.mixOM);	// mixed-in method from mixO

	let bi = new this.Base(1337);

	assert.deepEqual([1337], bi.cArgs);

	bi.meth(1338);
	assert.deepEqual([1338], bi.methArgs);

	bi.mixOM(1339);
	assert.deepEqual([1339], bi.mixOMArgs);
});

QUnit.test('mix a Function into a class', function (assert) {
	mix(this.Base.prototype, this.MixF.prototype);

	assert.ok(this.Base.prototype.meth);	// original Base method
	assert.ok(this.Base.prototype.mixFM);	// mixed-in method from MixF

	let bi = new this.Base(1337);

	assert.deepEqual([1337], bi.cArgs);
	assert.notOk(bi.mixFCArgs);	// MixF constructor not called

	bi.meth(1338);
	assert.deepEqual([1338], bi.methArgs);

	bi.mixFM(1339);
	assert.deepEqual([1339], bi.mixFMArgs);
});

QUnit.test('a Function and an Object walk into a bar...', function (assert) {
	mix(this.Base.prototype, this.mixO, this.MixF.prototype, {
		order (drink) {
			this.order = drink;
		}
	});

	assert.ok(this.Base.prototype.meth);	// original Base method
	assert.ok(this.Base.prototype.mixOM);	// mixed-in method from mixO
	assert.ok(this.Base.prototype.mixFM);	// mixed-in method from MixF
	assert.ok(this.Base.prototype.order);	// mixed-in method from new object

	let bi = new this.Base(1337);

	assert.deepEqual([1337], bi.cArgs);
	assert.notOk(bi.mixFCArgs);	// MixF constructor not called

	bi.meth(1338);
	assert.deepEqual([1338], bi.methArgs);

	bi.mixOM(1339);
	assert.deepEqual([1339], bi.mixOMArgs);

	bi.mixFM(8008);
	assert.deepEqual([8008], bi.mixFMArgs);

	bi.order('coffee');
	assert.equal('coffee', bi.order);
});

QUnit.test('createFactory', function (assert) {
	// region base factory
	const createBase = createFactory({
		baseM () {
			return 'baseM ' + this.baseInit.length;
		},
		other () {
			return 'other';
		}
	}, function (...args) {
		this.baseInit = args;
	});

	const bi = createBase('base');

	assert.ok(createdBy(bi, createBase), 'createdBy recognizes object\'s factory');

	assert.ok(Object.getPrototypeOf(bi) === createBase.prototype);
	assert.deepEqual(['base'], bi.baseInit);
	assert.equal('baseM 1', bi.baseM());
	assert.equal('other', bi.other());
	// endregion

	// region now extend base factory (without init)
	const createSub = extendFactory(createBase, {
		baseM () {
			return 'sub' + createBase.prototype.baseM.apply(this);
		}
	});

	const si = createSub('sup', 'dude');

	assert.ok(createdBy(si, createSub), 'createdBy recognizes object\'s extended factory');
	assert.ok(createdBy(si, createBase), 'createdBy recognizes object\'s base factory');

	assert.ok(Object.getPrototypeOf(si) === createSub.prototype);
	assert.deepEqual(['sup', 'dude'], si.baseInit);
	assert.equal('subbaseM 2', si.baseM());
	assert.equal('other', si.other());
	// endregion

	// region check relations between base and sub factories
	assert.ok(Object.getPrototypeOf(createSub.prototype) === createBase.prototype);
	assert.ok(Object.getPrototypeOf(Object.getPrototypeOf(si)) === Object.getPrototypeOf(bi));
	// endregion

	// region try two init functions
	const createSub2 = extendFactory(createSub, {}, function (superInit, ...args) {
		superInit();
		this.sub2Init = args;
	});
	const createSub3 = extendFactory(createSub2, {}, function (superInit, ...args) {
		superInit();
		this.sub3Init = args;
	});

	const s3i = createSub3('init');
	assert.deepEqual(['init'], s3i.baseInit);
	assert.deepEqual(s3i.baseInit, s3i.sub2Init);
	assert.deepEqual(s3i.baseInit, s3i.sub3Init);
	// endregion
});

QUnit.test('factory.mix', function (assert) {
	const createBase = createFactory({
		f: 'f'
	});

	const bi = createBase();

	createBase.mix({
		m1: 'm1'
	}, {
		m2: 'm2'
	});

	assert.equal(bi.f, 'f');
	assert.equal(bi.m1, 'm1');
	assert.equal(bi.m2, 'm2');

	// extend and mix again
	const createSub = extendFactory(createBase, {
		e: 'e'
	}).mix({
		m3: 'm3'
	});

	const si = createSub();
	assert.equal(si.f, 'f');
	assert.equal(si.m1, 'm1');
	assert.equal(si.m2, 'm2');
	assert.equal(si.e, 'e');
	assert.equal(si.m3, 'm3');
});
<|endoftext|>


<commit_before>import { Model } from '../model';
import { Relationship } from '../relationship';

export class TestType extends Model {}

export class Children extends Relationship {}
export class ValenceChildren extends Relationship {}
export class Likes extends Relationship {}
export class Agrees extends Relationship {}
export class QueryChildren extends Relationship {}

Children.$name = 'children';
Children.$sides = {
  parents: {
    self: {
      field: 'child_id',
      type: 'tests',
    },
    other: {
      field: 'parent_id',
      type: 'tests',
      title: 'children',
    },
  },
  children: {
    self: {
      field: 'parent_id',
      type: 'tests',
    },
    other: {
      field: 'child_id',
      type: 'tests',
      title: 'parents',
    },
  },
};

Likes.$sides = {
  likers: {
    self: {
      field: 'child_id',
      type: 'tests',
    },
    other: {
      field: 'parent_id',
      type: 'tests',
      title: 'likees',
    },
  },
  likees: {
    self: {
      field: 'parent_id',
      type: 'tests',
    },
    other: {
      field: 'child_id',
      type: 'tests',
      title: 'likers',
    },
  },
};

Likes.$restrict = {
  reaction: {
    type: 'string',
    value: 'like',
  },
};
Likes.$name = 'reactions';
Agrees.$sides = {
  agreers: {
    self: {
      field: 'child_id',
      type: 'tests',
    },
    other: {
      field: 'parent_id',
      type: 'tests',
      title: 'agreees',
    },
  },
  agreees: {
    self: {
      field: 'parent_id',
      type: 'tests',
    },
    other: {
      field: 'child_id',
      type: 'tests',
      title: 'agreers',
    },
  },
};

Agrees.$restrict = {
  reaction: {
    type: 'string',
    value: 'agree',
  },
};
Agrees.$name = 'reactions';

ValenceChildren.$sides = {
  valenceParents: {
    self: {
      field: 'child_id',
      type: 'tests',
    },
    other: {
      field: 'parent_id',
      type: 'tests',
      title: 'valenceChildren',
    },
  },
  valenceChildren: {
    self: {
      field: 'parent_id',
      type: 'tests',
    },
    other: {
      field: 'child_id',
      type: 'tests',
      title: 'valenceParents',
    },
  },
};

ValenceChildren.$extras = {
  perm: {
    type: 'number',
  },
};
ValenceChildren.$name = 'valence_children';

QueryChildren.$sides = {
  queryParents: {
    self: {
      field: 'child_id',
      type: 'tests',
      query: {
        logic: ['where', ['where', 'child_id', '=', '{id}'], ['where', 'perm', '>=', 2]],
        requireLoad: true,
      },
    },
    other: {
      field: 'parent_id',
      type: 'tests',
      title: 'queryChildren',
    },
  },
  queryChildren: {
    self: {
      field: 'parent_id',
      type: 'tests',
      query: {
        logic: ['where', ['where', 'parent_id', '=', '{id}'], ['where', 'perm', '>=', 2]],
        requireLoad: true,
      },
    },
    other: {
      field: 'child_id',
      type: 'tests',
      title: 'queryParents',
    },
  },
};
QueryChildren.$extras = {
  perm: {
    type: 'number',
  },
};

QueryChildren.$name = 'valence_children';

TestType.$name = 'tests';
TestType.$id = 'id';
TestType.$packageIncludes = ['children'];
TestType.$fields = {
  id: {
    type: 'number',
  },
  name: {
    type: 'string',
  },
  extended: {
    type: 'object',
    default: {},
  },
  children: {
    type: 'hasMany',
    relationship: Children,
  },
  valenceChildren: {
    type: 'hasMany',
    relationship: ValenceChildren,
  },
  parents: {
    type: 'hasMany',
    relationship: Children,
  },
  queryChildren: {
    type: 'hasMany',
    readonly: true,
    relationship: QueryChildren,
  },
  queryParents: {
    type: 'hasMany',
    readonly: true,
    relationship: QueryChildren,
  },
  valenceParents: {
    type: 'hasMany',
    relationship: ValenceChildren,
  },
  likers: {
    type: 'hasMany',
    relationship: Likes,
  },
  likees: {
    type: 'hasMany',
    relationship: Likes,
  },
  agreers: {
    type: 'hasMany',
    relationship: Agrees,
  },
  agreees: {
    type: 'hasMany',
    relationship: Agrees,
  },
};
TestType.$include = {
  children: {
    attributes: ['name', 'extended'],
    relationships: ['children'],
    depth: Infinity,
  },
};
<commit_msg>rename children colum<commit_after>import { Model } from '../model';
import { Relationship } from '../relationship';

export class TestType extends Model {}

export class Children extends Relationship {}
export class ValenceChildren extends Relationship {}
export class Likes extends Relationship {}
export class Agrees extends Relationship {}
export class QueryChildren extends Relationship {}

Children.$name = 'parent_child_relationship';
Children.$sides = {
  parents: {
    self: {
      field: 'child_id',
      type: 'tests',
    },
    other: {
      field: 'parent_id',
      type: 'tests',
      title: 'children',
    },
  },
  children: {
    self: {
      field: 'parent_id',
      type: 'tests',
    },
    other: {
      field: 'child_id',
      type: 'tests',
      title: 'parents',
    },
  },
};

Likes.$sides = {
  likers: {
    self: {
      field: 'child_id',
      type: 'tests',
    },
    other: {
      field: 'parent_id',
      type: 'tests',
      title: 'likees',
    },
  },
  likees: {
    self: {
      field: 'parent_id',
      type: 'tests',
    },
    other: {
      field: 'child_id',
      type: 'tests',
      title: 'likers',
    },
  },
};

Likes.$restrict = {
  reaction: {
    type: 'string',
    value: 'like',
  },
};
Likes.$name = 'reactions';
Agrees.$sides = {
  agreers: {
    self: {
      field: 'child_id',
      type: 'tests',
    },
    other: {
      field: 'parent_id',
      type: 'tests',
      title: 'agreees',
    },
  },
  agreees: {
    self: {
      field: 'parent_id',
      type: 'tests',
    },
    other: {
      field: 'child_id',
      type: 'tests',
      title: 'agreers',
    },
  },
};

Agrees.$restrict = {
  reaction: {
    type: 'string',
    value: 'agree',
  },
};
Agrees.$name = 'reactions';

ValenceChildren.$sides = {
  valenceParents: {
    self: {
      field: 'child_id',
      type: 'tests',
    },
    other: {
      field: 'parent_id',
      type: 'tests',
      title: 'valenceChildren',
    },
  },
  valenceChildren: {
    self: {
      field: 'parent_id',
      type: 'tests',
    },
    other: {
      field: 'child_id',
      type: 'tests',
      title: 'valenceParents',
    },
  },
};

ValenceChildren.$extras = {
  perm: {
    type: 'number',
  },
};
ValenceChildren.$name = 'valence_children';

QueryChildren.$sides = {
  queryParents: {
    self: {
      field: 'child_id',
      type: 'tests',
      query: {
        logic: ['where', ['where', 'child_id', '=', '{id}'], ['where', 'perm', '>=', 2]],
        requireLoad: true,
      },
    },
    other: {
      field: 'parent_id',
      type: 'tests',
      title: 'queryChildren',
    },
  },
  queryChildren: {
    self: {
      field: 'parent_id',
      type: 'tests',
      query: {
        logic: ['where', ['where', 'parent_id', '=', '{id}'], ['where', 'perm', '>=', 2]],
        requireLoad: true,
      },
    },
    other: {
      field: 'child_id',
      type: 'tests',
      title: 'queryParents',
    },
  },
};
QueryChildren.$extras = {
  perm: {
    type: 'number',
  },
};

QueryChildren.$name = 'valence_children';

TestType.$name = 'tests';
TestType.$id = 'id';
TestType.$packageIncludes = ['children'];
TestType.$fields = {
  id: {
    type: 'number',
  },
  name: {
    type: 'string',
  },
  extended: {
    type: 'object',
    default: {},
  },
  children: {
    type: 'hasMany',
    relationship: Children,
  },
  valenceChildren: {
    type: 'hasMany',
    relationship: ValenceChildren,
  },
  parents: {
    type: 'hasMany',
    relationship: Children,
  },
  queryChildren: {
    type: 'hasMany',
    readonly: true,
    relationship: QueryChildren,
  },
  queryParents: {
    type: 'hasMany',
    readonly: true,
    relationship: QueryChildren,
  },
  valenceParents: {
    type: 'hasMany',
    relationship: ValenceChildren,
  },
  likers: {
    type: 'hasMany',
    relationship: Likes,
  },
  likees: {
    type: 'hasMany',
    relationship: Likes,
  },
  agreers: {
    type: 'hasMany',
    relationship: Agrees,
  },
  agreees: {
    type: 'hasMany',
    relationship: Agrees,
  },
};
TestType.$include = {
  children: {
    attributes: ['name', 'extended'],
    relationships: ['children'],
    depth: Infinity,
  },
};
<|endoftext|>


<commit_before>import React from 'react'
import { injectGlobal } from 'styled-components';

// Define what props.theme will look like
const Llamas = {
    primary: '#8688BA',
    strongPrimary: 'rebeccapurple',
    secondary: '#7EC7F8',
    light: '#F7FCFF',
    middle: '#A6B0BF',
    dark: '#403B46',
    font: 'Roboto-Light'
};

export default Llamas<commit_msg>Adding sans-serif as alternative font.<commit_after>import React from 'react'
import { injectGlobal } from 'styled-components';

// Define what props.theme will look like
const Llamas = {
    primary: '#8688BA',
    strongPrimary: 'rebeccapurple',
    secondary: '#7EC7F8',
    light: '#F7FCFF',
    middle: '#A6B0BF',
    dark: '#403B46',
    font: '"Roboto-Light", sans-serif'
};

export default Llamas<|endoftext|>


<commit_before>// kdb.js - small library to access Elektra's kdb via node.js
const { exec } = require('child_process')

// constants
const ERR_KEY_NOT_FOUND = 'Did not find key'

// KDBError
function KDBError (message) {
    this.name = 'KDBError'
    this.message = message || ''
}
KDBError.prototype = Error.prototype

// remove newline from the end of a string
const trimNewline = (str) =>
  str.substring(0, str.length - 1)

// execute a script while catching and parsing errors
const safeExec = (script) => new Promise((resolve, reject) =>
  exec(script, (err, stdout, stderr) => {
    if (err) {
      const errors = err.message.split('\n')
      // ignore error if it's "key not found"
      if (!(errors.length > 1 && errors[1] === ERR_KEY_NOT_FOUND)) {
        return reject(err)
      }
    }
    if (stderr) {
      if (stderr !== ERR_KEY_NOT_FOUND) {
        return reject(new KDBError(stderr))
      } else {
        return resolve() // no key found, return no value
      }
    }
    const result = trimNewline(stdout)
    if (!result) {
      return resolve() // empty result, return no value
    }
    return resolve(result)
  })
)

// escape strings by surrounding them with ""
const escapeValues = (template, ...values) =>
  template.reduce((acc, part, i) => {
    /*
    Explanation of regular expression:
    - $1 `(\\\\)*` Matches an even number of _backslashes_
    - $4 Matches one _quote_ when there was an odd number of backslashes
    - $5 Matches one _quote_ when there was an even number of backslashes

    For instance in `\\\\\"`, $1 is `\\\\`, $4 is `"` and $5 is an empty string.
    So `\\\\\"` gets replaced to itself.
    In case of an even number of backslashes one backslash is added.

    (source: @krit0n - https://github.com/ElektraInitiative/libelektra/pull/983#discussion_r83965059)
    */
    let val = values[i - 1].replace(/((\\\\)*)(\\(")|("))/g, '$1\\$4$5'))
    if (typeof val === 'string') val = `"${val}"`
    return acc + val + part
  })

const ELEKTRA_VERSION_REGEX = /KDB_VERSION\:\ ([0-9]+\.[0-9]+\.[0-9]+)\n/

// get Elektra version
const version = () =>
  safeExec(`kdb --version`)
    .then(output => { // parse result
      const matches = ELEKTRA_VERSION_REGEX.exec(output)
      if (!matches) {
        throw new Error('invalid version: ' + output)
      }
      return matches
    })
    .then(matches => matches.length > 1 && matches[1]) // select version from matches
    .then(fullVersion => {
      const splitVersions = fullVersion.split('.')
      return {
        version: fullVersion,
        major: Number(splitVersions[0]),
        minor: Number(splitVersions[1]),
        micro: Number(splitVersions[2])
      }
    })

// list available paths under a given `path`
const ls = (path) =>
  safeExec(escapeValues`kdb ls ${path}`)
    .then(stdout => stdout && stdout.split('\n'))

// get value from given `path`
const get = (path) =>
  safeExec(escapeValues`kdb get ${path}`)

// set value at given `path`
const set = (path, value) =>
  safeExec(escapeValues`kdb set ${path} ${value}`)

// remove value at given `path`
const rm = (path) =>
  safeExec(escapeValues`kdb rm ${path}`)

// export javascript object from given `path`
const _export = (path) =>
  safeExec(escapeValues`kdb export ${path} yajl`)
    .then(stdout => stdout && JSON.parse(stdout))

// import javascript object at given `path`
const _import = (path, value) =>
  safeExec(
    `echo "${JSON.stringify(value).replace(/\"/g, '\\"')}" | ` + // pipe json into kdb
    escapeValues`kdb import ${path} yajl`
  ).then(result => _export(path))

// get value and available paths under a given `path`
const getAndLs = (path) =>
  Promise.all(
    [ ls(path), get(path) ] // execute ls and get in parallel
  ).then(([ lsRes, value ]) => {
    if (lsRes) {
      return { ls: lsRes, value } // return results as object
    }
  })

// export kdb functions as `kdb` object
module.exports = {
  version, ls, get, getAndLs, set, rm, export: _export, import: _import
}
<commit_msg>fix typo in regex<commit_after>// kdb.js - small library to access Elektra's kdb via node.js
const { exec } = require('child_process')

// constants
const ERR_KEY_NOT_FOUND = 'Did not find key'

// KDBError
function KDBError (message) {
    this.name = 'KDBError'
    this.message = message || ''
}
KDBError.prototype = Error.prototype

// remove newline from the end of a string
const trimNewline = (str) =>
  str.substring(0, str.length - 1)

// execute a script while catching and parsing errors
const safeExec = (script) => new Promise((resolve, reject) =>
  exec(script, (err, stdout, stderr) => {
    if (err) {
      const errors = err.message.split('\n')
      // ignore error if it's "key not found"
      if (!(errors.length > 1 && errors[1] === ERR_KEY_NOT_FOUND)) {
        return reject(err)
      }
    }
    if (stderr) {
      if (stderr !== ERR_KEY_NOT_FOUND) {
        return reject(new KDBError(stderr))
      } else {
        return resolve() // no key found, return no value
      }
    }
    const result = trimNewline(stdout)
    if (!result) {
      return resolve() // empty result, return no value
    }
    return resolve(result)
  })
)

// escape strings by surrounding them with ""
const escapeValues = (template, ...values) =>
  template.reduce((acc, part, i) => {
    /*
    Explanation of regular expression:
    - $1 `(\\\\)*` Matches an even number of _backslashes_
    - $4 Matches one _quote_ when there was an odd number of backslashes
    - $5 Matches one _quote_ when there was an even number of backslashes

    For instance in `\\\\\"`, $1 is `\\\\`, $4 is `"` and $5 is an empty string.
    So `\\\\\"` gets replaced to itself.
    In case of an even number of backslashes one backslash is added.

    (source: @krit0n - https://github.com/ElektraInitiative/libelektra/pull/983#discussion_r83965059)
    */
    let val = values[i - 1].replace(/((\\\\)*)(\\(")|("))/g, '$1\\$4$5')
    if (typeof val === 'string') val = `"${val}"`
    return acc + val + part
  })

const ELEKTRA_VERSION_REGEX = /KDB_VERSION\:\ ([0-9]+\.[0-9]+\.[0-9]+)\n/

// get Elektra version
const version = () =>
  safeExec(`kdb --version`)
    .then(output => { // parse result
      const matches = ELEKTRA_VERSION_REGEX.exec(output)
      if (!matches) {
        throw new Error('invalid version: ' + output)
      }
      return matches
    })
    .then(matches => matches.length > 1 && matches[1]) // select version from matches
    .then(fullVersion => {
      const splitVersions = fullVersion.split('.')
      return {
        version: fullVersion,
        major: Number(splitVersions[0]),
        minor: Number(splitVersions[1]),
        micro: Number(splitVersions[2])
      }
    })

// list available paths under a given `path`
const ls = (path) =>
  safeExec(escapeValues`kdb ls ${path}`)
    .then(stdout => stdout && stdout.split('\n'))

// get value from given `path`
const get = (path) =>
  safeExec(escapeValues`kdb get ${path}`)

// set value at given `path`
const set = (path, value) =>
  safeExec(escapeValues`kdb set ${path} ${value}`)

// remove value at given `path`
const rm = (path) =>
  safeExec(escapeValues`kdb rm ${path}`)

// export javascript object from given `path`
const _export = (path) =>
  safeExec(escapeValues`kdb export ${path} yajl`)
    .then(stdout => stdout && JSON.parse(stdout))

// import javascript object at given `path`
const _import = (path, value) =>
  safeExec(
    `echo "${JSON.stringify(value).replace(/\"/g, '\\"')}" | ` + // pipe json into kdb
    escapeValues`kdb import ${path} yajl`
  ).then(result => _export(path))

// get value and available paths under a given `path`
const getAndLs = (path) =>
  Promise.all(
    [ ls(path), get(path) ] // execute ls and get in parallel
  ).then(([ lsRes, value ]) => {
    if (lsRes) {
      return { ls: lsRes, value } // return results as object
    }
  })

// export kdb functions as `kdb` object
module.exports = {
  version, ls, get, getAndLs, set, rm, export: _export, import: _import
}
<|endoftext|>


<commit_before>/*
 * biojs-registry-workmen
 * https://github.com/biojs/biojs-registry-workmen
 *
 * Copyright (c) 2014 BioJS
 * Licensed under the Apache 2
 */

var registry = require("npm-registry");
var npm = new registry();
var info = require("./npm");
var github = require('./github');
var mquery = require('mquery');

var workmen = function(callback){
  var keywords = ['biojs', 'bionode'];
  this.callback = callback;
  self = this;

  this.info = new info(self.save, keywords);

  // load additional info crawlers

  var gitInstance = new github(this.info);

  if(this.isMongo()){
    this.loadMongo(this.loadCache);
  }else{
    this.loadNeDB(this.loadCache);
  }
}

workmen.prototype.loadNeDB= function(callback){
  var Datastore = require('nedb');
  this._db = new Datastore({ filename: __dirname + '/../.necache', autoload: true });
  // name has to uniq
  this._db.ensureIndex({ fieldName: 'name', unique: true, sparse: true });
  this._db.findOneAndUpdate = this._db.update;
  callback();
}
workmen.prototype.getMongoUri = function(){
  return mongoUri = process.env.MONGOLAB_URI ||
    process.env.MONGOHQ_URL || 'mongodb://localhost/test';
}

workmen.prototype.isMongo = function(){
  if( process.env.MONGOLAB_URI || process.env.MONGOHQ_URL || process.env.MONGO ){
    return true;
  }
  return false;
}

workmen.prototype.loadMongo = function(callback){
  var mongo = require('mongodb');

  mongo.Db.connect(this.getMongoUri(), function (err, dbM) {
    var npmcache = dbM.collection('npmcache');
    self._mdb = npmcache;

    npmcache.ensureIndex( { "name": 1 }, { unique: true }, callback)
    //callback();
  });
};

workmen.prototype.db = function(){
  if(self._mdb !== undefined){
    return mquery(self._mdb);
  }else{
    return self._db;
  }
}

// load cache
workmen.prototype.loadCache = function(){
  self.db().find({}).exec(function (err, docs) {
    self.reload(docs);
  });
};

workmen.prototype.reload = function(existPkgs){
  // TODO: use cache
  self.db().remove({}, function (err, numRemoved) {});
  npm.packages.keyword('biojs', function(err, packages){
    if(err != undefined){
      console.log(err); return;
    }
    self.info.start(packages);
  });
};

workmen.prototype.save = function(pkgs){
  console.log("saving db");
  var counts = 0;
  var expected = Object.keys(pkgs).length;
  var finalTrigger = function(){
    counts++;
    if(expected == counts){
      self.callback(pkgs, self.db);
    }
  }

  for(var i in pkgs){
    self.db().findOneAndUpdate({ name: pkgs[i] }, pkgs[i], { upsert: true }, function (err, numReplaced, upsert) {
      finalTrigger();
    });
  }
}

module.exports = workmen;
<commit_msg>support for multiple tags<commit_after>/*
 * biojs-registry-workmen
 * https://github.com/biojs/biojs-registry-workmen
 *
 * Copyright (c) 2014 BioJS
 * Licensed under the Apache 2
 */

var registry = require("npm-registry");
var npm = new registry();
var info = require("./npm");
var github = require('./github');
var mquery = require('mquery');

var workmen = function(callback){
  this.keywords = ['biojs', 'bionode'];
  this.callback = callback;
  self = this;

  this.info = new info(self.save, self.keywords);

  // load additional info crawlers

  var gitInstance = new github(this.info);

  if(this.isMongo()){
    this.loadMongo(this.loadCache);
  }else{
    this.loadNeDB(this.loadCache);
  }
}

workmen.prototype.loadNeDB= function(callback){
  var Datastore = require('nedb');
  this._db = new Datastore({ filename: __dirname + '/../.necache', autoload: true });
  // name has to uniq
  this._db.ensureIndex({ fieldName: 'name', unique: true, sparse: true });
  this._db.findOneAndUpdate = this._db.update;
  callback();
}
workmen.prototype.getMongoUri = function(){
  return mongoUri = process.env.MONGOLAB_URI ||
    process.env.MONGOHQ_URL || 'mongodb://localhost/test';
}

workmen.prototype.isMongo = function(){
  if( process.env.MONGOLAB_URI || process.env.MONGOHQ_URL || process.env.MONGO ){
    return true;
  }
  return false;
}

workmen.prototype.loadMongo = function(callback){
  var mongo = require('mongodb');

  mongo.Db.connect(this.getMongoUri(), function (err, dbM) {
    var npmcache = dbM.collection('npmcache');
    self._mdb = npmcache;

    npmcache.ensureIndex( { "name": 1 }, { unique: true }, callback)
      //callback();
  });
};

workmen.prototype.db = function(){
  if(self._mdb !== undefined){
    return mquery(self._mdb);
  }else{
    return self._db;
  }
}

// load cache
workmen.prototype.loadCache = function(){
  self.db().find({}).exec(function (err, docs) {
    self.reload(docs);
  });
};

workmen.prototype.reload = function(existPkgs){
  // TODO: use cache
  self.db().remove({}, function (err, numRemoved) {});

  var counter = 0;
  var expected = self.keywords.length;
  var pkgs = {};

  var finalTrigger = function(){
    counter++;
    if(expected == counter){
      self.info.start(pkgs);
    }
  }

  function downloadKeys(keyword){
    npm.packages.keyword(keyword, function(err, packages){
      if(err == undefined){
        console.log("received keyword:", keyword);
        for(var key in packages){
          // filter duplicates
          if(! (key in pkgs)){
            pkgs[key] = packages[key];
          }
        }
      }else{
        console.log("err", err);
      }
      finalTrigger();
    });
  }

  for(var index in self.keywords){
    console.log("started keyword:", self.keywords[index]);
    downloadKeys(self.keywords[index])
  };
}

workmen.prototype.save = function(pkgs){
  console.log("saving db");
  var counts = 0;
  var expected = Object.keys(pkgs).length;
  var finalTrigger = function(){
    counts++;
    if(expected == counts){
      self.callback(pkgs, self.db);
    }
  }

  for(var i in pkgs){
    self.db().findOneAndUpdate({ name: pkgs[i] }, pkgs[i], { upsert: true }, function (err, numReplaced, upsert) {
      finalTrigger();
    });
  }
}

module.exports = workmen;
<|endoftext|>


<commit_before>'use strict';
/*jslint unparam: true, indent:2, nomen: true, plusplus: true, vars: true */
var lexer = require('./lexer');

exports = module.exports = function (anyDB) {

  function wrapConnect(conn) {
    conn.real_query = conn.query;
    conn.query = function (text, params, callback) {
      if (typeof params === 'function') {
        callback = params;
        params = {};
      }
      try {
        var tokens = lexer.tokenize(text);
        var s = text, i = 0, p = [];
        tokens.forEach(function (token) {
          if (token[0] === 'VAR') {
            i = i + 1;
            if (conn.adapter.name === 'postgres') {
              s = s.replace(token[1], '$' + i);
            } else {
              s = s.replace(token[1], '?');
            }
            p.push(token[1].substr(1, token[1].length));
          }
        });
        var prm = [];
        p.forEach(function (col) {
          if (params[col] === undefined) {
            prm.push(null);
          } else if (params[col] === null) {
            prm.push(null);
          } else {
            prm.push(params[col]);
          }
        });
        return conn.real_query(s, prm, callback);
      } catch (e) {
        callback('not found script' + e);
      }
    };
    return conn;
  }

  return {
    createConnection: function (url) {
      return wrapConnect(anyDB.createConnection(url));
    },
    connect: function (url) {
      return wrapConnect(anyDB.createConnection(url));
    },
    createPool: function (url, prm) {
      var pool = anyDB.createPool(url, prm);
      return wrapConnect(pool);
    },
    wrapConnect : wrapConnect
  };
};<commit_msg>append wrapConnect<commit_after>'use strict';
/*jslint unparam: true, indent:2, nomen: true, plusplus: true, vars: true */
var lexer = require('./lexer');

function wrapConnect(conn) {
  conn.real_query = conn.query;
  conn.query = function (text, params, callback) {
    if (typeof params === 'function') {
      callback = params;
      params = {};
    }
    try {
      var tokens = lexer.tokenize(text);
      var s = text, i = 0, p = [];
      tokens.forEach(function (token) {
        if (token[0] === 'VAR') {
          i = i + 1;
          if (conn.adapter.name === 'postgres') {
            s = s.replace(token[1], '$' + i);
          } else {
            s = s.replace(token[1], '?');
          }
          p.push(token[1].substr(1, token[1].length));
        }
      });
      var prm = [];
      p.forEach(function (col) {
        if (params[col] === undefined) {
          prm.push(null);
        } else if (params[col] === null) {
          prm.push(null);
        } else {
          prm.push(params[col]);
        }
      });
      return conn.real_query(s, prm, callback);
    } catch (e) {
      callback('not found script' + e);
    }
  };
  return conn;
}

exports = module.exports = function (anyDB) {
  return {
    createConnection: function (url) {
      return wrapConnect(anyDB.createConnection(url));
    },
    connect: function (url) {
      return wrapConnect(anyDB.createConnection(url));
    },
    createPool: function (url, prm) {
      var pool = anyDB.createPool(url, prm);
      return wrapConnect(pool);
    }
  };
};
module.exports.wrapConnect = wrapConnect;<|endoftext|>


<commit_before>include('uki.js');
/** @namespace */
uki.view = {
    declare: function(/*name, baseClasses, implementation*/) {
        var args  = uki.toArray(arguments),
            name  = args.shift(),
            klass = uki.newClass.apply(uki, args),
            parts = name.split('.'),
            obj   = root,
            i, part;

        klass.prototype.typeName = function() { return name; };
        for ( i=0; i < parts.length - 1; i++ ) {
            part = parts[i];
            if (!obj[part]) obj[part] = {};
            obj = obj[part];
        };
        obj[parts[parts.length - 1]] = klass;
        return klass;
    }
};<commit_msg>View: loops optimizations<commit_after>include('uki.js');
/** @namespace */
uki.view = {
    declare: function(/*name, baseClasses, implementation*/) {
        var args  = uki.toArray(arguments),
            name  = args.shift(),
            klass = uki.newClass.apply(uki, args),
            parts = name.split('.'),
            obj   = root,
            i, part, $partslen = parts.length;

        klass.prototype.typeName = function() { return name; };
        for ( i= $partslen - 2; i >= 0; i-- ) {
            part = parts[i];
            if (!obj[part]) obj[part] = {};
            obj = obj[part];
        };

        obj[ parts[$partslen - 1] ] = klass;
        return klass;
    }
};<|endoftext|>


<commit_before>$.fn.respondosketch = function(props) {

    var canvas = this;
    var ctx = $(this)[0].getContext('2d');

    var backgroundColor = props.backgroundColor;
    var aspectRatio = props.aspectRatio;

    var sketch = false;
    var drawing = new Array();

    var width = canvas.width();
    var height = canvas.height();

    var lineColor = 'black';
    var lineSize = 5;

    $(window).resize(function(e) {
        var newWidth = canvas.parent().width();
        var newHeight = newWidth / aspectRatio;

        var xScale = newWidth/width;
        var yScale = newHeight/height;

        for (var i = 0; i < drawing.length; i++) {
            drawing[i].x *= xScale;
            drawing[i].y *= yScale;
        }

        setSize(newWidth, newHeight);

        redraw();
    });

    var startEvent = 'mousedown touchstart';
    canvas.on(startEvent, function(e) {
        if (e.type == 'touchstart') {
            e.preventDefault();
        }

        sketch = true;

        var mouse = getMouseLocation(this, e);

        drawing.push({
            x: mouse.x,
            y: mouse.y,
            type: e.type,
            color: lineColor,
            size: lineSize
        });

        redraw();
    });

    var moveEvent = 'mousemove touchmove';
    canvas.on(moveEvent, function(e) {
        var mouse = getMouseLocation(this, e);

        if (sketch) {
            drawing.push({
                x: mouse.x,
                y: mouse.y,
                type: e.type,
                color: lineColor,
                size: lineSize
            });
            redraw();
        }
    });

    var endEvent = 'mouseup mouseleave touchend';
    canvas.on(endEvent, function(e) {
        sketch = false;
    });

    function init() {
        var width = canvas.parent().width();
        var height = width / aspectRatio;

        ctx.lineJoin = 'round';
        ctx.lineCap = 'round';

        canvas.css('background-color', backgroundColor);

        setSize(width, height);
    }

    function getMouseLocation(element, event) {
        if (event.type.indexOf('touch') !== -1) {
            return {
                x: event.originalEvent.touches[0].pageX - element.offsetLeft,
                y: event.originalEvent.touches[0].pageY - element.offsetTop
            }
        } else {
            return {
                x: event.pageX - element.offsetLeft,
                y: event.pageY - element.offsetTop
            };
        }
    }

    function setSize(w, h) {
        width = w;
        height = h;

        canvas.width(width);
        canvas.height(height);

        canvas[0].setAttribute('width', width);
        canvas[0].setAttribute('height', height);
    }

    function redraw() {
        ctx.beginPath();
        for (var i = 1; i < drawing.length; i++) {

            if (drawing[i].type == 'mousemove' || drawing[i].type == 'touchmove') {
                ctx.moveTo(drawing[i - 1].x, drawing[i - 1].y);
            } else {
                ctx.moveTo(drawing[i].x, drawing[i].y);
            }

            ctx.lineTo(drawing[i].x, drawing[i].y);
            ctx.strokeStyle = drawing[i].color;
            ctx.lineWidth = lineSize
            ctx.stroke()
        }
        ctx.closePath();
    }

    function toJson() {
        return JSON.stringify(drawing);
    }

    this.changeLineColor = function(color) {
        lineColor = color;
    };

    this.changeLineSize = function(size) {
        lineSize = size;
    };

    init();

    return this;
};<commit_msg>Store locations as percentage of canvas for easier scaling<commit_after>$.fn.respondosketch = function(props) {

    var canvas = this;
    var ctx = $(this)[0].getContext('2d');

    var aspectRatio = props.aspectRatio;
    var sketch = false;
    var drawing = new Array();
    var width = canvas.width();
    var height = canvas.height();
    var lineColor = 'black';
    var lineSize = 5;

    // Setup Context
    ctx.lineJoin = 'round';
    ctx.lineCap = 'round';

    //
    $(window).resize(function(e) {
        var newWidth = canvas.parent().width();
        var newHeight = newWidth / aspectRatio;

        setSize(newWidth, newHeight);

        redraw();
    });

    var startEvent = 'mousedown touchstart ';
    canvas.on(startEvent, function(e) {
        if (e.type == 'touchstart') {
            e.preventDefault();
        } else {
            e.originalEvent.preventDefault();
        }

        sketch = true;

        var canvas = getCanvasLocation(this, e);

        drawing.push({
            x: canvas.x,
            y: canvas.y,
            type: e.type,
            color: lineColor,
            size: lineSize
        });

        redraw();
    });

    var moveEvent = 'mousemove touchmove ';
    canvas.on(moveEvent, function(e) {
        var canvas = getCanvasLocation(this, e);

        if (sketch) {
            drawing.push({
                x: canvas.x,
                y: canvas.y,
                type: e.type,
                color: lineColor,
                size: lineSize
            });

            redraw();
        }
    });

    var endEvent = 'mouseup mouseleave touchend ';
    canvas.on(endEvent, function(e) {
        sketch = false;
    });

    function init() {

        var color = props.backgroundColor;
        var locked = props.locked;

        if (props.json) {
            drawing = props.json.lines;
            aspectRatio = props.json.aspectRatio;
        }

        if (props.locked) {
            canvas.unbind(startEvent + moveEvent + endEvent);
        } else {
            canvas.css('cursor', 'crosshair');
        }

        var width = canvas.parent().width();
        var height = width / aspectRatio;

        canvas.css('background-color', color);

        setSize(width, height);

        redraw();

    }

    function getCanvasLocation(element, event) {
        if (event.type.indexOf('touch') !== -1) {
            return {
                x: (event.originalEvent.touches[0].pageX - element.offsetLeft) / width,
                y: (event.originalEvent.touches[0].pageY - element.offsetTop) / height
            }
        } else {
            return {
                x: (event.pageX - element.offsetLeft) / width,
                y: (event.pageY - element.offsetTop) / height
            };
        }
    }

    function setSize(w, h) {
        width = w;
        height = h;

        canvas.width(width);
        canvas.height(height);

        canvas[0].setAttribute('width', width);
        canvas[0].setAttribute('height', height);
    }

    function redraw() {
        ctx.beginPath();
        for (var i = 1; i < drawing.length; i++) {

            if (drawing[i].type == 'mousemove' || drawing[i].type == 'touchmove') {
                ctx.moveTo(drawing[i - 1].x * width, drawing[i - 1].y * height);
            } else {
                ctx.moveTo(drawing[i].x * width, drawing[i].y * height);
            }

            ctx.lineTo(drawing[i].x * width, drawing[i].y * height);
            ctx.strokeStyle = drawing[i].color;
            ctx.lineWidth = lineSize
            ctx.stroke()
        }
        ctx.closePath();
    }

    this.json = function() {
        return JSON.stringify({
            aspectRatio: aspectRatio,
            lines: drawing
        });
    };

    this.img = function() {
        var img = canvas[0].toDataURL("image/png");
        return img;
    };

    this.changeLineColor = function(color) {
        lineColor = color;
    };

    this.changeLineSize = function(size) {
        lineSize = size;
    };

    init();

    return this;
};<|endoftext|>


<commit_before>'use strict';

const { RangeError } = require('../errors');

/**
 * Data structure that makes it easy to interact with a bitfield.
 */
class BitField {
  /**
   * @param {BitFieldResolvable} [bits=0] Bit(s) to read from
   */
  constructor(bits) {
    /**
     * Bitfield of the packed bits
     * @type {number}
     */
    this.bitfield = this.constructor.resolve(bits);
  }

  /**
   * Checks whether the bitfield has a bit, or any of multiple bits.
   * @param {BitFieldResolvable} bit Bit(s) to check for
   * @returns {boolean}
   */
  any(bit) {
    return (this.bitfield & this.constructor.resolve(bit)) !== 0;
  }

  /**
   * Checks if this bitfield equals another
   * @param {BitFieldResolvable} bit Bit(s) to check for
   * @returns {boolean}
   */
  equals(bit) {
    return this.bitfield === this.constructor.resolve(bit);
  }

  /**
   * Checks whether the bitfield has a bit, or multiple bits.
   * @param {BitFieldResolvable} bit Bit(s) to check for
   * @returns {boolean}
   */
  has(bit) {
    if (Array.isArray(bit)) return bit.every(p => this.has(p));
    bit = this.constructor.resolve(bit);
    return (this.bitfield & bit) === bit;
  }

  /**
   * Gets all given bits that are missing from the bitfield.
   * @param {BitFieldResolvable} bits Bit(s) to check for
   * @param {...*} hasParams Additional parameters for the has method, if any
   * @returns {string[]}
   */
  missing(bits, ...hasParams) {
    if (!Array.isArray(bits)) bits = new this.constructor(bits).toArray(false);
    return bits.filter(p => !this.has(p, ...hasParams));
  }

  /**
   * Freezes these bits, making them immutable.
   * @returns {Readonly<BitField>} These bits
   */
  freeze() {
    return Object.freeze(this);
  }

  /**
   * Adds bits to these ones.
   * @param {...BitFieldResolvable} [bits] Bits to add
   * @returns {BitField} These bits or new BitField if the instance is frozen.
   */
  add(...bits) {
    let total = 0;
    for (const bit of bits) {
      total |= this.constructor.resolve(bit);
    }
    if (Object.isFrozen(this)) return new this.constructor(this.bitfield | total);
    this.bitfield |= total;
    return this;
  }

  /**
   * Removes bits from these.
   * @param {...BitFieldResolvable} [bits] Bits to remove
   * @returns {BitField} These bits or new BitField if the instance is frozen.
   */
  remove(...bits) {
    let total = 0;
    for (const bit of bits) {
      total |= this.constructor.resolve(bit);
    }
    if (Object.isFrozen(this)) return new this.constructor(this.bitfield & ~total);
    this.bitfield &= ~total;
    return this;
  }

  /**
   * Gets an object mapping field names to a {@link boolean} indicating whether the
   * bit is available.
   * @param {...*} hasParams Additional parameters for the has method, if any
   * @returns {Object}
   */
  serialize(...hasParams) {
    const serialized = {};
    for (const [flag, bit] of Object.entries(this.constructor.FLAGS)) serialized[flag] = this.has(bit, ...hasParams);
    return serialized;
  }

  /**
   * Gets an {@link Array} of bitfield names based on the bits available.
   * @param {...*} hasParams Additional parameters for the has method, if any
   * @returns {string[]}
   */
  toArray(...hasParams) {
    return Object.keys(this.constructor.FLAGS).filter(bit => this.has(bit, ...hasParams));
  }

  toJSON() {
    return this.bitfield;
  }

  valueOf() {
    return this.bitfield;
  }

  *[Symbol.iterator]() {
    yield* this.toArray();
  }

  /**
   * Data that can be resolved to give a bitfield. This can be:
   * * A string (see {@link BitField.FLAGS})
   * * A bit number
   * * An instance of BitField
   * * An Array of BitFieldResolvable
   * @typedef {string|number|BitField|BitFieldResolvable[]} BitFieldResolvable
   */

  /**
   * Resolves bitfields to their numeric form.
   * @param {BitFieldResolvable} [bit=0] - bit(s) to resolve
   * @returns {number}
   */
  static resolve(bit = 0) {
    if (typeof bit === 'number' && bit >= 0) return bit;
    if (bit instanceof BitField) return bit.bitfield;
    if (Array.isArray(bit)) return bit.map(p => this.resolve(p)).reduce((prev, p) => prev | p, 0);
    if (typeof bit === 'string' && typeof this.FLAGS[bit] !== 'undefined') return this.FLAGS[bit];
    throw new RangeError('BITFIELD_INVALID');
  }
}

/**
 * Numeric bitfield flags.
 * <info>Defined in extension classes</info>
 * @type {Object}
 * @abstract
 */
BitField.FLAGS = {};

module.exports = BitField;
<commit_msg>feat(BitField): add problematic bit to error (#4617)<commit_after>'use strict';

const { RangeError } = require('../errors');

/**
 * Data structure that makes it easy to interact with a bitfield.
 */
class BitField {
  /**
   * @param {BitFieldResolvable} [bits=0] Bit(s) to read from
   */
  constructor(bits) {
    /**
     * Bitfield of the packed bits
     * @type {number}
     */
    this.bitfield = this.constructor.resolve(bits);
  }

  /**
   * Checks whether the bitfield has a bit, or any of multiple bits.
   * @param {BitFieldResolvable} bit Bit(s) to check for
   * @returns {boolean}
   */
  any(bit) {
    return (this.bitfield & this.constructor.resolve(bit)) !== 0;
  }

  /**
   * Checks if this bitfield equals another
   * @param {BitFieldResolvable} bit Bit(s) to check for
   * @returns {boolean}
   */
  equals(bit) {
    return this.bitfield === this.constructor.resolve(bit);
  }

  /**
   * Checks whether the bitfield has a bit, or multiple bits.
   * @param {BitFieldResolvable} bit Bit(s) to check for
   * @returns {boolean}
   */
  has(bit) {
    if (Array.isArray(bit)) return bit.every(p => this.has(p));
    bit = this.constructor.resolve(bit);
    return (this.bitfield & bit) === bit;
  }

  /**
   * Gets all given bits that are missing from the bitfield.
   * @param {BitFieldResolvable} bits Bit(s) to check for
   * @param {...*} hasParams Additional parameters for the has method, if any
   * @returns {string[]}
   */
  missing(bits, ...hasParams) {
    if (!Array.isArray(bits)) bits = new this.constructor(bits).toArray(false);
    return bits.filter(p => !this.has(p, ...hasParams));
  }

  /**
   * Freezes these bits, making them immutable.
   * @returns {Readonly<BitField>} These bits
   */
  freeze() {
    return Object.freeze(this);
  }

  /**
   * Adds bits to these ones.
   * @param {...BitFieldResolvable} [bits] Bits to add
   * @returns {BitField} These bits or new BitField if the instance is frozen.
   */
  add(...bits) {
    let total = 0;
    for (const bit of bits) {
      total |= this.constructor.resolve(bit);
    }
    if (Object.isFrozen(this)) return new this.constructor(this.bitfield | total);
    this.bitfield |= total;
    return this;
  }

  /**
   * Removes bits from these.
   * @param {...BitFieldResolvable} [bits] Bits to remove
   * @returns {BitField} These bits or new BitField if the instance is frozen.
   */
  remove(...bits) {
    let total = 0;
    for (const bit of bits) {
      total |= this.constructor.resolve(bit);
    }
    if (Object.isFrozen(this)) return new this.constructor(this.bitfield & ~total);
    this.bitfield &= ~total;
    return this;
  }

  /**
   * Gets an object mapping field names to a {@link boolean} indicating whether the
   * bit is available.
   * @param {...*} hasParams Additional parameters for the has method, if any
   * @returns {Object}
   */
  serialize(...hasParams) {
    const serialized = {};
    for (const [flag, bit] of Object.entries(this.constructor.FLAGS)) serialized[flag] = this.has(bit, ...hasParams);
    return serialized;
  }

  /**
   * Gets an {@link Array} of bitfield names based on the bits available.
   * @param {...*} hasParams Additional parameters for the has method, if any
   * @returns {string[]}
   */
  toArray(...hasParams) {
    return Object.keys(this.constructor.FLAGS).filter(bit => this.has(bit, ...hasParams));
  }

  toJSON() {
    return this.bitfield;
  }

  valueOf() {
    return this.bitfield;
  }

  *[Symbol.iterator]() {
    yield* this.toArray();
  }

  /**
   * Data that can be resolved to give a bitfield. This can be:
   * * A string (see {@link BitField.FLAGS})
   * * A bit number
   * * An instance of BitField
   * * An Array of BitFieldResolvable
   * @typedef {string|number|BitField|BitFieldResolvable[]} BitFieldResolvable
   */

  /**
   * Resolves bitfields to their numeric form.
   * @param {BitFieldResolvable} [bit=0] - bit(s) to resolve
   * @returns {number}
   */
  static resolve(bit = 0) {
    if (typeof bit === 'number' && bit >= 0) return bit;
    if (bit instanceof BitField) return bit.bitfield;
    if (Array.isArray(bit)) return bit.map(p => this.resolve(p)).reduce((prev, p) => prev | p, 0);
    if (typeof bit === 'string' && typeof this.FLAGS[bit] !== 'undefined') return this.FLAGS[bit];
    const error = new RangeError('BITFIELD_INVALID');
    error.bit = bit;
    throw error;
  }
}

/**
 * Numeric bitfield flags.
 * <info>Defined in extension classes</info>
 * @type {Object}
 * @abstract
 */
BitField.FLAGS = {};

module.exports = BitField;
<|endoftext|>