diff options
Diffstat (limited to 'lib/utils')
-rw-r--r-- | lib/utils/git.js | 130 | ||||
-rw-r--r-- | lib/utils/i18n.js | 76 | ||||
-rw-r--r-- | lib/utils/images.js | 16 | ||||
-rw-r--r-- | lib/utils/logger.js | 176 |
4 files changed, 199 insertions, 199 deletions
diff --git a/lib/utils/git.js b/lib/utils/git.js index 9a669db..2c3dd3f 100644 --- a/lib/utils/git.js +++ b/lib/utils/git.js @@ -15,7 +15,7 @@ var GIT_TMP = null; // Check if an url is a git dependency url function checkGitUrl(giturl) { - return (giturl.indexOf(GIT_PREFIX) === 0); + return (giturl.indexOf(GIT_PREFIX) === 0); } // Validates a SHA in hexadecimal @@ -25,88 +25,88 @@ function validateSha(str) { // Parse and extract infos function parseGitUrl(giturl) { - var ref, uri, fileParts, filepath; + var ref, uri, fileParts, filepath; - if (!checkGitUrl(giturl)) return null; - giturl = giturl.slice(GIT_PREFIX.length); + if (!checkGitUrl(giturl)) return null; + giturl = giturl.slice(GIT_PREFIX.length); - uri = new URI(giturl); - ref = uri.fragment() || "master"; - uri.fragment(null); + uri = new URI(giturl); + ref = uri.fragment() || "master"; + uri.fragment(null); - // Extract file inside the repo (after the .git) - fileParts =uri.path().split(".git"); - filepath = fileParts.length > 1? fileParts.slice(1).join(".git") : ""; - if (filepath[0] == "/") filepath = filepath.slice(1); + // Extract file inside the repo (after the .git) + fileParts =uri.path().split(".git"); + filepath = fileParts.length > 1? fileParts.slice(1).join(".git") : ""; + if (filepath[0] == "/") filepath = filepath.slice(1); - // Recreate pathname without the real filename - uri.path(_.first(fileParts)+".git"); + // Recreate pathname without the real filename + uri.path(_.first(fileParts)+".git"); - return { - host: uri.toString(), - ref: ref || "master", - filepath: filepath - }; + return { + host: uri.toString(), + ref: ref || "master", + filepath: filepath + }; } // Clone a git repo from a specific ref function cloneGitRepo(host, ref) { - var isBranch = false; - - ref = ref || "master"; - if (!validateSha(ref)) isBranch = true; - - return Q() - - // Create temporary folder to store git repos - .then(function() { - if (GIT_TMP) return; - return fs.tmp.dir() - .then(function(_tmp) { - GIT_TMP = _tmp; - }); - }) - - // Return or clone the git repo - .then(function() { - // Unique ID for repo/ref combinaison - var repoId = crc.crc32(host+"#"+ref).toString(16); - - // Absolute path to the folder - var repoPath = path.resolve(GIT_TMP, repoId); - - return fs.exists(repoPath) - .then(function(doExists) { - if (doExists) return; - - // Clone repo - return exec("git clone "+host+" "+repoPath) - .then(function() { - return exec("git checkout "+ref, { cwd: repoPath }); - }) - }) - .thenResolve(repoPath); - }); + var isBranch = false; + + ref = ref || "master"; + if (!validateSha(ref)) isBranch = true; + + return Q() + + // Create temporary folder to store git repos + .then(function() { + if (GIT_TMP) return; + return fs.tmp.dir() + .then(function(_tmp) { + GIT_TMP = _tmp; + }); + }) + + // Return or clone the git repo + .then(function() { + // Unique ID for repo/ref combinaison + var repoId = crc.crc32(host+"#"+ref).toString(16); + + // Absolute path to the folder + var repoPath = path.resolve(GIT_TMP, repoId); + + return fs.exists(repoPath) + .then(function(doExists) { + if (doExists) return; + + // Clone repo + return exec("git clone "+host+" "+repoPath) + .then(function() { + return exec("git checkout "+ref, { cwd: repoPath }); + }) + }) + .thenResolve(repoPath); + }); } // Get file from a git repo function resolveFileFromGit(giturl) { - if (_.isString(giturl)) giturl = parseGitUrl(giturl); - if (!giturl) return Q(null); + if (_.isString(giturl)) giturl = parseGitUrl(giturl); + if (!giturl) return Q(null); - // Clone or get from cache - return cloneGitRepo(giturl.host, giturl.ref) - .then(function(repo) { + // Clone or get from cache + return cloneGitRepo(giturl.host, giturl.ref) + .then(function(repo) { - // Resolve relative path - return path.resolve(repo, giturl.filepath); - }); + // Resolve relative path + return path.resolve(repo, giturl.filepath); + }); }; module.exports = { - checkUrl: checkGitUrl, - parseUrl: parseGitUrl, - resolveFile: resolveFileFromGit + checkUrl: checkGitUrl, + parseUrl: parseGitUrl, + resolveFile: resolveFileFromGit }; diff --git a/lib/utils/i18n.js b/lib/utils/i18n.js index d7560bd..449fccb 100644 --- a/lib/utils/i18n.js +++ b/lib/utils/i18n.js @@ -5,14 +5,14 @@ var fs = require("fs"); var I18N_PATH = path.resolve(__dirname, "../../theme/i18n/") var getLocales = _.memoize(function() { - var locales = fs.readdirSync(I18N_PATH); - return _.chain(locales) + var locales = fs.readdirSync(I18N_PATH); + return _.chain(locales) .map(function(local) { local = path.basename(local, ".json"); return [local, _.extend({ - direction: "ltr" + direction: "ltr" }, require(path.join(I18N_PATH, local)), { - id: local + id: local })]; }) .object() @@ -20,53 +20,53 @@ var getLocales = _.memoize(function() { }); var getLanguages = function() { - return _.keys(getLocales()); + return _.keys(getLocales()); }; var getByLanguage = function(lang) { - lang = normalizeLanguage(lang); - var locales = getLocales(); - return locales[lang]; + lang = normalizeLanguage(lang); + var locales = getLocales(); + return locales[lang]; }; var compareLocales = function(lang, locale) { - var langMain = _.first(lang.split("-")); - var langSecond = _.last(lang.split("-")); + var langMain = _.first(lang.split("-")); + var langSecond = _.last(lang.split("-")); - var localeMain = _.first(locale.split("-")); - var localeSecond = _.last(locale.split("-")); + var localeMain = _.first(locale.split("-")); + var localeSecond = _.last(locale.split("-")); - if (locale == lang) return 100; - if (localeMain == langMain) return 50; - if (localeSecond == langSecond) return 20; - return 0; + if (locale == lang) return 100; + if (localeMain == langMain) return 50; + if (localeSecond == langSecond) return 20; + return 0; }; var normalizeLanguage = _.memoize(function(lang) { - var locales = getLocales(); - var language = _.chain(locales) - .values() - .map(function(locale) { - locale = locale.id; + var locales = getLocales(); + var language = _.chain(locales) + .values() + .map(function(locale) { + locale = locale.id; - return { - locale: locale, - score: compareLocales(lang, locale) - } - }) - .filter(function(lang) { - return lang.score > 0; - }) - .sortBy("score") - .pluck("locale") - .last() - .value(); - return language || lang; + return { + locale: locale, + score: compareLocales(lang, locale) + } + }) + .filter(function(lang) { + return lang.score > 0; + }) + .sortBy("score") + .pluck("locale") + .last() + .value(); + return language || lang; }); module.exports = { - getLocales: getLocales, - getLanguages: getLanguages, - getByLanguage: getByLanguage, - normalizeLanguage: normalizeLanguage + getLocales: getLocales, + getLanguages: getLanguages, + getByLanguage: getByLanguage, + normalizeLanguage: normalizeLanguage }; diff --git a/lib/utils/images.js b/lib/utils/images.js index c5a8a11..3bc650a 100644 --- a/lib/utils/images.js +++ b/lib/utils/images.js @@ -7,14 +7,14 @@ var links = require("./links"); // Convert a svg file var convertSVG = function(source, dest, options) { - if (!fs.existsSync(source)) return Q.reject(new Error("File doesn't exist: "+source)); - var d = Q.defer(); + if (!fs.existsSync(source)) return Q.reject(new Error("File doesn't exist: "+source)); + var d = Q.defer(); - options = _.defaults(options || {}, { + options = _.defaults(options || {}, { - }); + }); - //var command = shellescape(['svgexport', source, dest]); + //var command = shellescape(['svgexport', source, dest]); var child = spawn('svgexport', [source, dest]); child.on("error", function(error) { @@ -30,10 +30,10 @@ var convertSVG = function(source, dest, options) { } }); - return d.promise; + return d.promise; }; module.exports = { - convertSVG: convertSVG, - INVALID: [".svg"] + convertSVG: convertSVG, + INVALID: [".svg"] }; diff --git a/lib/utils/logger.js b/lib/utils/logger.js index 4c6af79..5c1da8c 100644 --- a/lib/utils/logger.js +++ b/lib/utils/logger.js @@ -3,100 +3,100 @@ var util = require('util'); var color = require('bash-color'); var LEVELS = { - DEBUG: 0, - INFO: 1, - WARN: 2, - ERROR: 3, - DISABLED: 10 + DEBUG: 0, + INFO: 1, + WARN: 2, + ERROR: 3, + DISABLED: 10 }; var COLORS = { - DEBUG: color.purple, - INFO: color.cyan, - WARN: color.yellow, - ERROR: color.red + DEBUG: color.purple, + INFO: color.cyan, + WARN: color.yellow, + ERROR: color.red }; module.exports = function(_write, logLevel) { - var logger = {}; - var lastChar = '\n'; - if (_.isString(logLevel)) logLevel = LEVELS[logLevel.toUpperCase()]; - - // Write a simple message - logger.write = function(msg) { - msg = msg.toString(); - lastChar = _.last(msg); - return _write(msg); - }; - - // Format a message - logger.format = function() { - return util.format.apply(util, arguments); - }; - - // Write a line - logger.writeLn = function(msg) { - return this.write((msg || "")+"\n"); - }; - - // Write a message with a certain level - logger.log = function(level) { - if (level < logLevel) return; - - var levelKey = _.findKey(LEVELS, function(v) { return v == level; }); - var args = Array.prototype.slice.apply(arguments, [1]); - var msg = logger.format.apply(logger, args); - - if (lastChar == '\n') { - msg = COLORS[levelKey](levelKey.toLowerCase()+":")+" "+msg; - } - - return logger.write(msg); - }; - logger.logLn = function() { - if (lastChar != '\n') logger.write("\n"); - - var args = Array.prototype.slice.apply(arguments); - args.push("\n"); - logger.log.apply(logger, args); - }; - - // Write a OK - logger.ok = function(level) { - var args = Array.prototype.slice.apply(arguments, [1]); - var msg = logger.format.apply(logger, args); - if (arguments.length > 1) { - logger.logLn(level, color.green('>> ') + msg.trim().replace(/\n/g, color.green('\n>> '))); - } else { - logger.log(level, color.green("OK"), "\n"); - } - }; - - // Write an "FAIL" - logger.fail = function(level) { - return logger.log(level, color.red("ERROR")+"\n"); - }; - - _.each(_.omit(LEVELS, 'DISABLED'), function(level, levelKey) { - levelKey = levelKey.toLowerCase(); - - logger[levelKey] = _.partial(logger.log, level); - logger[levelKey].ln = _.partial(logger.logLn, level); - logger[levelKey].ok = _.partial(logger.ok, level); - logger[levelKey].fail = _.partial(logger.fail, level); - logger[levelKey].promise = function(p) { - return p. - then(function(st) { - logger[levelKey].ok(); - return st; - }, function(err) { - logger[levelKey].fail(); - throw err; - }); - } - }); - - return logger; + var logger = {}; + var lastChar = '\n'; + if (_.isString(logLevel)) logLevel = LEVELS[logLevel.toUpperCase()]; + + // Write a simple message + logger.write = function(msg) { + msg = msg.toString(); + lastChar = _.last(msg); + return _write(msg); + }; + + // Format a message + logger.format = function() { + return util.format.apply(util, arguments); + }; + + // Write a line + logger.writeLn = function(msg) { + return this.write((msg || "")+"\n"); + }; + + // Write a message with a certain level + logger.log = function(level) { + if (level < logLevel) return; + + var levelKey = _.findKey(LEVELS, function(v) { return v == level; }); + var args = Array.prototype.slice.apply(arguments, [1]); + var msg = logger.format.apply(logger, args); + + if (lastChar == '\n') { + msg = COLORS[levelKey](levelKey.toLowerCase()+":")+" "+msg; + } + + return logger.write(msg); + }; + logger.logLn = function() { + if (lastChar != '\n') logger.write("\n"); + + var args = Array.prototype.slice.apply(arguments); + args.push("\n"); + logger.log.apply(logger, args); + }; + + // Write a OK + logger.ok = function(level) { + var args = Array.prototype.slice.apply(arguments, [1]); + var msg = logger.format.apply(logger, args); + if (arguments.length > 1) { + logger.logLn(level, color.green('>> ') + msg.trim().replace(/\n/g, color.green('\n>> '))); + } else { + logger.log(level, color.green("OK"), "\n"); + } + }; + + // Write an "FAIL" + logger.fail = function(level) { + return logger.log(level, color.red("ERROR")+"\n"); + }; + + _.each(_.omit(LEVELS, 'DISABLED'), function(level, levelKey) { + levelKey = levelKey.toLowerCase(); + + logger[levelKey] = _.partial(logger.log, level); + logger[levelKey].ln = _.partial(logger.logLn, level); + logger[levelKey].ok = _.partial(logger.ok, level); + logger[levelKey].fail = _.partial(logger.fail, level); + logger[levelKey].promise = function(p) { + return p. + then(function(st) { + logger[levelKey].ok(); + return st; + }, function(err) { + logger[levelKey].fail(); + throw err; + }); + } + }); + + return logger; }; module.exports.LEVELS = LEVELS; module.exports.COLORS = COLORS; |