taxicab
Advanced tools
Comparing version 1.0.0 to 1.1.0
182
index.js
@@ -1,64 +0,130 @@ | ||
var getDriverScripts = require('amd-driver-scripts'), | ||
var amdDriverScripts = require('amd-driver-scripts'), | ||
getModulesToBuild = require('get-modules-to-build'), | ||
getTreeAsList = require('dependency-tree').getTreeAsList, | ||
path = require('path'), | ||
q = require('q'); | ||
getTreeAsList = require('dependency-tree').getTreeAsList, | ||
path = require('path'), | ||
q = require('q'), | ||
timer = require('node-tictoc'); | ||
/** | ||
* Gets the driver script(s) that depend on the given module | ||
* @param {String} file - The path of the module whose driver script should be found | ||
* @param {String} root - The path where all JS files are | ||
* @param {Function} cb - (String[]) Executed with the driver scripts that depend on the given file | ||
* @param {String} [config] - Path to the requirejs config (to avoid recomputing all driver scripts) | ||
* @param {Object} options | ||
* @param {String} options.filename - The path of the module whose driver script should be found | ||
* @param {String} options.root - The path where all JS files are | ||
* @param {Function} options.success - (String[]) Executed with the driver scripts that depend on the given file | ||
* @param {String} [options.config] - Path to the requirejs config (to avoid recomputing all driver scripts) | ||
*/ | ||
module.exports = function(file, root, cb, config) { | ||
var drivers; | ||
module.exports.findDriver = function(options) { | ||
if (!options.filename) throw new Error('file not given'); | ||
if (!options.root) throw new Error('root location not given'); | ||
if (!options.success) throw new Error('callback not given'); | ||
if (!file) throw new Error('file not given'); | ||
if (!root) throw new Error('root location not given'); | ||
if (!cb) throw new Error('callback not given'); | ||
options.filename = path.resolve(process.cwd(), options.filename); | ||
file = path.resolve(process.cwd(), file); | ||
getDriverScripts(options) | ||
.then(function(drivers) { | ||
options.drivers = drivers; | ||
return options; | ||
}) | ||
.then(findRelatedDrivers) | ||
.done(options.success); | ||
}; | ||
if (config) { | ||
drivers = getModulesToBuild(config); | ||
drivers = resolvePaths(drivers, root); | ||
/** | ||
* Finds the driver scripts from the list that depend on the given | ||
* file (at some point in the dependency tree) | ||
* | ||
* @param {Object} options | ||
* @param {String[]} options.drivers | ||
* @param {String} options.filename | ||
* @param {String} options.root | ||
* @return {Promise} Resolves with a list of the relevant driver scripts | ||
*/ | ||
function findRelatedDrivers(options) { | ||
return q().then(function() { | ||
return options; | ||
}) | ||
.then(getTrees) | ||
.then(function(trees) { | ||
options.trees = trees; | ||
return options; | ||
}) | ||
.then(function() { | ||
var relatedDrivers = []; | ||
drivers = drivers.map(function(driver) { | ||
return driver.indexOf('.js') === -1 ? | ||
driver + '.js' : | ||
driver; | ||
options.trees.forEach(function(treeList, idx) { | ||
if (treeList.indexOf(options.filename) !== -1) { | ||
relatedDrivers.push(options.drivers[idx]); | ||
} | ||
}); | ||
findRelatedDrivers(drivers, file, root).then(cb); | ||
return relatedDrivers; | ||
}); | ||
} | ||
/** | ||
* Returns how long it takes to generate the dependency trees for each of the | ||
* modules in the given root directory | ||
* @param {Object} options | ||
* @param {String} options.root - Path where your JavaScript files exist | ||
* @param {String} options.success - Executed with {Object} containing the module name and time to tree | ||
*/ | ||
module.exports.timeToGenerateTrees = function(options) { | ||
options = options || {}; | ||
if (!options.root) throw new Error('root not given'); | ||
if (!options.success) throw new Error('success callback not given'); | ||
options.profile = true; | ||
options.table = {}; | ||
getDriverScripts(options) | ||
.then(function(drivers) { | ||
options.drivers = drivers; | ||
return options; | ||
}) | ||
.then(getTrees) | ||
.then(function() { | ||
options.success(options.table); | ||
}); | ||
}; | ||
/** | ||
* @param {Options} options | ||
* @param {Options} options.root | ||
* @param {Options} options.config | ||
* @return {Promise} (String[]) => null - Resolves with a list of driver scripts | ||
*/ | ||
function getDriverScripts(options) { | ||
var deferred = q.defer(); | ||
if (options.config) { | ||
deferred.resolve(getConfigDrivers(options.root, options.config)); | ||
} else { | ||
getDriverScripts(root, function(drivers) { | ||
// console.log('DEDUCED DRIVERS: ', drivers) | ||
findRelatedDrivers(drivers, file, root).then(cb); | ||
amdDriverScripts(options.root, function(drivers) { | ||
deferred.resolve(drivers); | ||
}); | ||
} | ||
}; | ||
return deferred.promise; | ||
} | ||
/** | ||
* Finds the driver scripts from the list that depend on the given | ||
* file (at some point in the dependency tree) | ||
* @param {String[]} drivers | ||
* @param {String} filename | ||
* Get the driver scripts from a requirejs configuration file | ||
* @param {String} root | ||
* @return {Promise} Resolves with a list of the relevant driver scripts | ||
* @param {String} configPath | ||
* @return {String[]} | ||
*/ | ||
function findRelatedDrivers(drivers, filename, root) { | ||
return getTrees(drivers, root) | ||
.then(function(results) { | ||
var relatedDrivers = []; | ||
function getConfigDrivers(root, configPath) { | ||
var drivers = getModulesToBuild(configPath); | ||
results.forEach(function(treeList, idx) { | ||
if (treeList.indexOf(filename) !== -1) { | ||
relatedDrivers.push(drivers[idx]); | ||
} | ||
}); | ||
drivers = resolvePaths(drivers, root); | ||
return relatedDrivers; | ||
}); | ||
drivers = drivers.map(function(driver) { | ||
return driver.indexOf('.js') === -1 ? | ||
driver + '.js' : | ||
driver; | ||
}); | ||
return drivers; | ||
} | ||
@@ -68,11 +134,25 @@ | ||
* Gets the dependency tree for each of the given files | ||
* @param {String[]} drivers | ||
* @return {Promise} Resolves with a list of trees for each file in the list | ||
* @param {Object} options | ||
* @param {String[]} options.drivers | ||
* @param {String} options.root | ||
* @param {Boolean} options.profile - Whether or not to print profiling information | ||
* @param {cli-table} options.table - data store for pretty output | ||
* @return {Promise} (String[]) => null Resolves with a list of trees for each file in the list | ||
*/ | ||
function getTrees(drivers, root) { | ||
return q.all(drivers.map(function(driver) { | ||
var deferred = q.defer(); | ||
function getTrees(options) { | ||
return q.all(options.drivers.map(function(driver) { | ||
var deferred = q.defer(), | ||
time; | ||
getTreeAsList(driver, root, deferred.resolve.bind(deferred)); | ||
if (options.profile) { | ||
timer.tic(); | ||
} | ||
getTreeAsList(driver, options.root, deferred.resolve.bind(deferred)); | ||
if (options.profile) { | ||
time = timer.toct(); | ||
options.table[driver.split(options.root)[1]] = (time.seconds ? time.seconds + 's ' : '') + time.ms + 'ms'; | ||
} | ||
return deferred.promise; | ||
@@ -82,2 +162,8 @@ })); | ||
/** | ||
* Resolve each driver script path against the root | ||
* @param {String[]} drivers | ||
* @param {String} root | ||
* @return {String[]} | ||
*/ | ||
function resolvePaths(drivers, root) { | ||
@@ -84,0 +170,0 @@ return drivers.map(function(name) { |
{ | ||
"name": "taxicab", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Find the driver script that has a given module in its dependency tree", | ||
"main": "index.js", | ||
"bin": { | ||
"taxicab": "bin/cli.js" | ||
"find-driver": "bin/find-driver.js", | ||
"time-to-trees": "bin/time-to-trees.js" | ||
}, | ||
@@ -33,4 +34,6 @@ "scripts": { | ||
"get-modules-to-build": "^1.0.1", | ||
"q": "^1.0.1" | ||
"node-tictoc": "^1.1.0", | ||
"q": "^1.0.1", | ||
"columnify": "^1.1.0" | ||
} | ||
} |
### TaxiCab | ||
> Find the driver script(s) that depend on the current module | ||
> A collection of helpful utilities for AMD modules | ||
`npm install taxicab` | ||
`npm install -g taxicab` | ||
### Usage | ||
Supported Commands: | ||
1. [Find all driver scripts that depend on a module](#find-driver) | ||
2. [How long does it take to compute the dependency trees about all modules in a directory](#time-to-trees) | ||
##### find-driver | ||
> Find the driver script(s) that depend on the given module | ||
```js | ||
var taxicab = require('taxicab'); | ||
var findDriver = require('taxicab').findDriver; | ||
taxicab(file, root, function(drivers) { | ||
findDriver({ | ||
file: 'file/in/question', | ||
root: 'path/to/all/js, | ||
config: 'path/to/my/requirejs/config.js', // Optional | ||
success: function(drivers) { | ||
console.log(drivers); | ||
} | ||
}); | ||
``` | ||
You may optionally supply a 4th argument that's the path to your requirejs configuration file. | ||
If supplied, [get-modules-to-build](https://github.com/mrjoelkemp/node-get-modules-to-build) is used to look up | ||
* If `config` is supplied, [get-modules-to-build](https://github.com/mrjoelkemp/node-get-modules-to-build) is used to look up | ||
the driver scripts. Otherwise, the driver scripts are computed via [amd-driver-scripts](https://github.com/mrjoelkemp/node-amd-driver-scripts). | ||
Shell usage: | ||
Usage via the shell (assumes a global install `npm install -g taxicab`) | ||
`find-driver filename root [config]` | ||
``` | ||
taxicab filename root [config] | ||
``` | ||
Prints: | ||
@@ -36,2 +46,26 @@ | ||
##### time-to-trees | ||
> Get profiling data about how long it takes to generate the dependency trees for each module in a directory | ||
```js | ||
var timeToTree = require('taxicab').timeToGenerateTrees; | ||
timeToTree({ | ||
root: 'path/to/all/js, | ||
success: function(profileData) { | ||
console.log(profileData); | ||
} | ||
}); | ||
``` | ||
Shell usage: | ||
`time-to-tree root` | ||
Prints via ([columnify](https://github.com/timoxley/columnify)): | ||
``` | ||
a.js 200ms | ||
b.js 100ms | ||
``` |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
8159
5
185
71
6
1
+ Addedcolumnify@^1.1.0
+ Addednode-tictoc@^1.1.0
+ Addedansi-regex@5.0.1(transitive)
+ Addedclone@1.0.4(transitive)
+ Addedcolumnify@1.6.0(transitive)
+ Addeddefaults@1.0.4(transitive)
+ Addednode-tictoc@1.3.0(transitive)
+ Addedstrip-ansi@6.0.1(transitive)
+ Addedwcwidth@1.0.1(transitive)