<commit_before>'use strict';

module.exports = function (grunt) {
    var esprima = require('esprima');
    var getComment = require('./comments').getComment;

    function extractStrings(fileName) {
        function walkTree(node, fn) {
            fn(node);

            for (var key in node) {
                var obj = node[key];
                if (typeof obj === 'object') {
                    walkTree(obj, fn);
                }
            }
        }

        function tryEval(code) {
            var val;
            try {
                val = eval(code);
            } catch (e) {
                grunt.verbose.warn(e + ' - while statically evaluating code: ' + code);
            }
            return val;
        }

        var code = grunt.file.read(fileName);
        var syntax = esprima.parse(code, {comment: true, loc: true, tokens: true, range: true});
        var items;
        var handleGtCall = function (node, gtName, module) {
            if (node !== null &&
                node.type === 'CallExpression' &&
                node.callee !== null &&
                node.callee.name === gtName &&
                node['arguments'] !== null &&
                node['arguments'].length
            ) {
                var msgId = node['arguments'][0].value || tryEval(code.slice(node['arguments'][0].range[0], node['arguments'][0].range[1]));
                if (!msgId) {
                    if (getComment(syntax, node).indexOf('#, dynamic') >= 0) {
                        grunt.verbose.warn('Ignoring gt.pgettext call with dynamic flag set.');
                        return;
                    }
                    grunt.verbose.warn(JSON.stringify(node['arguments'], null, 4));
                    grunt.log.error('Override this by adding a "dynamic" flag to the translator comments');
                    grunt.fail.warn('Could not read node in file: ' + fileName + ' use --verbose for more info');
                    return;
                }
                items = items || [];
                items.push({
                    msgid: msgId.trim(),
                    module: module,
                    fileName: fileName,
                    line: node.loc.start.line,
                    comment: getComment(syntax, node)
                });
            } else if (
                node !== null &&
                node.type === 'CallExpression' &&
                node.callee !== null &&
                node.callee.type === 'MemberExpression' &&
                node.callee.object.name === gtName &&
                node.callee.property.name === 'pgettext' &&
                node['arguments'] !== null &&
                node['arguments'].length === 2
            ) {
                var msgId = node['arguments'][1].value || tryEval(code.slice(node['arguments'][1].range[0], node['arguments'][1].range[1]));
                var msgContext = node['arguments'][0].value || tryEval(code.slice(node['arguments'][0].range[0], node['arguments'][0].range[1]));
                if (!msgId || !msgContext) {
                    if (getComment(syntax, node).indexOf('#, dynamic') >= 0) {
                        grunt.verbose.warn('Ignoring gt.pgettext call with dynamic flag set.');
                        return;
                    }
                    grunt.verbose.warn(JSON.stringify(node['arguments'], null, 4));
                    grunt.log.error('Override this by adding a "dynamic" flag to the translator comments');
                    grunt.fail.warn('Could not read node in file: ' + fileName + ' use --verbose for more info');
                    return;
                }
                items = items || [];
                items.push({
                    msgid: msgId.trim(),
                    msgctxt: msgContext.trim(),
                    module: module,
                    fileName: fileName,
                    line: node.loc.start.line,
                    comment: getComment(syntax, node)
                });
            } else if (
                node !== null &&
                node.type === 'CallExpression' &&
                node.callee !== null &&
                node.callee.type === 'MemberExpression' &&
                node.callee.object.name === gtName &&
                node.callee.property.name === 'ngettext' &&
                node['arguments'] !== null &&
                node['arguments'].length === 3
            ) {
                var msgId = node['arguments'][0].value || tryEval(code.slice(node['arguments'][0].range[0], node['arguments'][0].range[1]));
                var msgIdPlural = node['arguments'][1].value || tryEval(code.slice(node['arguments'][1].range[0], node['arguments'][1].range[1]));
                if (!msgId || !msgIdPlural || !node['arguments'][2]) {
                    if (getComment(syntax, node).indexOf('#, dynamic') >= 0) {
                        grunt.verbose.warn('Ignoring gt.pgettext call with dynamic flag set.');
                        return;
                    }
                    grunt.verbose.warn(JSON.stringify(node['arguments'], null, 4));
                    grunt.log.error('Override this by adding a "dynamic" flag to the translator comments');
                    grunt.fail.warn('Could not read node  in file: ' + fileName + ' use --verbose for more info');
                    return;
                }
                items = items || [];
                items.push({
                    msgid: msgId.trim(),
                    msgid_plural: msgIdPlural.trim(),
                    module: module,
                    fileName: fileName,
                    line: node.loc.start.line,
                    comment: getComment(syntax, node)
                });
            } else if (
                node !== null &&
                node.type === 'CallExpression' &&
                node.callee !== null &&
                node.callee.type === 'MemberExpression' &&
                node.callee.object.name === gtName &&
                node.callee.property.name === 'npgettext' &&
                node['arguments'] !== null &&
                node['arguments'].length === 4
            ) {
                var msgContext = node['arguments'][0].value || tryEval(code.slice(node['arguments'][0].range[0], node['arguments'][0].range[1]));
                var msgId = node['arguments'][1].value || tryEval(code.slice(node['arguments'][1].range[0], node['arguments'][1].range[1]));
                var msgIdPlural = node['arguments'][2].value || tryEval(code.slice(node['arguments'][2].range[0], node['arguments'][2].range[1]));
                if (!msgContext || !msgId || !msgIdPlural || !node['arguments'][3]) {
                    if (getComment(syntax, node).indexOf('#, dynamic') >= 0) {
                        grunt.verbose.warn('Ignoring gt.pgettext call with dynamic flag set.');
                        return;
                    }
                    grunt.verbose.warn(JSON.stringify(node['arguments'], null, 4));
                    grunt.log.error('Override this by adding a "dynamic" flag to the translator comments');
                    grunt.fail.warn('Could not read node in file: ' + fileName + ' use --verbose for more info');
                    return;
                }
                items = items || [];
                items.push({
                    msgctxt: msgContext.trim(),
                    msgid: msgId.trim(),
                    msgid_plural: msgIdPlural.trim(),
                    module: module,
                    fileName: fileName,
                    line: node.loc.start.line,
                    comment: getComment(syntax, node)
                });
            }
        };

        walkTree(syntax, function (node) {
            if (node !== null &&
                node.type === 'CallExpression' &&
                node.callee !== null &&
                (node.callee.name === 'define' ||
                (node.callee.type === 'MemberExpression' &&
                node.callee.object !== null &&
                node.callee.object.name === 'define' &&
                node.callee.property.name === 'async')) &&
                node['arguments'] !== null &&
                node['arguments'].length > 2)
            {
                node['arguments'][1].elements.map(function (el, index) {
                    if (el.value.substr(0, 8) === 'gettext!') {
                        var param = node['arguments'][2].params[index];
                        if (!param) {
                            grunt.log.warn('Unused require-gettext module call in', fileName);
                            return;
                        }
                        return {
                            name: param.name,
                            module: el.value.substr(8)
                        };
                    }
                }).filter(function (arg) {
                    return arg !== undefined;
                }).forEach(function (obj) {
                    var gtName = obj.name;
                    var module = obj.module;

                    if (node['arguments'][2].type === 'FunctionExpression') {
                        walkTree(node['arguments'][2], function (node) {
                            handleGtCall(node, gtName, module);
                        });
                    }
                });
            }
        });
        return items;
    }

    function mkKey(poItem) {
        var key = poItem.msgctxt ? poItem.msgctxt + '\x00' + poItem.msgid : poItem.msgid;
        if (poItem.msgid_plural) {
            key += '\x01' + poItem.msgid_plural;
        }
        return key;
    }

    function fromFiles(files) {
        var PO = require('pofile');
        var _ = require('lodash');
        return files.map(function (srcFile) {
            return {srcFile: srcFile, items: extractStrings(srcFile)};
        }).reduce(function (acc, result) {
            if (!acc) {
                return;
            }
            var items = result.items;
            if (!items) {
                items = [];
                grunt.log.debug('No strings extracted from file ' + result.srcFile);
            }
            items.forEach(function (item) {
                var key = mkKey(item);
                if (!acc[key]) {
                    acc[key] = new PO.Item();
                }
                var poItem = acc[key];
                poItem.msgid = item.msgid;
                poItem.msgid_plural = item.msgid_plural;
                poItem.msgctxt = item.msgctxt;
                if (poItem.references.indexOf(item.fileName + ':' + item.line) < 0) {
                    poItem.references.push(item.fileName + ':' + item.line);
                }
                if (poItem.references.indexOf('module:' + item.module) < 0) {
                    poItem.references.push('module:' + item.module);
                }
                if (poItem.msgid_plural) {
                    //FIXME: may be, this should be handled by pofile library
                    poItem.msgstr = ['', ''];
                }
                if (item.comment) {
                    var commentItem = PO.parse('msgid ""\nmsgstr ""\n\n' + item.comment + '\nmsgid "temp"\nmsgstr""\n').items[0];
                    _(poItem.flags).extend(commentItem.flags);
                    poItem.extractedComments = _.uniq([].concat(poItem.extractedComments, commentItem.extractedComments));
                }
            });
            return acc;
        }, {});
    }

    return {
        extractStrings: extractStrings,
        fromFiles: fromFiles,
        mkKey: mkKey
    };
};
<commit_msg>be less verbose in case of warnings<commit_after>'use strict';

module.exports = function (grunt) {
    var esprima = require('esprima');
    var getComment = require('./comments').getComment;

    function extractStrings(fileName) {
        function walkTree(node, fn) {
            fn(node);

            for (var key in node) {
                var obj = node[key];
                if (typeof obj === 'object') {
                    walkTree(obj, fn);
                }
            }
        }

        function tryEval(code) {
            var val;
            try {
                val = eval(code);
            } catch (e) {
                grunt.verbose.warn(e + ' - while statically evaluating code: ' + code);
            }
            return val;
        }

        var code = grunt.file.read(fileName);
        var syntax = esprima.parse(code, {comment: true, loc: true, tokens: true, range: true});
        var items;
        var handleGtCall = function (node, gtName, module) {
            if (node !== null &&
                node.type === 'CallExpression' &&
                node.callee !== null &&
                node.callee.name === gtName &&
                node['arguments'] !== null &&
                node['arguments'].length
            ) {
                var msgId = node['arguments'][0].value || tryEval(code.slice(node['arguments'][0].range[0], node['arguments'][0].range[1]));
                if (!msgId) {
                    if (getComment(syntax, node).indexOf('#, dynamic') >= 0) {
                        grunt.verbose.warn('Ignoring gt.pgettext call with dynamic flag set.');
                        return;
                    }
                    grunt.verbose.warn(JSON.stringify(node['arguments'], null, 4));
                    grunt.verbose.error('Override this by adding a "dynamic" flag to the translator comments');
                    grunt.fail.warn('Could not read node in file: ' + fileName + ' use --verbose for more info');
                    return;
                }
                items = items || [];
                items.push({
                    msgid: msgId.trim(),
                    module: module,
                    fileName: fileName,
                    line: node.loc.start.line,
                    comment: getComment(syntax, node)
                });
            } else if (
                node !== null &&
                node.type === 'CallExpression' &&
                node.callee !== null &&
                node.callee.type === 'MemberExpression' &&
                node.callee.object.name === gtName &&
                node.callee.property.name === 'pgettext' &&
                node['arguments'] !== null &&
                node['arguments'].length === 2
            ) {
                var msgId = node['arguments'][1].value || tryEval(code.slice(node['arguments'][1].range[0], node['arguments'][1].range[1]));
                var msgContext = node['arguments'][0].value || tryEval(code.slice(node['arguments'][0].range[0], node['arguments'][0].range[1]));
                if (!msgId || !msgContext) {
                    if (getComment(syntax, node).indexOf('#, dynamic') >= 0) {
                        grunt.verbose.warn('Ignoring gt.pgettext call with dynamic flag set.');
                        return;
                    }
                    grunt.verbose.warn(JSON.stringify(node['arguments'], null, 4));
                    grunt.log.error('Override this by adding a "dynamic" flag to the translator comments');
                    grunt.fail.warn('Could not read node in file: ' + fileName + ' use --verbose for more info');
                    return;
                }
                items = items || [];
                items.push({
                    msgid: msgId.trim(),
                    msgctxt: msgContext.trim(),
                    module: module,
                    fileName: fileName,
                    line: node.loc.start.line,
                    comment: getComment(syntax, node)
                });
            } else if (
                node !== null &&
                node.type === 'CallExpression' &&
                node.callee !== null &&
                node.callee.type === 'MemberExpression' &&
                node.callee.object.name === gtName &&
                node.callee.property.name === 'ngettext' &&
                node['arguments'] !== null &&
                node['arguments'].length === 3
            ) {
                var msgId = node['arguments'][0].value || tryEval(code.slice(node['arguments'][0].range[0], node['arguments'][0].range[1]));
                var msgIdPlural = node['arguments'][1].value || tryEval(code.slice(node['arguments'][1].range[0], node['arguments'][1].range[1]));
                if (!msgId || !msgIdPlural || !node['arguments'][2]) {
                    if (getComment(syntax, node).indexOf('#, dynamic') >= 0) {
                        grunt.verbose.warn('Ignoring gt.pgettext call with dynamic flag set.');
                        return;
                    }
                    grunt.verbose.warn(JSON.stringify(node['arguments'], null, 4));
                    grunt.log.error('Override this by adding a "dynamic" flag to the translator comments');
                    grunt.fail.warn('Could not read node  in file: ' + fileName + ' use --verbose for more info');
                    return;
                }
                items = items || [];
                items.push({
                    msgid: msgId.trim(),
                    msgid_plural: msgIdPlural.trim(),
                    module: module,
                    fileName: fileName,
                    line: node.loc.start.line,
                    comment: getComment(syntax, node)
                });
            } else if (
                node !== null &&
                node.type === 'CallExpression' &&
                node.callee !== null &&
                node.callee.type === 'MemberExpression' &&
                node.callee.object.name === gtName &&
                node.callee.property.name === 'npgettext' &&
                node['arguments'] !== null &&
                node['arguments'].length === 4
            ) {
                var msgContext = node['arguments'][0].value || tryEval(code.slice(node['arguments'][0].range[0], node['arguments'][0].range[1]));
                var msgId = node['arguments'][1].value || tryEval(code.slice(node['arguments'][1].range[0], node['arguments'][1].range[1]));
                var msgIdPlural = node['arguments'][2].value || tryEval(code.slice(node['arguments'][2].range[0], node['arguments'][2].range[1]));
                if (!msgContext || !msgId || !msgIdPlural || !node['arguments'][3]) {
                    if (getComment(syntax, node).indexOf('#, dynamic') >= 0) {
                        grunt.verbose.warn('Ignoring gt.pgettext call with dynamic flag set.');
                        return;
                    }
                    grunt.verbose.warn(JSON.stringify(node['arguments'], null, 4));
                    grunt.log.error('Override this by adding a "dynamic" flag to the translator comments');
                    grunt.fail.warn('Could not read node in file: ' + fileName + ' use --verbose for more info');
                    return;
                }
                items = items || [];
                items.push({
                    msgctxt: msgContext.trim(),
                    msgid: msgId.trim(),
                    msgid_plural: msgIdPlural.trim(),
                    module: module,
                    fileName: fileName,
                    line: node.loc.start.line,
                    comment: getComment(syntax, node)
                });
            }
        };

        walkTree(syntax, function (node) {
            if (node !== null &&
                node.type === 'CallExpression' &&
                node.callee !== null &&
                (node.callee.name === 'define' ||
                (node.callee.type === 'MemberExpression' &&
                node.callee.object !== null &&
                node.callee.object.name === 'define' &&
                node.callee.property.name === 'async')) &&
                node['arguments'] !== null &&
                node['arguments'].length > 2)
            {
                node['arguments'][1].elements.map(function (el, index) {
                    if (el.value.substr(0, 8) === 'gettext!') {
                        var param = node['arguments'][2].params[index];
                        if (!param) {
                            grunt.log.warn('Unused require-gettext module call in', fileName);
                            return;
                        }
                        return {
                            name: param.name,
                            module: el.value.substr(8)
                        };
                    }
                }).filter(function (arg) {
                    return arg !== undefined;
                }).forEach(function (obj) {
                    var gtName = obj.name;
                    var module = obj.module;

                    if (node['arguments'][2].type === 'FunctionExpression') {
                        walkTree(node['arguments'][2], function (node) {
                            handleGtCall(node, gtName, module);
                        });
                    }
                });
            }
        });
        return items;
    }

    function mkKey(poItem) {
        var key = poItem.msgctxt ? poItem.msgctxt + '\x00' + poItem.msgid : poItem.msgid;
        if (poItem.msgid_plural) {
            key += '\x01' + poItem.msgid_plural;
        }
        return key;
    }

    function fromFiles(files) {
        var PO = require('pofile');
        var _ = require('lodash');
        return files.map(function (srcFile) {
            return {srcFile: srcFile, items: extractStrings(srcFile)};
        }).reduce(function (acc, result) {
            if (!acc) {
                return;
            }
            var items = result.items;
            if (!items) {
                items = [];
                grunt.log.debug('No strings extracted from file ' + result.srcFile);
            }
            items.forEach(function (item) {
                var key = mkKey(item);
                if (!acc[key]) {
                    acc[key] = new PO.Item();
                }
                var poItem = acc[key];
                poItem.msgid = item.msgid;
                poItem.msgid_plural = item.msgid_plural;
                poItem.msgctxt = item.msgctxt;
                if (poItem.references.indexOf(item.fileName + ':' + item.line) < 0) {
                    poItem.references.push(item.fileName + ':' + item.line);
                }
                if (poItem.references.indexOf('module:' + item.module) < 0) {
                    poItem.references.push('module:' + item.module);
                }
                if (poItem.msgid_plural) {
                    //FIXME: may be, this should be handled by pofile library
                    poItem.msgstr = ['', ''];
                }
                if (item.comment) {
                    var commentItem = PO.parse('msgid ""\nmsgstr ""\n\n' + item.comment + '\nmsgid "temp"\nmsgstr""\n').items[0];
                    _(poItem.flags).extend(commentItem.flags);
                    poItem.extractedComments = _.uniq([].concat(poItem.extractedComments, commentItem.extractedComments));
                }
            });
            return acc;
        }, {});
    }

    return {
        extractStrings: extractStrings,
        fromFiles: fromFiles,
        mkKey: mkKey
    };
};
<|endoftext|>


<commit_before>var fs = require( 'fs' )
  , path = require( 'path' )
  , extractorPath = path.join( __dirname, "extractors" )
  , typeExtractors = {}
  , regexExtractors = []
  , WHITELIST_PRESERVE_LINEBREAKS = /[^A-Za-z\x80-\xFF 0-9 \u2018\u2019\u2026 \u4E00-\u9FFF \.,\?""!@#\$%\^&\*\(\)-_=\+;:<>\/\\\|\}\{\[\]`~'-\w\n\r]*/g
  , WHITELIST_STRIP_LINEBREAKS = /[^A-Za-z\x80-\xFF 0-9 \u2018\u2019\u2026 \u4E00-\u9FFF \.,\?""!@#\$%\^&\*\(\)-_=\+;:<>\/\\\|\}\{\[\]`~'-\w]*/g
  , SINGLE_QUOTES = /[\u2018|\u2019]/g
  , DOUBLE_QUOTES = /[\u201C|\u201D]/g
  , MULTIPLE_SPACES = /[ \t\v\u00A0]{2,}/g // multiple spaces, tabs, vertical tabs, non-breaking space
  , extractors = []
  , totalExtractors = 0
  , satisfiedExtractors = 0;

var registerExtractor = function( extractor ) {
  extractor.types.forEach( function( type ) {
    if ( typeof type === "string" ) {
      typeExtractors[type] = extractor.extract;
    } else {
      if ( type instanceof RegExp ) {
        regexExtractors.push( { reg: type, extractor: extractor.extract} );
      }
    }
  });
};

var testExtractor = function( extractor ) {
  extractor.test( function( passedTest ) {
    satisfiedExtractors++;
    if ( passedTest ) {
      registerExtractor( extractor );
    }
  });
};

// global, all file type, content cleansing
var cleanseText = function( options, cb ) {
  return function( error, text ) {
    if ( !error ) {
      // clean up text
      if ( options.preserveLineBreaks ) {
        text = text.replace( WHITELIST_PRESERVE_LINEBREAKS, ' ' );
      } else {
        text = text.replace( WHITELIST_STRIP_LINEBREAKS, ' ' );
      }

      text = text.replace( / (?! )/g, '' )
        .replace( MULTIPLE_SPACES, ' ' )  // replace 2 or more spaces with 1 space
        .replace( SINGLE_QUOTES, "'" ) // replace nasty quotes with simple ones
        .replace( DOUBLE_QUOTES, '"' ); // replace nasty quotes with simple ones

    }
    cb( error, text );
  };
};

var extract = function( type, filePath, options, cb ) {
  if ( totalExtractors === satisfiedExtractors ) {
    var theExtractor = null;
    if ( typeExtractors[type] ) {
      theExtractor = typeExtractors[type];
    } else {
      regexExtractors.forEach( function (extractor) {
        if ( type.match( extractor.reg ) ) {
          theExtractor = extractor.extractor;
        }
      });
    }

    if (theExtractor) {
      cb = cleanseText( options, cb );
      theExtractor( filePath, options, cb );
    } else {
      var error = new Error( "textract does not currently extract files of type [[ " + type + " ]]" );
      error.typeNotFound = true;
      cb( error , null );
    }
  } else {
    console.error( "textract not ready, retrying in .5 seconds" );
    setTimeout(function(){
      extract( type, filePath, options, cb );
    }, 500);
  }
};

extractors = fs.readdirSync( extractorPath ).filter( function( extractor ) {
  return extractor !== "temp";
});
totalExtractors = extractors.length;
extractors.map( function ( item ) {
  var fullExtractorPath = path.join( extractorPath, item );
  return require( fullExtractorPath );
}). forEach( function( extractor ) {
  if ( extractor.test ) {
    testExtractor( extractor );
  } else {
    satisfiedExtractors++;
    registerExtractor( extractor );
  }
});

module.exports = extract;
<commit_msg>adding ability to register for extensions, not just mime types<commit_after>var fs = require( 'fs' )
  , path = require( 'path' )
  , extractorPath = path.join( __dirname, "extractors" )
  , extensionExtractors = {}
  , typeExtractors = {}
  , regexExtractors = []
  , WHITELIST_PRESERVE_LINEBREAKS = /[^A-Za-z\x80-\xFF 0-9 \u2018\u2019\u2026 \u4E00-\u9FFF \.,\?""!@#\$%\^&\*\(\)-_=\+;:<>\/\\\|\}\{\[\]`~'-\w\n\r]*/g
  , WHITELIST_STRIP_LINEBREAKS = /[^A-Za-z\x80-\xFF 0-9 \u2018\u2019\u2026 \u4E00-\u9FFF \.,\?""!@#\$%\^&\*\(\)-_=\+;:<>\/\\\|\}\{\[\]`~'-\w]*/g
  , SINGLE_QUOTES = /[\u2018|\u2019]/g
  , DOUBLE_QUOTES = /[\u201C|\u201D]/g
  , MULTIPLE_SPACES = /[ \t\v\u00A0]{2,}/g // multiple spaces, tabs, vertical tabs, non-breaking space
  , extractors = []
  , totalExtractors = 0
  , satisfiedExtractors = 0;

var registerExtractor = function( extractor ) {
  if ( extractor.types ) {
    extractor.types.forEach( function( type ) {
      if ( typeof type === "string" ) {
        typeExtractors[type] = extractor.extract;
      } else {
        if ( type instanceof RegExp ) {
          regexExtractors.push( { reg: type, extractor: extractor.extract} );
        }
      }
    });
  }

  if ( extractor.extensions ) {
    extractor.extensions.forEach( function( ext ) {
      extensionExtractors[ext] = extractor.extract;
    });
  }
};

var testExtractor = function( extractor ) {
  extractor.test( function( passedTest ) {
    satisfiedExtractors++;
    if ( passedTest ) {
      registerExtractor( extractor );
    }
  });
};

// global, all file type, content cleansing
var cleanseText = function( options, cb ) {
  return function( error, text ) {
    if ( !error ) {
      // clean up text
      if ( options.preserveLineBreaks ) {
        text = text.replace( WHITELIST_PRESERVE_LINEBREAKS, ' ' );
      } else {
        text = text.replace( WHITELIST_STRIP_LINEBREAKS, ' ' );
      }

      text = text.replace( / (?! )/g, '' )
        .replace( MULTIPLE_SPACES, ' ' )  // replace 2 or more spaces with 1 space
        .replace( SINGLE_QUOTES, "'" ) // replace nasty quotes with simple ones
        .replace( DOUBLE_QUOTES, '"' ); // replace nasty quotes with simple ones

    }
    cb( error, text );
  };
};

var extract = function( type, filePath, options, cb ) {
  if ( totalExtractors === satisfiedExtractors ) {
    var theExtractor;
    var extension = path.extname( filePath ).substring( 1 );
    if ( extensionExtractors[extension] ) {
      theExtractor = extensionExtractors[extension];
    } else if ( typeExtractors[type] ) {
      theExtractor = typeExtractors[type];
    } else {
      regexExtractors.forEach( function (extractor) {
        if ( type.match( extractor.reg ) ) {
          theExtractor = extractor.extractor;
        }
      });
    }

    if (theExtractor) {
      cb = cleanseText( options, cb );
      theExtractor( filePath, options, cb );
    } else {
      var error = new Error( "textract does not currently extract files of type [[ " + type + " ]]" );
      error.typeNotFound = true;
      cb( error , null );
    }
  } else {
    console.error( "textract not ready, retrying in .5 seconds" );
    setTimeout(function(){
      extract( type, filePath, options, cb );
    }, 500);
  }
};

extractors = fs.readdirSync( extractorPath ).filter( function( extractor ) {
  return extractor !== "temp";
});
totalExtractors = extractors.length;
extractors.map( function ( item ) {
  var fullExtractorPath = path.join( extractorPath, item );
  return require( fullExtractorPath );
}). forEach( function( extractor ) {
  if ( extractor.test ) {
    testExtractor( extractor );
  } else {
    satisfiedExtractors++;
    registerExtractor( extractor );
  }
});

module.exports = extract;
<|endoftext|>


<commit_before>// Dependencies
var path = require('path'),
  helpers = require('./helperfunctions'),
  filterHelp = require('./filterhelpers'),
  response = require('./responseHelpers'),
  fs = require('fs'),
  gm = require('gm'),
  im = require('imagemagick'),
  uuid = require('node-uuid'),
  spawn = require('child_process').spawn,
  imagePath = '/tmp/';

module.exports = {
  // Route which filter should be applied based on the request filter param
  routeFilter: function(req, res) {
    filters[req.filter](req, res);
  }
};

var filters = {
  // Blur filter
  // Defaults to a radius of 0 and a sigma of 6
  blur : function(req, res) {
    var key = uuid.v4().split('-').pop(),
      rad = req.query.r || 0,
      sig = req.query.s || 6;

    gm(imagePath + req.key + '.jpg')
    .blur(rad, sig)
    .stream(function(err, stdout, stderr) {
      var writeStream = fs.createWriteStream(imagePath + key + '.jpg');

      stdout.pipe(writeStream);

      stdout.on('close', function() {
        writeStream.close();

        response.getRes(req, res, key);
      });
    });
  },

  // Charcoal image filter
  // Defaults to a factor of 3
  charcoal: function(req, res) {
    var key = uuid.v4().split('-').pop(),
      factor = req.query.f || 3;

    gm(imagePath + req.key + '.jpg')
    .charcoal(factor)
    .stream(function(err, stdout, stderr) {
      var writeStream = fs.createWriteStream(imagePath + key + '.jpg');

      stdout.pipe(writeStream);

      stdout.on('close', function() {
        writeStream.close();
      });
    });
  },

  // Channel image
  // Defaults to red channel
  channel: function(req, res) {
    var type = req.query.t || 'red',
      key = uuid.v4().split('-').pop();

    gm(imagePath + req.key + '.jpg')
    .channel(type)
    .stream(function(err, stdout, stderr) {
      var writeStream = fs.createWriteStream(imagePath + key + '.jpg');

      stdout.pipe(writeStream);

      stdout.on('close', function() {
        writeStream.close();
        response.getRes(req, res, key);
      });
    });
  },

  // Nashville filter
  // Tints with peach and blue, and increases the contrast of the image
  nashville: function(req, res) {
    var key = uuid.v4().split('-').pop(),
      source = imagePath + req.key + '.jpg',
      target = imagePath + key + '.jpg',
      args = [source, '-fill', '#f7daae', '-colorize', '20', '-fill','#222b6d', '-colorize', '20', '+contrast', '+contrast', target],
      convert = spawn('convert', args);

    convert.stdout.pipe(fs.createWriteStream(target));

    convert.on('close', function() {
      response.getRes(req, res, key);
    });
  },

  // Auroral filter
  // Increase hue, brightness, and saturation
  // Tint with red tone
  auroral: function(req, res) {
    var key = uuid.v4().split('-').pop(),
      source = imagePath + req.key + '.jpg',
      target = imagePath + key + '.jpg',
      args = [source, '-modulate', '150,80,80', '-fill', '#330000', '-colorize', '40', '+contrast', '+contrast', target],
      convert = spawn('convert', args);

    convert.stdout.pipe(fs.createWriteStream(target));

    convert.on('close', function() {
      response.getRes(req, res, key);
    });
  },

  // Enhance filter
  // Increase red and green channels
  enhance: function(req, res) {
    var key = uuid.v4().split('-').pop(),
      source = imagePath + req.key + '.jpg',
      target = imagePath + key + '.jpg',
      args = [source, '-channel', 'R', '-level', '33%', '-channel', 'G', '-level', '33%', target],
      convert = spawn('convert', args);

    convert.stdout.pipe(fs.createWriteStream(target));

    convert.on('close', function() {
      response.getRes(req, res, key);
    });
  },

  // Standard black and gray filter
  bw: function(req, res) {
    var key = uuid.v4().split('-').pop(),
      source = imagePath + req.key + '.jpg',
      target = imagePath + key + '.jpg',
      args = [source, '-colorspace', 'Gray', target],
      convert = spawn('convert', args);

    convert.stdout.pipe(fs.createWriteStream(target));

    convert.on('close', function() {
      response.getRes(req, res, key);
    });
  },

  // Standard sepia filter
  sepia: function(req, res) {
    var key = uuid.v4().split('-').pop(),
      source = imagePath + req.key + '.jpg',
      target = imagePath + key + '.jpg',
      args = [source, '-modulate', '115,0,100', '-colorize', '7,21,50', target],
      convert = spawn('convert', args);

    convert.stdout.pipe(fs.createWriteStream(target));

    convert.on('close', function() {
      response.getRes(req, res, key);
    });
  },

  // Lomo filter
  // Tint with indigo
  // Decrease brightness and saturation
  lomo: function(req, res) {
    var key = uuid.v4().split('-').pop(),
      source = imagePath + req.key + '.jpg',
      target = imagePath + key + '.jpg',
      args = [source, '-fill', '#222b6d', '-colorize', '30','-modulate', '90,80,100', '-compose', 'Over', target],
      convert = spawn('convert', args);

    convert.stdout.pipe(fs.createWriteStream(target));

    convert.on('close', function() {
      response.getRes(req, res, key);
    });
  },

  // Gothic filter
  // Tint with blue
  // Increase contrast
  gotham: function(req, res) {
    var key = uuid.v4().split('-').pop(),
      source = imagePath + req.key + '.jpg',
      target = imagePath + key + '.jpg',
      args = [source, '-modulate', '120,10,100', '-fill', '#222b6d', '-colorize', '20', '-gamma', '0.5', '+contrast', '+contrast', '-compose', 'Over',
      '-bordercolor', 'black', '-border', '7,7', target],
      convert = spawn('convert', args);

    convert.stdout.pipe(fs.createWriteStream(target));

    convert.on('close', function() {
      response.getRes(req, res, key);
    });
  },

  // Black and white gradient filter
  // Convert target image to grayscale
  // Overlay target image with a gradient image
  bw_grad: function(req, res)  {
    var key = uuid.v4().split('-').pop(),
      source = imagePath + req.key + '.jpg',
      target = imagePath + key + '.jpg',
      args = [source, '-colorspace', 'gray', target],
      convert = spawn('convert', args);

    convert.stdout.pipe(fs.createWriteStream(target));

    convert.on('close', function() {
      response.getRes(req, res, key);
    });

    //obtain the dimensions of the requested image
    im.identify(imagePath + '/temp_' + req.key + '.jpg', function(err, features) {
      if (err) {
        console.error('Image transformation failed with error:', err);
        throw err;
      }

      filterHelp.resizeBW(req, res, features.width, features.height, key, filterHelp.addBWGrad);
    });
  },

  // Vintage filter
  // Overlay target image with antique image
  vintage: function(req, res) {
    var key = uuid.v4().split('-').pop();

    //obtain the dimensions of the requested image
    im.identify(imagePath + req.key + '.jpg', function(err, features) {
      if (err) {
        console.error('Image transformation failed with error:', err);
        throw err;
      }

      filterHelp.resizeVintage(req, res, features.width, features.height, key, filterHelp.addHipsterOverlay);
    });
  }
};<commit_msg>refactor vintage<commit_after>// Dependencies
var path = require('path'),
  helpers = require('./helperfunctions'),
  filterHelp = require('./filterhelpers'),
  response = require('./responseHelpers'),
  fs = require('fs'),
  gm = require('gm'),
  im = require('imagemagick'),
  uuid = require('node-uuid'),
  spawn = require('child_process').spawn,
  imagePath = '/tmp/';

module.exports = {
  // Route which filter should be applied based on the request filter param
  routeFilter: function(req, res) {
    filters[req.filter](req, res);
  }
};

var filters = {
  // Blur filter
  // Defaults to a radius of 0 and a sigma of 6
  blur : function(req, res) {
    var key = uuid.v4().split('-').pop(),
      rad = req.query.r || 0,
      sig = req.query.s || 6;

    gm(imagePath + req.key + '.jpg')
    .blur(rad, sig)
    .stream(function(err, stdout, stderr) {
      var writeStream = fs.createWriteStream(imagePath + key + '.jpg');

      stdout.pipe(writeStream);

      stdout.on('close', function() {
        writeStream.close();

        response.getRes(req, res, key);
      });
    });
  },

  // Charcoal image filter
  // Defaults to a factor of 3
  charcoal: function(req, res) {
    var key = uuid.v4().split('-').pop(),
      factor = req.query.f || 3;

    gm(imagePath + req.key + '.jpg')
    .charcoal(factor)
    .stream(function(err, stdout, stderr) {
      var writeStream = fs.createWriteStream(imagePath + key + '.jpg');

      stdout.pipe(writeStream);

      stdout.on('close', function() {
        writeStream.close();
      });
    });
  },

  // Channel image
  // Defaults to red channel
  channel: function(req, res) {
    var type = req.query.t || 'red',
      key = uuid.v4().split('-').pop();

    gm(imagePath + req.key + '.jpg')
    .channel(type)
    .stream(function(err, stdout, stderr) {
      var writeStream = fs.createWriteStream(imagePath + key + '.jpg');

      stdout.pipe(writeStream);

      stdout.on('close', function() {
        writeStream.close();
        response.getRes(req, res, key);
      });
    });
  },

  // Nashville filter
  // Tints with peach and blue, and increases the contrast of the image
  nashville: function(req, res) {
    var key = uuid.v4().split('-').pop(),
      source = imagePath + req.key + '.jpg',
      target = imagePath + key + '.jpg',
      args = [source, '-fill', '#f7daae', '-colorize', '20', '-fill','#222b6d', '-colorize', '20', '+contrast', '+contrast', target],
      convert = spawn('convert', args);

    convert.stdout.pipe(fs.createWriteStream(target));

    convert.on('close', function() {
      response.getRes(req, res, key);
    });
  },

  // Auroral filter
  // Increase hue, brightness, and saturation
  // Tint with red tone
  auroral: function(req, res) {
    var key = uuid.v4().split('-').pop(),
      source = imagePath + req.key + '.jpg',
      target = imagePath + key + '.jpg',
      args = [source, '-modulate', '150,80,80', '-fill', '#330000', '-colorize', '40', '+contrast', '+contrast', target],
      convert = spawn('convert', args);

    convert.stdout.pipe(fs.createWriteStream(target));

    convert.on('close', function() {
      response.getRes(req, res, key);
    });
  },

  // Enhance filter
  // Increase red and green channels
  enhance: function(req, res) {
    var key = uuid.v4().split('-').pop(),
      source = imagePath + req.key + '.jpg',
      target = imagePath + key + '.jpg',
      args = [source, '-channel', 'R', '-level', '33%', '-channel', 'G', '-level', '33%', target],
      convert = spawn('convert', args);

    convert.stdout.pipe(fs.createWriteStream(target));

    convert.on('close', function() {
      response.getRes(req, res, key);
    });
  },

  // Standard black and gray filter
  bw: function(req, res) {
    var key = uuid.v4().split('-').pop(),
      source = imagePath + req.key + '.jpg',
      target = imagePath + key + '.jpg',
      args = [source, '-colorspace', 'Gray', target],
      convert = spawn('convert', args);

    convert.stdout.pipe(fs.createWriteStream(target));

    convert.on('close', function() {
      response.getRes(req, res, key);
    });
  },

  // Standard sepia filter
  sepia: function(req, res) {
    var key = uuid.v4().split('-').pop(),
      source = imagePath + req.key + '.jpg',
      target = imagePath + key + '.jpg',
      args = [source, '-modulate', '115,0,100', '-colorize', '7,21,50', target],
      convert = spawn('convert', args);

    convert.stdout.pipe(fs.createWriteStream(target));

    convert.on('close', function() {
      response.getRes(req, res, key);
    });
  },

  // Lomo filter
  // Tint with indigo
  // Decrease brightness and saturation
  lomo: function(req, res) {
    var key = uuid.v4().split('-').pop(),
      source = imagePath + req.key + '.jpg',
      target = imagePath + key + '.jpg',
      args = [source, '-fill', '#222b6d', '-colorize', '30','-modulate', '90,80,100', '-compose', 'Over', target],
      convert = spawn('convert', args);

    convert.stdout.pipe(fs.createWriteStream(target));

    convert.on('close', function() {
      response.getRes(req, res, key);
    });
  },

  // Gothic filter
  // Tint with blue
  // Increase contrast
  gotham: function(req, res) {
    var key = uuid.v4().split('-').pop(),
      source = imagePath + req.key + '.jpg',
      target = imagePath + key + '.jpg',
      args = [source, '-modulate', '120,10,100', '-fill', '#222b6d', '-colorize', '20', '-gamma', '0.5', '+contrast', '+contrast', '-compose', 'Over',
      '-bordercolor', 'black', '-border', '7,7', target],
      convert = spawn('convert', args);

    convert.stdout.pipe(fs.createWriteStream(target));

    convert.on('close', function() {
      response.getRes(req, res, key);
    });
  },

  // Black and white gradient filter
  // Convert target image to grayscale
  // Overlay target image with a gradient image
  bw_grad: function(req, res)  {
    var key = uuid.v4().split('-').pop(),
      source = imagePath + req.key + '.jpg',
      target = imagePath + key + '.jpg',
      args = [source, '-colorspace', 'gray', target],
      convert = spawn('convert', args);

    convert.stdout.pipe(fs.createWriteStream(target));

    convert.on('close', function() {
      response.getRes(req, res, key);
    });

    //obtain the dimensions of the requested image
    im.identify(imagePath + '/temp_' + req.key + '.jpg', function(err, features) {
      if (err) {
        console.error('Image transformation failed with error:', err);
        throw err;
      }

      filterHelp.resizeBW(req, res, features.width, features.height, key, filterHelp.addBWGrad);
    });
  },

  // Vintage filter
  // Overlay target image with antique image
  vintage: function(req, res) {
    var key = uuid.v4().split('-').pop(),
      source = imagePath + req.key + '.jpg',
      target = imagePath + key + '.jpg',
      args = [source],
      convert = spawn('identify', args);

    convert.stdout.pipe(fs.createWriteStream(target));

    convert.on('close', function() {
      filterHelp.resizeVintage(req, res, features.width, features.height, key);
    });

    // var key = uuid.v4().split('-').pop();

    // //obtain the dimensions of the requested image
    // im.identify(imagePath + req.key + '.jpg', function(err, features) {
    //   if (err) {
    //     console.error('Image transformation failed with error:', err);
    //     throw err;
    //   }

    //   filterHelp.resizeVintage(req, res, features.width, features.height, key, filterHelp.addHipsterOverlay);
    // });
  }
};<|endoftext|>


<commit_before>// Load in dependencies
var fs = require('fs');
var util = require('util');
var _ = require('underscore');
var async = require('async');
var Command = require('commander-completion').Command;
var Release = require('./release');

function Foundry() {
  // Set up program
  Command.apply(this, arguments);

  // Bind our commands
  this.bindCommands();
}
Foundry.Release = Release;
Foundry.getConfig = function (params, callback) {
  // In series
  var source;
  var content;
  async.series([
    function attemptToLoadFoundryRc (cb) {
      // Load in our `.foundryrc` (relative to cwd)
      // DEV: We use `readFile` instead of `stat` + `readFile` to prevent the file descriptor from changing
      //   Most likely overkill but it also simplifies the code
      fs.readFile('.foundryrc', 'utf8', function handleFoundryrc (err, _content) {
        // If there was no file, continue to `package.json` resolution
        if (err && err.code === 'ENOENT') {
          return cb();
        // Otherwise if there was any other error, callback with the error
        } else if (err) {
          return cb(err);
        // Otherwise, save the content and continue
        } else {
          source = '.foundryrc';
          content = _content;
          return cb();
        }
      });
    },
    function attemptToLoadJson (cb) {
      // If we already have content, continue
      if (content) {
        return cb();
      }

      // Load in our `package.json` (relative to cwd)
      fs.readFile('package.json', 'utf8', function handlePackageJson (err, _content) {
        // If there was an error, callback with it
        if (err) {
          return cb(err);
        // Otherwise, save our content and continue
        } else {
          source = 'package.json';
          content = _content;
          return cb();
        }
      });
    }
  ], function handleContent (err) {
    // If there was no file anywhere, provide a useful message
    if (err && err.code === 'ENOENT') {
      return callback(new Error('Failed to find a `.foundryrc` or `package.json` file. ' +
        'Please run `foundry` in the same working directory as these files. ' +
        'We enforce this to guarantee steps are given the proper context.'));
    // Otherwise, callback with any other error
    } else if (err) {
      return callback(err);
    }

    // Otherwise, attempt to parse the JSON
    var pkg;
    try {
      pkg = JSON.parse(content);
    } catch (err2) {
      return callback(err2);
    }

    // Extract foundry configuration from `package.json`
    var config = pkg;
    if (source === 'package.json') {
      config = pkg.foundry;
      if (!config) {
        return callback(new Error('No `foundry` field found in `package.json`'));
      }
    }

    // Callback with our config
    return callback(null, config);
  });
};
util.inherits(Foundry, Command);
_.extend(Foundry.prototype, {
  name: 'foundry',
  bindCommands: function () {
    // Set up commands
    var program = this;

    program
      .command('release <version>')
      .description('Update package metadata and publish to registries')
      .option('-n, --dry-run', 'Output steps but don\'t execute them')
      .option('--no-color', 'Removed color from output')
      .action(function callRelease (version, argv) {
        // Gather configuration for `foundry`
        Foundry.getConfig({}, function handleReleaseLibs (err, config) {
          // If there was an error, throw it
          if (err) {
            throw err;
          } else if (!config.releaseCommands) {
            throw new Error('No `foundry.releaseCommands` was not defined in our `package.json`. ' +
              'Please list out the commands you would like to use for releases');
          } else if (config.releaseCommands.length === 0) {
            throw new Error('`foundry.releaseCommands` was found but empty in our `package.json`. ' +
              'Please list out the commands you would like to use for releases');
          }

          // Otherwise, release our package
          var release = new Release(config.releaseCommands, argv);
          release.release(version, function handleError (err) {
            if (err) {
              throw err;
            }
          });
        });
      });

    program
      .command('resume')
      .description('Resume an incomplete release')
      // TODO: Make sure we can actually do this...
      .option('-n, --dry-run', 'Output steps but don\'t execute them')
      .option('--no-color', 'Removed color from output')
      .action(function callResume (version, argv) {
        // Gather configuration for `foundry`
        Foundry.getConfig({}, function handleReleaseLibs (err, config) {
          // If there was an error, throw it
          if (err) {
            throw err;
          } else if (!config.releaseCommands) {
            throw new Error('No `foundry.releaseCommands` was not defined in our `package.json`. ' +
              'Please list out the commands you would like to use for releases');
          } else if (config.releaseCommands.length === 0) {
            throw new Error('`foundry.releaseCommands` was found but empty in our `package.json`. ' +
              'Please list out the commands you would like to use for releases');
          }

          // Initialize our release library (catches any issues with `package.json`)
          var release = new Release(config.releaseCommands, argv);

          // Gather past release information
          Release.readResumeFile(function handleResumeFile (err, content) {
            // If there was an error, throw it
            if (err) {
              throw err;
            }

            // Parse our JSON (might throw but that's fine)
            var resumeParams = JSON.parse(content);

            // Resume our release
            release.resume(resumeParams, function handleError (err) {
              if (err) {
                throw err;
              }
            });
          });
        });
      });

    program
      .command('commands')
      .description('List commands used by current package')
      .action(function listConfiguration (argv) {
        // Gather configuration for `foundry`
        Foundry.getConfig({}, function handleReleaseLibs (err, config) {
          // If there was an error, throw it
          if (err) {
            throw err;
          } else if (!config.releaseCommands) {
            throw new Error('No `foundry.releaseCommands` was not defined in our `package.json`. ' +
              'Please list out the commands you would like to use for releases');
          }

          // Parse the commands into full objects
          var release = new Release(config.releaseCommands, argv);

          // List our the commands provided
          var infoStr = release.releaseCommands.map(function formatReleaseCommand (releaseCommand) {
            if (releaseCommand.type === 'releaseCommand') {
              // Example: `- foundry-release-echo`
              return '- ' + releaseCommand.command;
            } else if (releaseCommand.type === 'customCommand') {
              // Example: `- <Custom command>\n    - updateFiles: echo hi`
              return [
                '- <Custom command>',
                releaseCommand.updateFiles ? '    - updateFiles: ' + releaseCommand.updateFiles : null,
                releaseCommand.commit ? '    - commit: ' + releaseCommand.commit : null,
                releaseCommand.register ? '    - register: ' + releaseCommand.register : null,
                releaseCommand.publish ? '    - publish: ' + releaseCommand.publish : null
              // DEV: We use filter to remove `null` lines
              ].filter(Boolean).join('\n');
            }
          }).join('\n');
          console.log(infoStr);
        });
      });

    program
      .command('completion')
      .description('Get potential completions for a command. Looks for `COMP_CWORD`, `COMP_LINE`, `COMP_POINT`.')
      .action(function completeLine (/* word1, word2, ..., argv */) {
        // Run completion against environment variables
        program.complete({
          line: process.env.COMP_LINE,
          cursor: process.env.COMP_POINT
        });
      });
  }
});
module.exports = Foundry;
<commit_msg>Removed dev TODO<commit_after>// Load in dependencies
var fs = require('fs');
var util = require('util');
var _ = require('underscore');
var async = require('async');
var Command = require('commander-completion').Command;
var Release = require('./release');

function Foundry() {
  // Set up program
  Command.apply(this, arguments);

  // Bind our commands
  this.bindCommands();
}
Foundry.Release = Release;
Foundry.getConfig = function (params, callback) {
  // In series
  var source;
  var content;
  async.series([
    function attemptToLoadFoundryRc (cb) {
      // Load in our `.foundryrc` (relative to cwd)
      // DEV: We use `readFile` instead of `stat` + `readFile` to prevent the file descriptor from changing
      //   Most likely overkill but it also simplifies the code
      fs.readFile('.foundryrc', 'utf8', function handleFoundryrc (err, _content) {
        // If there was no file, continue to `package.json` resolution
        if (err && err.code === 'ENOENT') {
          return cb();
        // Otherwise if there was any other error, callback with the error
        } else if (err) {
          return cb(err);
        // Otherwise, save the content and continue
        } else {
          source = '.foundryrc';
          content = _content;
          return cb();
        }
      });
    },
    function attemptToLoadJson (cb) {
      // If we already have content, continue
      if (content) {
        return cb();
      }

      // Load in our `package.json` (relative to cwd)
      fs.readFile('package.json', 'utf8', function handlePackageJson (err, _content) {
        // If there was an error, callback with it
        if (err) {
          return cb(err);
        // Otherwise, save our content and continue
        } else {
          source = 'package.json';
          content = _content;
          return cb();
        }
      });
    }
  ], function handleContent (err) {
    // If there was no file anywhere, provide a useful message
    if (err && err.code === 'ENOENT') {
      return callback(new Error('Failed to find a `.foundryrc` or `package.json` file. ' +
        'Please run `foundry` in the same working directory as these files. ' +
        'We enforce this to guarantee steps are given the proper context.'));
    // Otherwise, callback with any other error
    } else if (err) {
      return callback(err);
    }

    // Otherwise, attempt to parse the JSON
    var pkg;
    try {
      pkg = JSON.parse(content);
    } catch (err2) {
      return callback(err2);
    }

    // Extract foundry configuration from `package.json`
    var config = pkg;
    if (source === 'package.json') {
      config = pkg.foundry;
      if (!config) {
        return callback(new Error('No `foundry` field found in `package.json`'));
      }
    }

    // Callback with our config
    return callback(null, config);
  });
};
util.inherits(Foundry, Command);
_.extend(Foundry.prototype, {
  name: 'foundry',
  bindCommands: function () {
    // Set up commands
    var program = this;

    program
      .command('release <version>')
      .description('Update package metadata and publish to registries')
      .option('-n, --dry-run', 'Output steps but don\'t execute them')
      .option('--no-color', 'Removed color from output')
      .action(function callRelease (version, argv) {
        // Gather configuration for `foundry`
        Foundry.getConfig({}, function handleReleaseLibs (err, config) {
          // If there was an error, throw it
          if (err) {
            throw err;
          } else if (!config.releaseCommands) {
            throw new Error('No `foundry.releaseCommands` was not defined in our `package.json`. ' +
              'Please list out the commands you would like to use for releases');
          } else if (config.releaseCommands.length === 0) {
            throw new Error('`foundry.releaseCommands` was found but empty in our `package.json`. ' +
              'Please list out the commands you would like to use for releases');
          }

          // Otherwise, release our package
          var release = new Release(config.releaseCommands, argv);
          release.release(version, function handleError (err) {
            if (err) {
              throw err;
            }
          });
        });
      });

    program
      .command('resume')
      .description('Resume an incomplete release')
      .option('-n, --dry-run', 'Output steps but don\'t execute them')
      .option('--no-color', 'Removed color from output')
      .action(function callResume (version, argv) {
        // Gather configuration for `foundry`
        Foundry.getConfig({}, function handleReleaseLibs (err, config) {
          // If there was an error, throw it
          if (err) {
            throw err;
          } else if (!config.releaseCommands) {
            throw new Error('No `foundry.releaseCommands` was not defined in our `package.json`. ' +
              'Please list out the commands you would like to use for releases');
          } else if (config.releaseCommands.length === 0) {
            throw new Error('`foundry.releaseCommands` was found but empty in our `package.json`. ' +
              'Please list out the commands you would like to use for releases');
          }

          // Initialize our release library (catches any issues with `package.json`)
          var release = new Release(config.releaseCommands, argv);

          // Gather past release information
          Release.readResumeFile(function handleResumeFile (err, content) {
            // If there was an error, throw it
            if (err) {
              throw err;
            }

            // Parse our JSON (might throw but that's fine)
            var resumeParams = JSON.parse(content);

            // Resume our release
            release.resume(resumeParams, function handleError (err) {
              if (err) {
                throw err;
              }
            });
          });
        });
      });

    program
      .command('commands')
      .description('List commands used by current package')
      .action(function listConfiguration (argv) {
        // Gather configuration for `foundry`
        Foundry.getConfig({}, function handleReleaseLibs (err, config) {
          // If there was an error, throw it
          if (err) {
            throw err;
          } else if (!config.releaseCommands) {
            throw new Error('No `foundry.releaseCommands` was not defined in our `package.json`. ' +
              'Please list out the commands you would like to use for releases');
          }

          // Parse the commands into full objects
          var release = new Release(config.releaseCommands, argv);

          // List our the commands provided
          var infoStr = release.releaseCommands.map(function formatReleaseCommand (releaseCommand) {
            if (releaseCommand.type === 'releaseCommand') {
              // Example: `- foundry-release-echo`
              return '- ' + releaseCommand.command;
            } else if (releaseCommand.type === 'customCommand') {
              // Example: `- <Custom command>\n    - updateFiles: echo hi`
              return [
                '- <Custom command>',
                releaseCommand.updateFiles ? '    - updateFiles: ' + releaseCommand.updateFiles : null,
                releaseCommand.commit ? '    - commit: ' + releaseCommand.commit : null,
                releaseCommand.register ? '    - register: ' + releaseCommand.register : null,
                releaseCommand.publish ? '    - publish: ' + releaseCommand.publish : null
              // DEV: We use filter to remove `null` lines
              ].filter(Boolean).join('\n');
            }
          }).join('\n');
          console.log(infoStr);
        });
      });

    program
      .command('completion')
      .description('Get potential completions for a command. Looks for `COMP_CWORD`, `COMP_LINE`, `COMP_POINT`.')
      .action(function completeLine (/* word1, word2, ..., argv */) {
        // Run completion against environment variables
        program.complete({
          line: process.env.COMP_LINE,
          cursor: process.env.COMP_POINT
        });
      });
  }
});
module.exports = Foundry;
<|endoftext|>


<commit_before>'use strict';

var download = require('download');
var HASP_URL = 'https://raw.githubusercontent.com/djanowski/hasp/master/bin/hasp';

(function () {
  console.log('Downloading hasp lib...');

  new download({mode: '755'})
  .get(HASP_URL)
  .dest(__dirname)
  .run(function (error) {
    if (error) {
      throw new Error('hasp download', error.message);
    }

    console.log('...download completed!');
  });
})();
<commit_msg>Readified logs<commit_after>'use strict';

var download = require('download');
var HASP_URL = 'https://raw.githubusercontent.com/djanowski/hasp/master/bin/hasp';

(function () {
  console.log('Downloading latest hasp lib...');

  new download({mode: '755'})
  .get(HASP_URL)
  .dest(__dirname)
  .run(function (error) {
    if (error) {
      throw new Error('hasp download', error.message);
    }

    console.log('...download completed!');
  });
})();
<|endoftext|>


<commit_before>var Gifffer = function() {
    var images, d = document;
    images = d && d.querySelectorAll ? d.querySelectorAll('[data-gifffer]') : [];
    var process = function(el) {
        var url, container, c, w, h, play, trngl, gif, playing = false;
        url = el.getAttribute('data-gifffer');
        el.style.display = 'block';
        c = document.createElement('canvas');
        el.onload = function() {
            if(!!(c.getContext && c.getContext('2d'))) {
                w = el.width;
                h = el.height;
                // creating play button
                play = d.createElement('DIV');
                play.setAttribute('style', 'width:60px;height:60px;border-radius:30px;background:rgba(0, 0, 0, 0.3);position:absolute;left:' + ((w/2)-15
                ) + 'px;top:' + ((h/2)-15) + 'px;');
                trngl = d.createElement('DIV');
                trngl.setAttribute('style', 'width:0;height: 0;border-top:14px solid transparent;border-bottom:14px solid transparent;border-left:14px solid rgba(0, 0, 0, 0.5);position:absolute;left:26px;top:16px;')
                play.appendChild(trngl);
                // creating the container
                container = d.createElement('DIV');
                container.setAttribute('style', 'position:relative;cursor:pointer;width:' + w + 'px;height:' + h + 'px;');
                container.addEventListener('click', function() {
                    if(!playing) {
                        playing = true;
                        gif = d.createElement('IMG');
                        gif.setAttribute('style', 'width:' + w + 'px;height:' + h + 'px;');
                        gif.src = url;
                        container.removeChild(play);
                        container.removeChild(c);
                        container.appendChild(gif);
                    } else {
                        playing = false;
                        container.appendChild(play);
                        container.removeChild(gif);
                        container.appendChild(c);
                        gif = null;
                    }
                });
                // canvas
                c.width = w;
                c.height = h;
                c.getContext('2d').drawImage(el, 0, 0, w, h);
                // dom placement
                container.appendChild(c);
                container.appendChild(play);
                el.parentNode.replaceChild(container, el);
            }
        }
        el.src = url;
    }
    for(var i=0; i<images.length; i++) process(images[i]);
}<commit_msg>Fixing circle position<commit_after>var Gifffer = function() {
    var images, d = document;
    images = d && d.querySelectorAll ? d.querySelectorAll('[data-gifffer]') : [];
    var process = function(el) {
        var url, container, c, w, h, play, trngl, gif, playing = false;
        url = el.getAttribute('data-gifffer');
        el.style.display = 'block';
        c = document.createElement('canvas');
        el.onload = function() {
            if(!!(c.getContext && c.getContext('2d'))) {
                w = el.width;
                h = el.height;
                // creating play button
                play = d.createElement('DIV');
                play.setAttribute('style', 'width:60px;height:60px;border-radius:30px;background:rgba(0, 0, 0, 0.3);position:absolute;left:' + ((w/2)-30
                ) + 'px;top:' + ((h/2)-30) + 'px;');
                trngl = d.createElement('DIV');
                trngl.setAttribute('style', 'width:0;height: 0;border-top:14px solid transparent;border-bottom:14px solid transparent;border-left:14px solid rgba(0, 0, 0, 0.5);position:absolute;left:26px;top:16px;')
                play.appendChild(trngl);
                // creating the container
                container = d.createElement('DIV');
                container.setAttribute('style', 'position:relative;cursor:pointer;width:' + w + 'px;height:' + h + 'px;');
                container.addEventListener('click', function() {
                    if(!playing) {
                        playing = true;
                        gif = d.createElement('IMG');
                        gif.setAttribute('style', 'width:' + w + 'px;height:' + h + 'px;');
                        gif.src = url;
                        container.removeChild(play);
                        container.removeChild(c);
                        container.appendChild(gif);
                    } else {
                        playing = false;
                        container.appendChild(play);
                        container.removeChild(gif);
                        container.appendChild(c);
                        gif = null;
                    }
                });
                // canvas
                c.width = w;
                c.height = h;
                c.getContext('2d').drawImage(el, 0, 0, w, h);
                // dom placement
                container.appendChild(c);
                container.appendChild(play);
                el.parentNode.replaceChild(container, el);
            }
        }
        el.src = url;
    }
    for(var i=0; i<images.length; i++) process(images[i]);
}<|endoftext|>


<commit_before>#!/usr/bin/env node

'use strict';

// Require underscore.
var _ = require('underscore');

var async = require('async');
var util = require('util');
var path = require('path');
var spawn = require('child_process').spawn;
var sh;

// GitFlow Node.js Command-Line Tool
// =================================
// This is the Node.js implementation of GitFlow for those who don't
// want to bother with C programs or Makefiles. If you have Node
// installed already, you can install this CLI from NPM globally in
// order to use it from the terminal.

var usageMsg = 'usage:  gitflow <command> [options]';

// A pre-defined list of commands which are allowed in GitFlow.
var commandList = [
	'feature',
	'init',
	'release',
	'patch',
	'gh-pages'
];

var usage = function() {
	console.log(usageMsg);
}

// CLI
// ---
// The command-line functionality is defined here. First, it takes
// the argument immediately following the script name and checks to
// see if it is a valid command. If so, it invokes the corresponding
// script and passes the remaining arguments to it.
var i;
var command = process.argv[2];

var script;
var scriptPath;

if(_.contains(commandList, command)) {

	script = command + '.sh';
	scriptPath = path.join('../', 'scripts/', script);
	// Call Bash Script
	// ----------------
	// Call bash script residing in the 'scripts/' folder
	// corresponding to the command given.
	command = Array.prototype.concat(
			[scriptPath],
			process.argv.slice(3)
		);

	async.series([
		function(callback) {

			sh = spawn('sh', command, {
				cwd: process.cwd()
			});

			sh.stdout.on('data', function (data) {    // register one or more handlers
				process.stdout.write(data);
			});

			sh.stderr.on('data', function (data) {
				console.log('Error: ', data);
			});

			sh.on('exit', function (code) {
				process.exit(code);
			});

			callback(null);

		}
	],
	function(err, results) {
		if(err) {
			console.log("Error: ", err);
		}
	});

}
// If something went wrong, show the usage information and exit with
// an error signal.
else {
	usage();
	process.exit(1);
}

<commit_msg>bugfix: async function calls<commit_after>#!/usr/bin/env node

'use strict';

// Require underscore.
var _ = require('underscore');

var async = require('async');
var util = require('util');
var path = require('path');
var spawn = require('child_process').spawn;
var sh;

// GitFlow Node.js Command-Line Tool
// =================================
// This is the Node.js implementation of GitFlow for those who don't
// want to bother with C programs or Makefiles. If you have Node
// installed already, you can install this CLI from NPM globally in
// order to use it from the terminal.

var usageMsg = 'usage:  gitflow <command> [options]';

// A pre-defined list of commands which are allowed in GitFlow.
var commandList = [
	'feature',
	'init',
	'release',
	'patch',
	'gh-pages'
];

var usage = function() {
	console.log(usageMsg);
}

// CLI
// ---
// The command-line functionality is defined here. First, it takes
// the argument immediately following the script name and checks to
// see if it is a valid command. If so, it invokes the corresponding
// script and passes the remaining arguments to it.
var i;
var command = process.argv[2];

var script;
var scriptPath;

if(_.contains(commandList, command)) {

	script = command + '.sh';
	scriptPath = path.join('../', 'scripts/', script);
	// Call Bash Script
	// ----------------
	// Call bash script residing in the 'scripts/' folder
	// corresponding to the command given.
	command = Array.prototype.concat(
			[scriptPath],
			process.argv.slice(3)
		);

	async.series([
		function(callback) {

			sh = spawn('sh', command, {
				cwd: process.cwd()
			});

			callback(null);

		},
		function(callback) {
			sh.stdout.on('data', function (data) {
				process.stdout.write(data);
			});

			sh.stderr.on('data', function (data) {
				console.log('Error: ', data);
			});

			sh.on('exit', function (code) {});

			callback(null);
		}
	],
	function(err, results) {
		if(err) {
			console.log("Error: ", err);
		}
	});

}
// If something went wrong, show the usage information and exit with
// an error signal.
else {
	usage();
	process.exit(1);
}

<|endoftext|>


<commit_before>'use strict';

var request = require('request');
var constants = require('./gn-constants');
var q = require('q');

function GnAuth(options) {
  this.clientId = options.clientId;
  this.clientSecret = options.clientSecret;
  this.baseUrl = options.baseUrl;
}

GnAuth.prototype.getAccessToken = function() {
  var defer = q.defer();

  var postParams = {
    url: this.baseUrl + constants.ENDPOINTS.authorize,
    json: true,
    form: {
      grant_type: 'client_credentials',
      client_id: this.clientId,
      client_secret: this.clientSecret
    }
  }

  var callback = function(err, httpResponse, body) {
    if (err)
      defer.reject(err)
    else if (httpResponse.statusCode != 200)
      defer.reject(body)
    else
      defer.resolve(body)
  }

  request.post(postParams, callback);
  return defer.promise;
}

module.exports = GnAuth;<commit_msg>Adjust config params<commit_after>'use strict';

var request = require('request');
var constants = require('./gn-constants');
var q = require('q');

function GnAuth(options) {
  this.clientId = options.client_id;
  this.clientSecret = options.client_secret;
  this.baseUrl = options.baseUrl;
}

GnAuth.prototype.getAccessToken = function () {
  var defer = q.defer();

  var postParams = {
    url: this.baseUrl + constants.ENDPOINTS.authorize,
    json: true,
    form: {
      grant_type: 'client_credentials',
      client_id: this.clientId,
      client_secret: this.clientSecret
    }
  }

  var callback = function (err, httpResponse, body) {
    if (err)
      defer.reject(err)
    else if (httpResponse.statusCode != 200)
      defer.reject(body)
    else
      defer.resolve(body)
  }

  request.post(postParams, callback);
  return defer.promise;
}

module.exports = GnAuth;<|endoftext|>


<commit_before>var cheerio = require('cheerio');
var helpers = require('./helpers');

exports = module.exports = function(site, body, callback) {
    if (!body) return callback('no body to parse');
    if (!site) return callback('no template to parse body with');

    try {
        parseSite(body, site, callback);
    } catch (e) {
        callback({
            template: site.name,
            msg: 'There is a problem with your supplied template',
            error: e
        });
    }
};

function parseSite(body, site, callback) {
    var $ = cheerio.load(body);
    var resultList = [];
    var ranking = 1;

    // THIS IS SYNCRONOUS (AND SHOULD/HAS TO BE)
    site.template.containers.forEach(function(container) {
        // loop through all the container types that can hold articles
        $(container.selector).each(function() {
            // flag for valid entry
            var validEntry = true;

            // article-entry
            var entry = {
                origin: site.url
            };

            // add article styles to entry
            processContainer($, container, this, entry);

            // this is where we fetch all the data
            container.elements.forEach(function(element) {
                // loop through all the items
                element.items.forEach(function(item) {

                    var allowed = ['first', 'last', 'all'];
                    var method = 'first';
                    var selected;
                    var result;

                    // force which element to pick from the results (first or last, default to each)
                    if (element.occurence && ~allowed.indexOf(element.occurence.toLowerCase())) {
                        method = element.occurence.toLowerCase();
                    }

                    // process on item-level
                    if (method === 'all') {
                        // iterate through the matches
                        $(this).find(item.selector).each(function() {
                            selected = this;
                            result = processItem($, selected, item, element);

                            // set item if it has been found
                            if (result) {
                                entry[element.name] = entry[element.name] || []
                                entry[element.name].push(result);
                            }
                        });
                    } else if (method === 'last') {
                        selected = $(this).find(item.selector).last();
                        result = processItem($, selected, item, element);

                        // set item if it has been found
                        if (result && !entry[element.name]) {
                            entry[element.name] = result;
                        }
                    } else {
                        selected = $(this).find(item.selector).first();
                        result = processItem($, selected, item, element);

                        // set item if it has been found
                        if (result && !entry[element.name]) {
                            entry[element.name] = result;
                        }
                    }

                }.bind(this));

                // process on entry-level
                entry = processElement(element, entry);

                // check if item is required for entry to be valid, and then check if item is set
                // validEntry cascades through every item, so that all required properties must be valid
                // to get make validEntry true at the end
                if (element.required && !entry[element.name]) {
                    validEntry = false;
                } else {
                    validEntry = validEntry && true;
                }

            }.bind(this));

            // push entry to array (if a link is found in the article)
            if (validEntry) {
                entry.ranking = ranking++;
                resultList.push(entry);
            }
        });
    });

    callback(null, resultList);
    //return resultList;
}

function processItem($, selected, item, element, callback) {
    var holder; // hold the results
    var tempHolder; // temporary holder helper

    // find attribute (use element text if no attribute is provided)
    if (item.attribute && $(selected).attr(item.attribute)) {
        holder = $(selected).attr(item.attribute).trim();
    } else {
        holder = $(selected).text().trim();
    }

    // delimit the text if required (on a certain text-phrase like "read more")
    var delimiter = element.delimiter || item.delimiter;
    if (holder && delimiter) {
        tempHolder = holder.toString();
        holder = tempHolder.slice(0, tempHolder.indexOf(delimiter)).trim();
    }

    // attach classes if required
    if (holder && (element.classes || item.classes)) {
        holder = {
            content: holder,
            classes: $(selected).attr('class') || ''
        };
    }

    return holder;
}

function processElement(element, entry) {
    // add fallback if supplied
    if (element.fallback && !entry[element.name]) {
        entry[element.name] = element.fallback;
    }

    // resolve relative urls
    if (entry[element.name] && (element.type == 'url')) {
        entry[element.name] = helpers.fixRelativePath(entry[element.name], entry.origin);
    }

    return entry;
}

function processContainer($, container, selected, entry) {
    if (container.classes) {
        entry.classes = $(selected).attr('class').split(' ');
    }
}
<commit_msg>use semi-private namespace for default parameters<commit_after>var cheerio = require('cheerio');
var helpers = require('./helpers');
var url = require('url');

exports = module.exports = function(site, body, callback) {
    if (!body) return callback('no body to parse');
    if (!site) return callback('no template to parse body with');

    try {
        parseSite(body, site, callback);
    } catch (e) {
        callback({
            template: site.name,
            msg: 'There is a problem with your supplied template',
            error: e
        });
    }
};

function parseSite(body, site, callback) {
    var $ = cheerio.load(body);
    var resultList = [];
    var ranking = 1;

    // THIS IS SYNCRONOUS (AND SHOULD/HAS TO BE)
    site.template.containers.forEach(function(container) {
        // loop through all the container types that can hold articles
        $(container.selector).each(function() {
            // flag for valid entry
            var validEntry = true;

            // article-entry
            var entry = {
                _source: site.name,
                _origin: site.url,
                _host: url.parse(site.url).host
            };

            // add article styles to entry
            processContainer($, container, this, entry);

            // this is where we fetch all the data
            container.elements.forEach(function(element) {
                // loop through all the items
                element.items.forEach(function(item) {

                    var allowed = ['first', 'last', 'all'];
                    var method = 'first';
                    var selected;
                    var result;

                    // force which element to pick from the results (first or last, default to each)
                    if (element.occurence && ~allowed.indexOf(element.occurence.toLowerCase())) {
                        method = element.occurence.toLowerCase();
                    }

                    // process on item-level
                    if (method === 'all') {
                        // iterate through the matches
                        $(this).find(item.selector).each(function() {
                            selected = this;
                            result = processItem($, selected, item, element);

                            // set item if it has been found
                            if (result) {
                                entry[element.name] = entry[element.name] || []
                                entry[element.name].push(result);
                            }
                        });
                    } else if (method === 'last') {
                        selected = $(this).find(item.selector).last();
                        result = processItem($, selected, item, element);

                        // set item if it has been found
                        if (result && !entry[element.name]) {
                            entry[element.name] = result;
                        }
                    } else {
                        selected = $(this).find(item.selector).first();
                        result = processItem($, selected, item, element);

                        // set item if it has been found
                        if (result && !entry[element.name]) {
                            entry[element.name] = result;
                        }
                    }

                }.bind(this));

                // process on entry-level
                entry = processElement(element, entry);

                // check if item is required for entry to be valid, and then check if item is set
                // validEntry cascades through every item, so that all required properties must be valid
                // to get make validEntry true at the end
                if (element.required && !entry[element.name]) {
                    validEntry = false;
                } else {
                    validEntry = validEntry && true;
                }

            }.bind(this));

            // push entry to array (if a link is found in the article)
            if (validEntry) {
                entry._ranking = ranking++;
                resultList.push(entry);
            }
        });
    });

    callback(null, resultList);
    //return resultList;
}

function processItem($, selected, item, element, callback) {
    var holder; // hold the results
    var tempHolder; // temporary holder helper

    // find attribute (use element text if no attribute is provided)
    if (item.attribute && $(selected).attr(item.attribute)) {
        holder = $(selected).attr(item.attribute).trim();
    } else {
        holder = $(selected).text().trim();
    }

    // delimit the text if required (on a certain text-phrase like "read more")
    var delimiter = element.delimiter || item.delimiter;
    if (holder && delimiter) {
        tempHolder = holder.toString();
        holder = tempHolder.slice(0, tempHolder.indexOf(delimiter)).trim();
    }

    // attach classes if required
    if (holder && (element.classes || item.classes)) {
        holder = {
            content: holder,
            classes: $(selected).attr('class') || ''
        };
    }

    return holder;
}

function processElement(element, entry) {
    // add fallback if supplied
    if (element.fallback && !entry[element.name]) {
        entry[element.name] = element.fallback;
    }

    // resolve relative urls
    if (entry[element.name] && (element.type == 'url')) {
        entry[element.name] = helpers.fixRelativePath(entry[element.name], entry._origin);
    }

    return entry;
}

function processContainer($, container, selected, entry) {
    if (container.classes) {
        entry.classes = $(selected).attr('class').split(' ');
    }
}
<|endoftext|>


<commit_before>const Aggregate = require('./Aggregate');

exports.mongo = (self, message, done) => {
    //parse the body of the messager
    const json = JSON.parse(message.Body);

    //check the parsed object for the data we need
    if (json.Records &&
        json.Records[0] &&
        json.Records[0].s3 &&
        json.Records[0].s3.bucket &&
        json.Records[0].s3.bucket.name &&
        json.Records[0].s3.object &&
        json.Records[0].s3.object.key) {

        //s3 read
        let bucket = json.Records[0].s3.bucket.name;
        let key = json.Records[0].s3.object.key;
        let readParams = { Bucket: bucket, Key: key };

        self.aggregate = new Aggregate(self.c, self.mongo, self.config);
        self.aggregate.save(readParams, (err) => {
            done(err);
        });

    } else {
        done();
    }
};

exports.redshift = (self, message, done) => {
    //parse the body of the messager
    const json = JSON.parse(message.Body);

    //check the parsed object for the data we need
    if (json.Records &&
        json.Records[0] &&
        json.Records[0].s3 &&
        json.Records[0].s3.bucket &&
        json.Records[0].s3.bucket.name &&
        json.Records[0].s3.object &&
        json.Records[0].s3.object.key) {

        //s3 read
        let bucket = json.Records[0].s3.bucket.name;
        let key = json.Records[0].s3.object.key;
        let readParams = { Bucket: bucket, Key: key };
        let query;

        if (self.config.environment === 'sandbox') {
            query = `COPY dax from 's3://${readParams.Bucket}/${readParams.Key}'
credentials 'aws_access_key_id=${self.sqsConfig.accessKeyId};aws_secret_access_key=${self.sqsConfig.secretAccessKey}'
gzip delimiter ',' removequotes escape TRUNCATECOLUMNS TRIMBLANKS NULL AS 'NULL';`;
        } else {
            query = `COPY dax from 's3://${readParams.Bucket}/${readParams.Key}'
iam_role '${self.config.output.postgresql.iam}'
gzip delimiter ',' removequotes escape TRUNCATECOLUMNS TRIMBLANKS NULL AS 'NULL';`;
        }

        self.postgresql.query(query, (err) => {
            if (err) {
                self.c.a('Error Copying ' + key + ' to redshift', 3, 'Poller');
                self.c.a(JSON.stringify(err), 3, 'Poller-Redshift');
            } else {
                self.c.a('Finsihed Copying ' + key + ' to redshift', 1, 'Poller');
            }
            done();
        });

    } else {
        done();
    }
};<commit_msg>updated keys<commit_after>const Aggregate = require('./Aggregate');

exports.mongo = (self, message, done) => {
    //parse the body of the messager
    const json = JSON.parse(message.Body);

    //check the parsed object for the data we need
    if (json.Records &&
        json.Records[0] &&
        json.Records[0].s3 &&
        json.Records[0].s3.bucket &&
        json.Records[0].s3.bucket.name &&
        json.Records[0].s3.object &&
        json.Records[0].s3.object.key) {

        //s3 read
        let bucket = json.Records[0].s3.bucket.name;
        let key = json.Records[0].s3.object.key;
        let readParams = { Bucket: bucket, Key: key };

        self.aggregate = new Aggregate(self.c, self.mongo, self.config);
        self.aggregate.save(readParams, (err) => {
            done(err);
        });

    } else {
        done();
    }
};

exports.redshift = (self, message, done) => {
    //parse the body of the messager
    const json = JSON.parse(message.Body);

    //check the parsed object for the data we need
    if (json.Records &&
        json.Records[0] &&
        json.Records[0].s3 &&
        json.Records[0].s3.bucket &&
        json.Records[0].s3.bucket.name &&
        json.Records[0].s3.object &&
        json.Records[0].s3.object.key) {

        //s3 read
        let bucket = json.Records[0].s3.bucket.name;
        let key = json.Records[0].s3.object.key;
        let readParams = { Bucket: bucket, Key: key };
        let query;

        if (self.config.environment === 'sandbox') {
            query = `COPY dax from 's3://${readParams.Bucket}/${readParams.Key}'
credentials 'aws_access_key_id=${self.config.output.postgresql.accessKeyId};aws_secret_access_key=${self.config.output.postgresql.secretAccessKey}'
gzip delimiter ',' removequotes escape TRUNCATECOLUMNS TRIMBLANKS NULL AS 'NULL';`;
        } else {
            query = `COPY dax from 's3://${readParams.Bucket}/${readParams.Key}'
iam_role '${self.config.output.postgresql.iam}'
gzip delimiter ',' removequotes escape TRUNCATECOLUMNS TRIMBLANKS NULL AS 'NULL';`;
        }

        self.postgresql.query(query, (err) => {
            if (err) {
                self.c.a('Error Copying ' + key + ' to redshift', 3, 'Poller');
                self.c.a(JSON.stringify(err), 3, 'Poller-Redshift');
            } else {
                self.c.a('Finsihed Copying ' + key + ' to redshift', 1, 'Poller');
            }
            done();
        });

    } else {
        done();
    }
};<|endoftext|>


<commit_before>'use babel'

const COLOR_EXTRACTION_REGEXP = /rgb\(\d+, ?\d+, ?\d+\)|rgba\(\d+, ?\d+, ?\d+, ?\d?\.?\d+\)|#[0-9a-f]{3,6}/

export function isColor(textEditor, bufferPosition) {
  const lineText = textEditor.getTextInRange([[bufferPosition.row, 0], [bufferPosition.row, Infinity]])
  const matches = COLOR_EXTRACTION_REGEXP.exec(lineText)
  if (matches === null) {
    return false
  }
  const offsetStart = matches.index
  const offsetEnd = offsetStart + matches[0].length
  return bufferPosition.column >= offsetStart && offsetEnd >= bufferPosition.column
}
<commit_msg>:new: Support multiple colors on the same line<commit_after>'use babel'

const COLOR_EXTRACTION_REGEXP = /rgb\(\d+, ?\d+, ?\d+\)|rgba\(\d+, ?\d+, ?\d+, ?\d?\.?\d+\)|#[0-9a-f]{3,6}/g

export function isColor(textEditor, bufferPosition) {
  const lineText = textEditor.getTextInRange([[bufferPosition.row, 0], [bufferPosition.row, Infinity]])
  let matches
  while ((matches = COLOR_EXTRACTION_REGEXP.exec(lineText)) !== null) {
    const offsetStart = matches.index
    const offsetEnd = offsetStart + matches[0].length
    if (bufferPosition.column >= offsetStart && offsetEnd >= bufferPosition.column) {
      return true
    }
  }
  return false
}
<|endoftext|>


<commit_before>var moment = require('moment')
  , util = require('util')

exports.withinActiveTime = function(payload,zoneData) {

  if (!zoneData.activeTimes)
    return false;

  var startOfWeek = moment().day(0).hours(0).minutes(0).seconds(0).milliseconds(0).valueOf();
  var now = moment(payload.timestamp).valueOf()-startOfWeek;
  var times = zoneData.activeTimes;

  for (var i=0;i<times.length;i++) {

    var from = times[i];
    var to = times[++i];

    if (now > from && now < to) return true;
  }

  return false;
}
exports.validateAlert = function(type,alertee) {

  switch (type) {

    case 'email':
      if (alertee.indexOf('@')===-1) return 'invalid email address';
    break;

    case 'call':
    case 'sms':
      var numberTest = /\+(9[976]\d|8[987530]\d|6[987]\d|5[90]\d|42\d|3[875]\d|2[98654321]\d|9[8543210]|8[6421]|6[6543210]|5[87654321]|4[987654310]|3[9643210]|2[70]|7|1)\d{1,14}$/
      if (!numberTest.test(alertee)) return 'invalid phone number, must be of the form +xxxxxxxxxxxx';
    break;

    default: return 'unkown `type`'; break;
  }
  return true;
};

exports.validateActiveTimes = function(times) {

  if (!util.isArray(times) || times.length % 2 !== 0) {
    return 'it must be an array of integer timestamp couplets, ie [from,to,from,to]';
  }
  for (var i=0;i<times.length;i++) {
    if (typeof times[i] !== "number") return 'each element of the array must be a number';
  }
  return true;
}

exports.intruderAlert = function(req,zoneData,alertData) {

  for (var a in alertData) {
    if (alertData.hasOwnProperty(a) && alertData[a].active===true) {
      switch(alertData[a].type) {

        case 'email':
          sendAlertEmail(req,alertData[a].alertee,zoneData);
        break;

        case 'call' :
          makeAlertCall(req,alertData[a].alertee,zoneData);
        break;

        case 'sms':
          sendAlertSms(req,alertData[a].alertee,zoneData);
        break;

      }
    }
  }
};

function sendAlertSms(req,to,zone) {

  req.phone.sendSms(to,'This is Ninja Sentinel. You have an alert in zone... '+zone.name,{},function(sms) {

    exports.logActivity(req.redisClient,req.body.id,'alert','SMS to '+to+' was sent');
  });

}

function makeAlertCall(req,to,zone) {

  req.phone.makeCall(to, null, function(call) {

    var Twiml = require('twilio').Twiml;

    call.on('answered', function(reqParams, res) {

        exports.logActivity(req.redisClient,req.body.id,'alert','Call to '+to+' was answered');
        res.append(new Twiml.Say('This is Ninja Sentinel. You have an alert in zone... '+zone.name));
        res.send();
    });

    call.on('ended', function(reqParams) {
      console.logconsole.log("User "+req.body.id+' Zone '+zone.id+' call ended');
    });
  });

}

function sendAlertEmail(req,to,zone) {

  var ago = moment(req.body.timestamp).fromNow()
  var at = moment(req.body.tinmestamp).toDate()

  var body = [
    'Zone ',
    zone.name,
    ' was alerted ',
    ago,
    ' at ',
    at
  ].join('');

  req.mailer.sendMail({
      to : to,
      from : process.env.EMAIL_FROM,
      subject : "[SENTINEL] Alert in "+zone.name,
      text: body
    },
    function(err, result) {
      if (err) console.log(err);
      else exports.logActivity(req.redisClient,req.body.id,'alert','Email to '+to+' was sent');
  });

}

exports.logActivity = function(redisClient,id,type,message) {
  var msg = {
    type:type,
    message:message,
    timestamp:new Date().getTime()
  };
  var key = 'user:'+id+':history';

  redisClient.lpush(key,JSON.stringify(msg),function(err) {
    if (err) console.log(err);
  });
  redisClient.ltrim(key,'0','1000')
};<commit_msg>Fix console.log<commit_after>var moment = require('moment')
  , util = require('util')

exports.withinActiveTime = function(payload,zoneData) {

  if (!zoneData.activeTimes)
    return false;

  var startOfWeek = moment().day(0).hours(0).minutes(0).seconds(0).milliseconds(0).valueOf();
  var now = moment(payload.timestamp).valueOf()-startOfWeek;
  var times = zoneData.activeTimes;

  for (var i=0;i<times.length;i++) {

    var from = times[i];
    var to = times[++i];

    if (now > from && now < to) return true;
  }

  return false;
}
exports.validateAlert = function(type,alertee) {

  switch (type) {

    case 'email':
      if (alertee.indexOf('@')===-1) return 'invalid email address';
    break;

    case 'call':
    case 'sms':
      var numberTest = /\+(9[976]\d|8[987530]\d|6[987]\d|5[90]\d|42\d|3[875]\d|2[98654321]\d|9[8543210]|8[6421]|6[6543210]|5[87654321]|4[987654310]|3[9643210]|2[70]|7|1)\d{1,14}$/
      if (!numberTest.test(alertee)) return 'invalid phone number, must be of the form +xxxxxxxxxxxx';
    break;

    default: return 'unkown `type`'; break;
  }
  return true;
};

exports.validateActiveTimes = function(times) {

  if (!util.isArray(times) || times.length % 2 !== 0) {
    return 'it must be an array of integer timestamp couplets, ie [from,to,from,to]';
  }
  for (var i=0;i<times.length;i++) {
    if (typeof times[i] !== "number") return 'each element of the array must be a number';
  }
  return true;
}

exports.intruderAlert = function(req,zoneData,alertData) {

  for (var a in alertData) {
    if (alertData.hasOwnProperty(a) && alertData[a].active===true) {
      switch(alertData[a].type) {

        case 'email':
          sendAlertEmail(req,alertData[a].alertee,zoneData);
        break;

        case 'call' :
          makeAlertCall(req,alertData[a].alertee,zoneData);
        break;

        case 'sms':
          sendAlertSms(req,alertData[a].alertee,zoneData);
        break;

      }
    }
  }
};

function sendAlertSms(req,to,zone) {

  req.phone.sendSms(to,'This is Ninja Sentinel. You have an alert in zone... '+zone.name,{},function(sms) {

    exports.logActivity(req.redisClient,req.body.id,'alert','SMS to '+to+' was sent');
  });

}

function makeAlertCall(req,to,zone) {

  req.phone.makeCall(to, null, function(call) {

    var Twiml = require('twilio').Twiml;

    call.on('answered', function(reqParams, res) {

        exports.logActivity(req.redisClient,req.body.id,'alert','Call to '+to+' was answered');
        res.append(new Twiml.Say('This is Ninja Sentinel. You have an alert in zone... '+zone.name));
        res.send();
    });

    call.on('ended', function(reqParams) {
      console.log("User "+req.body.id+' Zone '+zone.id+' call ended');
    });
  });

}

function sendAlertEmail(req,to,zone) {

  var ago = moment(req.body.timestamp).fromNow()
  var at = moment(req.body.tinmestamp).toDate()

  var body = [
    'Zone ',
    zone.name,
    ' was alerted ',
    ago,
    ' at ',
    at
  ].join('');

  req.mailer.sendMail({
      to : to,
      from : process.env.EMAIL_FROM,
      subject : "[SENTINEL] Alert in "+zone.name,
      text: body
    },
    function(err, result) {
      if (err) console.log(err);
      else exports.logActivity(req.redisClient,req.body.id,'alert','Email to '+to+' was sent');
  });

}

exports.logActivity = function(redisClient,id,type,message) {
  var msg = {
    type:type,
    message:message,
    timestamp:new Date().getTime()
  };
  var key = 'user:'+id+':history';

  redisClient.lpush(key,JSON.stringify(msg),function(err) {
    if (err) console.log(err);
  });
  redisClient.ltrim(key,'0','1000')
};<|endoftext|>