示例#1
0
  var doReplace = function (file, callback) {

    var origReplacement = replacement;

    if (typeof replacement === 'function') {

      // If second parameter is __absolutePath__, inject the files absolute path as second parameter
      // TODO: Get index of __absolutePath__ and inject it dynamically
      if (/function[\s]*\([\w]+\,[\s]*__absolutePath__[\s]*\)*/.test(replacement.toString())) {
        replacement = function (file) {
          return origReplacement.apply(null, [arguments[0], file.path]);
        }
      }
      else {
        replacement = function () {
          return origReplacement.apply(null, arguments);
        }
      }
    }
    else {
      replacement = function () {
        return origReplacement;
      }
    }

    var isRegExp = search instanceof RegExp;
    var isStream = file.contents && typeof file.contents.on === 'function' && typeof file.contents.pipe === 'function';
    var isBuffer = file.contents instanceof Buffer;

    if (isRegExp && isStream) {
      return callback(new Error('gulp-replace-path: Cannot do regexp replace on a stream'), file);
    }

    function doReplace() {

      if (isStream) {
        file.contents = file.contents.pipe(rs(search, replacement(file)));
        return callback(null, file);
      }

      if (isBuffer) {
        if (isRegExp) {
          file.contents = new Buffer(String(file.contents).replace(search, replacement(file)));
        }
        else {
          var chunks = String(file.contents).split(search);

          var result;
          if (typeof replacement(file) === 'function') {
            // Start with the first chunk already in the result
            // Replacements will be added thereafter
            // This is done to avoid checking the value of i in the loop
            result = [chunks[0]];

            // The replacement function should be called once for each match
            for (var i = 1; i < chunks.length; i++) {
              // Add the replacement value
              result.push(replacement(file)(search));

              // Add the next chunk
              result.push(chunks[i]);
            }

            result = result.join('');
          }
          else {
            result = chunks.join(replacement(file));
          }

          file.contents = new Buffer(result);
        }
        return callback(null, file);
      }

      callback(null, file);

    }

    if (options && options.skipBinary) {
      istextorbinary.isText('', file.contents, function (err, result) {
        if (err) {
          return callback(err, file);
        }

        if (!result) {
          return callback(null, file);
        } else {
          doReplace();
        }
      });
    }
    else {
      doReplace();
    }

  };
示例#2
0
  var doReplace = function(file, enc, callback) {
    var repl = wrapWithFile(replacement, file);

    if (file.isNull()) {
      return callback(null, file);
    }

    function doReplace() {
      if (file.isStream()) {
        file.contents = file.contents.pipe(rs(search, repl));
        return callback(null, file);
      }

      if (file.isBuffer()) {
        if (search instanceof RegExp) {
          file.contents = new Buffer(String(file.contents).replace(search, repl));
        }
        else {
          var chunks = String(file.contents).split(search);

          var result;
          if (typeof repl === 'function') {
            // Start with the first chunk already in the result
            // Replacements will be added thereafter
            // This is done to avoid checking the value of i in the loop
            result = [ chunks[0] ];

            // The replacement function should be called once for each match
            for (var i = 1; i < chunks.length; i++) {
              // Add the replacement value
              result.push(repl(search, file));

              // Add the next chunk
              result.push(chunks[i]);
            }

            result = result.join('');
          }
          else {
            result = chunks.join(repl);
          }

          file.contents = new Buffer(result);
        }
        return callback(null, file);
      }

      callback(null, file);
    }

    if (options && options.skipBinary) {
      istextorbinary.isText('', file.contents, function(err, result) {
        if (err) {
          return callback(err, file);
        }

        if (!result) {
          callback(null, file);
        } else {
          doReplace();
        }
      });

      return;
    }

    doReplace();
  };
          fs.readFile(settings.folder + "/" + file, function(err,data) {


            if (err) {
              //crapout(err);
              console.log("    - ERROR reading file prior to save: " + settings.folder + "/" + file);
              deferred2.resolve(settings.folder + "/" + file);
            } else {
              itob.isText(file,data,function (err,result) {
                if (result) {
                  data = data.toString(); // convert to string if utf8, otherwise leave as binary buffer
                }

              // actually upload the file once working

              var vf = settings.folder;
              if (settings.stripBaseFolder) {
                vf = settings.folder.substring(base.length + 1);
              }
              /*if (0 == vf.indexOf("/")) {
                vf = vf.substring(1);
              }*/
              if (0 != vf.indexOf("/") && vf.length != 0) {
                vf = "/" + vf;
              }
              var vff = file;
              /*if (0 == vff.indexOf("/")) {
                vff = vff.substring(1);
              }*/
              if (0 != vff.indexOf("/")) {
                vff = "/" + vff;
              }
              var uri = settings.prefix + vf + vff;
              if ("//"==uri.substring(0,2)) {
                uri = uri.substring(1); // remove extra slash at front
              }
              var cols = "";
              for (var c = 0, maxc = settings.collections.length,col;c < maxc;c++) {
                col = settings.collections[c];
                if (c > 0) {
                  cols += ",";
                }
                cols += col;
              }
              var props = {
              };
              if (undefined != cols && "" != cols) {
                props.collection=cols;
              }
              if (uri.substring(uri.length - 4) == ".xqy") {
                props.contentType = "application/xquery";
              }
              db.save(data,uri,props,function(result) {
                if (result.inError) {
                  // just log the message
                  console.log("    - ERROR saving file to uri: " + uri);
                } else {
                  console.log("    - SUCCESS " + settings.folder + "/" + file + " => " + uri + " (" + result.docuri + ")");
                }
                deferred2.resolve(settings.folder + "/" + file);
              });

              }); // end itob
            } // end error if
          });