Skip to content

Commit

Permalink
Replace lodash-dependency by smaller functions
Browse files Browse the repository at this point in the history
  • Loading branch information
nknapp committed Mar 24, 2017
1 parent ceeb304 commit f6a19c6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 21 deletions.
39 changes: 23 additions & 16 deletions handlebars/helpers.js
@@ -1,6 +1,12 @@
const path = require('path')
const cp = require('child_process')
const _ = require('lodash')
const _ = {
escapeRegExp: require('lodash.escaperegexp'),
cloneDeep: require('lodash.clonedeep'),
isPlainObject: require('lodash.isplainobject'),
groupBy: require('lodash.groupby'),
map: require('lodash.map')
}
const debug = require('debug')('thought:helpers')
const Promise = require('bluebird')
const glob = Promise.promisify(require('glob'))
Expand Down Expand Up @@ -51,7 +57,7 @@ function json (obj) {
function include (filename, language) {
return qfs.read(filename).then(function (contents) {
return '```' +
(_.isString(language) ? language : path.extname(filename).substr(1)) +
(typeof language === 'string' ? language : path.extname(filename).substr(1)) +
'\n' +
contents +
'\n```\n'
Expand Down Expand Up @@ -143,7 +149,7 @@ function exec (command, options) {
start = end = '`'
break
default:
const fenceLanguage = _.isString(lang) ? lang : ''
const fenceLanguage = lang || ''
start = '```' + fenceLanguage + '\n'
end = '\n```'
}
Expand All @@ -165,7 +171,7 @@ function exec (command, options) {
*/
function dirTree (baseDir, globPattern, options) {
// Is basedir is not a string, it is probably the handlebars "options" object
if (!_.isString(globPattern)) {
if (typeof globPattern !== 'string') {
globPattern = '**'
}

Expand Down Expand Up @@ -413,20 +419,21 @@ function treeFromPathComponents (files, label) {
if (files.length === 0) {
return label
}

var byFirstPathComponent = _.groupBy(files, '0')

const result = {
label: label,
nodes: _(files)
.groupBy('0')
.map(function (group, key) {
const values = group
.map(function (item) {
return item.slice(1)
})
.filter(function (item) {
return item.length > 0
})
return treeFromPathComponents(values, key)
}).value()
nodes: _.map(byFirstPathComponent, function (group, key) {
const values = group
.map(function (item) {
return item.slice(1)
})
.filter(function (item) {
return item.length > 0
})
return treeFromPathComponents(values, key)
})
}

// Condense path if directory only has one entry
Expand Down
13 changes: 10 additions & 3 deletions lib/utils/exeq.js
Expand Up @@ -5,10 +5,17 @@
* Released under the MIT license.
*/
var cp = require('child_process')
var Q = require('q')
var _ = require('lodash')

/**
* Call child_process.execFile with `{ encoding: utf-8 }` and return a promise of `[ stdout, stderr]`
*/
module.exports = _.partial(Q.denodeify(cp.execFile), _, _, {encoding: 'utf-8'})
module.exports = function (cmd, args) {
return new Promise(function (resolve, reject) {
return cp.execFile(cmd, args, function (err, stdout, stderr) {
if (err) {
return reject(err)
}
return resolve([stdout, stderr])
})
})
}
8 changes: 6 additions & 2 deletions package.json
Expand Up @@ -35,13 +35,17 @@
"bluebird": "^3.5.0",
"cheerio": "^0.22.0",
"commander": "^2.8.1",
"customize": "^1.0.0",
"customize": "^2.0.0-alpha1",
"customize-write-files": "^1.1.0",
"debug": "^2.2.0",
"find-package": "^1.0.0",
"glob": "^7.0.5",
"handlebars": "^4.0.5",
"lodash": "^4.13.1",
"lodash.clonedeep": "^4.5.0",
"lodash.escaperegexp": "^4.1.2",
"lodash.groupby": "^4.6.0",
"lodash.isplainobject": "^4.0.6",
"lodash.map": "^4.6.0",
"m-io": "^0.5.0",
"request": "^2.79.0",
"request-promise": "^4.1.1",
Expand Down

0 comments on commit f6a19c6

Please sign in to comment.