documentdb-utils
Version:
Drop-in replacement+extensions for Azure's DocumentDB node.js client with auto-retry on 429 errors plus a lot more
124 lines (115 loc) • 4.57 kB
JavaScript
// Generated by CoffeeScript 1.9.2
(function() {
var _, indent, insertMixins, path, spaces, startingSpacesCount;
path = require('path');
_ = require('lodash');
spaces = function(n) {
var a;
a = new Array(n - 1);
return a.join(' ');
};
startingSpacesCount = function(line) {
var char, j, len, spaceCount;
spaceCount = 0;
for (j = 0, len = line.length; j < len; j++) {
char = line[j];
if (char === ' ') {
spaceCount++;
}
}
return spaceCount;
};
indent = function(s, n) {
var j, len, line, lines, outputLines;
outputLines = [];
lines = s.split('\n');
for (j = 0, len = lines.length; j < len; j++) {
line = lines[j];
if (line.trim().length > 0) {
outputLines.push(spaces(n) + line);
} else {
outputLines.push(line);
}
}
return outputLines.join('\n');
};
insertMixins = function(source, minify) {
var afterRequireArray, afterRequireIndex, afterRequireString, currentLine, functionToInsert, functionToInsertString, i, key, keyIndex, keyString, nextReference, sourceLines, sourceString, spacesToIndent, stringsToInsert, toRequireString, value, variableString;
if (minify == null) {
minify = false;
}
switch (typeof source) {
case 'function':
sourceString = source.toString();
break;
case 'string':
sourceString = source;
}
keyString = "= require(";
keyIndex = sourceString.indexOf(keyString);
if (keyIndex >= 0) {
sourceLines = sourceString.split('\n');
i = 0;
currentLine = sourceLines[i];
while (currentLine.indexOf(keyString) < 0) {
i++;
currentLine = sourceLines[i];
}
keyIndex = currentLine.indexOf(keyString);
variableString = currentLine.substr(0, keyIndex).trim();
toRequireString = currentLine.substr(keyIndex + keyString.length + 1);
afterRequireIndex = toRequireString.indexOf("').");
if (afterRequireIndex >= 0) {
afterRequireString = toRequireString.substr(afterRequireIndex + 3);
afterRequireString = afterRequireString.substr(0, afterRequireString.length - 1);
afterRequireArray = afterRequireString.split('.');
}
toRequireString = toRequireString.substr(0, toRequireString.indexOf("'"));
functionToInsert = require(toRequireString);
while ((afterRequireArray != null ? afterRequireArray.length : void 0) > 0) {
nextReference = afterRequireArray.shift();
functionToInsert = functionToInsert[nextReference];
}
spacesToIndent = startingSpacesCount(currentLine);
if (_.isFunction(functionToInsert)) {
functionToInsertString = indent(variableString + " = " + functionToInsert.toString() + ';\n', spacesToIndent);
} else if (_.isString(functionToInsert) || _.isBoolean(functionToInsert) || _.isNull(functionToInsert) || _.isNumber(functionToInsert)) {
functionToInsertString = indent(variableString + " = " + JSON.stringify(functionToInsert) + ';\n', spacesToIndent);
} else if (_.isPlainObject(functionToInsert)) {
functionToInsertString = indent(variableString + " = {\n", spacesToIndent);
stringsToInsert = [];
for (key in functionToInsert) {
value = functionToInsert[key];
if (_.isFunction(value)) {
stringsToInsert.push(indent(key + ": " + value.toString(), spacesToIndent + 2));
} else if (_.isString(value) || _.isBoolean(value) || _.isNull(value) || _.isNumber(value)) {
stringsToInsert.push(indent(key + ": " + JSON.stringify(value), spacesToIndent + 2));
} else {
throw new Error((typeof value) + " is not a supported type for expandScript");
}
}
functionToInsertString += stringsToInsert.join(',\n') + '\n' + indent('};', spacesToIndent);
} else {
throw new Error((typeof functionToInsert) + " is not a supported type for expandScript");
}
sourceLines[i] = functionToInsertString;
sourceString = sourceLines.join('\n');
return insertMixins(sourceString, minify);
} else {
if (minify) {
console.log('Minify not implemented yet');
}
return sourceString;
}
};
module.exports = function(sourceFile) {
var body, id, sourceFunction;
sourceFunction = require(sourceFile);
id = path.basename(sourceFile, '.coffee');
body = insertMixins(sourceFunction.toString());
return {
id: id,
body: body
};
};
}).call(this);