summaryrefslogtreecommitdiffstats
path: root/lib/utils
diff options
context:
space:
mode:
authorSamy Pesse <samypesse@gmail.com>2016-04-30 23:07:43 +0200
committerSamy Pesse <samypesse@gmail.com>2016-04-30 23:07:43 +0200
commitc681cd286a746d5cf3f69fc800bcb79a42e70973 (patch)
tree66b852e90ebfe38da444532af729f7a5af0c6e5d /lib/utils
parentc1d53ec11fbe085932df911bda5686b7bf671f53 (diff)
downloadgitbook-c681cd286a746d5cf3f69fc800bcb79a42e70973.zip
gitbook-c681cd286a746d5cf3f69fc800bcb79a42e70973.tar.gz
gitbook-c681cd286a746d5cf3f69fc800bcb79a42e70973.tar.bz2
Remove lodash dependency
Diffstat (limited to 'lib/utils')
-rw-r--r--lib/utils/command.js26
-rw-r--r--lib/utils/git.js16
-rw-r--r--lib/utils/logger.js43
-rw-r--r--lib/utils/path.js10
4 files changed, 54 insertions, 41 deletions
diff --git a/lib/utils/command.js b/lib/utils/command.js
index 93df750..083e72e 100644
--- a/lib/utils/command.js
+++ b/lib/utils/command.js
@@ -1,4 +1,3 @@
-var _ = require('lodash');
var childProcess = require('child_process');
var spawn = require('spawn-cmd').spawn;
var Promise = require('./promise');
@@ -62,15 +61,22 @@ function escapeShellArg(s) {
}
function optionsToShellArgs(options) {
- return _.chain(options)
- .map(function(value, key) {
- if (value === null || value === undefined || value === false) return null;
- if (value === true) return key;
- return key + '=' + escapeShellArg(value);
- })
- .compact()
- .value()
- .join(' ');
+ var result = [];
+
+ for (var key in options) {
+ var value = options[key];
+
+ if (value === null || value === undefined || value === false) {
+ continue;
+ }
+ if (value === true) {
+ result.push(key);
+ }
+
+ result.push(key + '=' + escapeShellArg(value));
+ }
+
+ return result.join(' ');
}
module.exports = {
diff --git a/lib/utils/git.js b/lib/utils/git.js
index 52b1096..6884b83 100644
--- a/lib/utils/git.js
+++ b/lib/utils/git.js
@@ -1,4 +1,4 @@
-var _ = require('lodash');
+var is = require('is');
var path = require('path');
var crc = require('crc');
var URI = require('urijs');
@@ -69,7 +69,7 @@ Git.prototype.resolve = function(giturl) {
if (this.resolveRoot(giturl)) return Promise(giturl);
return Promise(null);
}
- if (_.isString(giturl)) giturl = Git.parseUrl(giturl);
+ if (is.string(giturl)) giturl = Git.parseUrl(giturl);
if (!giturl) return Promise(null);
// Clone or get from cache
@@ -88,8 +88,10 @@ Git.prototype.resolveRoot = function(filepath) {
// Extract first directory (is the repo id)
relativeToGit = path.relative(this.tmpDir, filepath);
- repoId = _.first(relativeToGit.split(path.sep));
- if (!repoId) return;
+ repoId = relativeToGit.split(path.sep)[0];
+ if (!repoId) {
+ return;
+ }
// Return an absolute file
return path.resolve(this.tmpDir, repoId);
@@ -114,10 +116,12 @@ Git.parseUrl = function(giturl) {
// 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);
+ if (filepath[0] == '/') {
+ filepath = filepath.slice(1);
+ }
// Recreate pathname without the real filename
- uri.path(_.first(fileParts)+'.git');
+ uri.path(fileParts[0] + '.git');
return {
host: uri.toString(),
diff --git a/lib/utils/logger.js b/lib/utils/logger.js
index fc9c394..3bde904 100644
--- a/lib/utils/logger.js
+++ b/lib/utils/logger.js
@@ -1,21 +1,22 @@
-var _ = require('lodash');
+var is = require('is');
var util = require('util');
var color = require('bash-color');
+var Immutable = require('immutable');
-var LEVELS = {
+var LEVELS = Immutable.Map({
DEBUG: 0,
INFO: 1,
WARN: 2,
ERROR: 3,
DISABLED: 10
-};
+});
-var COLORS = {
+var COLORS = Immutable.Map({
DEBUG: color.purple,
INFO: color.cyan,
WARN: color.yellow,
ERROR: color.red
-};
+});
function Logger(write, logLevel) {
if (!(this instanceof Logger)) return new Logger(write, logLevel);
@@ -29,17 +30,18 @@ function Logger(write, logLevel) {
this.setLevel(logLevel || 'info');
- _.bindAll(this);
-
// Create easy-to-use method like "logger.debug.ln('....')"
- _.each(_.omit(LEVELS, 'DISABLED'), function(level, levelKey) {
+ LEVELS.forEach(function(level, levelKey) {
+ if (levelKey === 'DISABLED') {
+ return;
+ }
levelKey = levelKey.toLowerCase();
- this[levelKey] = _.partial(this.log, level);
- this[levelKey].ln = _.partial(this.logLn, level);
- this[levelKey].ok = _.partial(this.ok, level);
- this[levelKey].fail = _.partial(this.fail, level);
- this[levelKey].promise = _.partial(this.promise, level);
+ this[levelKey] = this.log.bind(this, level);
+ this[levelKey].ln = this.logLn.bind(this, level);
+ this[levelKey].ok = this.ok.bind(this, level);
+ this[levelKey].fail = this.fail.bind(this, level);
+ this[levelKey].promise = this.promise.bind(this, level);
}, this);
}
@@ -49,7 +51,11 @@ function Logger(write, logLevel) {
@param {String} logLevel
*/
Logger.prototype.setLevel = function(logLevel) {
- if (_.isString(logLevel)) logLevel = LEVELS[logLevel.toUpperCase()];
+ if (is.string(logLevel)) {
+ logLevel = logLevel.toUpperCase();
+ logLevel = LEVELS.get(logLevel);
+ }
+
this.logLevel = logLevel;
};
@@ -60,7 +66,7 @@ Logger.prototype.setLevel = function(logLevel) {
*/
Logger.prototype.write = function(msg) {
msg = msg.toString();
- this.lastChar = _.last(msg);
+ this.lastChar = msg[msg.length - 1];
return this._write(msg);
};
@@ -88,12 +94,14 @@ Logger.prototype.writeLn = function(msg) {
Logger.prototype.log = function(level) {
if (level < this.logLevel) return;
- var levelKey = _.findKey(LEVELS, function(v) { return v == level; });
+ var levelKey = LEVELS.findKey(function(v) {
+ return v === level;
+ });
var args = Array.prototype.slice.apply(arguments, [1]);
var msg = this.format.apply(this, args);
if (this.lastChar == '\n') {
- msg = COLORS[levelKey](levelKey.toLowerCase()+':')+' '+msg;
+ msg = COLORS.get(levelKey)(levelKey.toLowerCase()+':')+' '+msg;
}
return this.write(msg);
@@ -151,6 +159,5 @@ Logger.prototype.promise = function(level, p) {
};
Logger.LEVELS = LEVELS;
-Logger.COLORS = COLORS;
module.exports = Logger;
diff --git a/lib/utils/path.js b/lib/utils/path.js
index a4968c8..9daa8e6 100644
--- a/lib/utils/path.js
+++ b/lib/utils/path.js
@@ -1,6 +1,4 @@
-var _ = require('lodash');
var path = require('path');
-
var error = require('./error');
// Normalize a filename
@@ -18,17 +16,15 @@ function isInRoot(root, filename) {
// Throw error if file is outside this folder
function resolveInRoot(root) {
var input, result;
+ var args = Array.prototype.slice.call(arguments, 1);
- input = _.chain(arguments)
- .toArray()
- .slice(1)
+ input = args
.reduce(function(current, p) {
// Handle path relative to book root ("/README.md")
if (p[0] == '/' || p[0] == '\\') return p.slice(1);
return current? path.join(current, p) : path.normalize(p);
- }, '')
- .value();
+ }, '');
result = path.resolve(root, input);