Skip to content

Commit

Permalink
v0.0.3
Browse files Browse the repository at this point in the history
- Improved doc
- Expose resolvers automatically
  • Loading branch information
cgadam committed Aug 1, 2016
1 parent 2849cb6 commit b91058b
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 27 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# Dependency Analyzer
After coding enough in a node.js project you might end up asking yourself: *are all the **dependencies** defined in my **package.json** actually being **used in my project***?
After coding enough in a node.js project you might end up asking yourself: _are all the **dependencies** defined in my **package.json** actually being **used in my project**_?

The truth is that *we always add dependencies* (otherwise the code would not work), but, *do we always **remove** them when they're not needed anymore?* If you're
The truth is that *we always add dependencies* (otherwise the code would not work), but, _do we always **remove** them when they're not needed anymore?_ If you're
disciplined enough you might do it but you can't be accountable for the other *N developers* working in your same project!. The thing is that there's nothing preventing us
from having a dependency defined and not being used: meaning, **the code will still work**.

The other way around, although it's less likely to happen, is also valid. *Do you have all the **npm dependencies** you need to have declared in your **package.json**?*
The other way around, although it's less likely to happen, is also valid. _Do you have all the **npm dependencies** you need to have declared in your **package.json**?_
The answer will come to light during the execution of your program. Well, **as long as the code is executed**, right?

This dependency analyzer *is aimed to check consistency between the **"definition of dependencies"** vs **"their real usage in the source code"** and provide useful
information for you to take further actions*. It's basically a static analysis.
This dependency analyzer _is aimed to check consistency between the **"definition of dependencies"** vs **"their real usage in the source code"** and provide useful
information for you to take further actions_. It's basically a static analysis.

## How it works
There are 3 essential things to consider for the analysis:
Expand Down Expand Up @@ -115,6 +115,7 @@ If you run this:

You'll get this result:

```javascript
[
{
"names_": [
Expand Down Expand Up @@ -192,10 +193,12 @@ You'll get this result:
"isCore_": true
}
]
```

Let's analyze each field one by one:
(Let's take this one as an example)

```javascript
{
"names_": [
"node-core-module-names"
Expand All @@ -207,6 +210,7 @@ You'll get this result:
"requiredNumber_": 1,
"isCore_": false
}
```

* __Names__: you might find the same dependency being called in different ways in the code (each way is probably understood by different resolvers) . As long as the dependency is solved (no matter which resolver solved it) to the same resolved value then we're always talking about the same dependency. The different "names" used to reference this dependency are added to this array.

Expand Down
28 changes: 12 additions & 16 deletions bin/program.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var program = require('commander'),
program.allowUnknownOption();

var DependencyAnalyzer = require('..').DependencyAnalyzer,
BaseResolver = require('..').BaseResolver;
BaseResolver = require('..').Resolvers.BaseResolver;

var version = '0.0.1';

Expand Down Expand Up @@ -120,28 +120,24 @@ try {
});

if (!program.resolvers) {
/* REMEMBER THAT RESOLVERS ORDER MATTERS */
resolvers = [
path.resolve(__dirname, '../lib/resolvers/path-resolver.js'),
path.resolve(__dirname, '../lib/resolvers/node-resolver.js')
];
new (require('..').Resolvers.PathResolver)(program),
new (require('..').Resolvers.NodeResolver)(program),
];
} else {
resolvers = getFilesList(program.resolvers, {
errorMsg: 'Resolvers list should be a list of paths to the resolver files: ' +
'./resolverA.js,../folder/resolverB.js'
}).map(function (resolverPath) {
var loadPath = path.resolve(path.join(path.dirname(resolverPath), path.basename(resolverPath, path.extname(resolverPath)))),
ResolverModule = require(loadPath);
/*
THE PROGRAM ITSELF IS PASSED TO THE RESOLVER SO THAT EACH RESOLVER
CAN READ CUSTOM OPTIONS
*/
return new ResolverModule(program);
});
}

resolvers = resolvers.map(function (resolverPath) {
var loadPath = path.resolve(path.join(path.dirname(resolverPath), path.basename(resolverPath, path.extname(resolverPath)))),
ResolverModule = require(loadPath);
/*
THE PROGRAM ITSELF IS PASSED TO THE RESOLVER SO THAT EACH RESOLVER
CAN READ CUSTOM OPTIONS
*/
return new ResolverModule(program);
});

} catch (err) {
console.log('\nDependencies Analyzer v' + version);
console.log('****************************');
Expand Down
60 changes: 56 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,59 @@
/**
Here we export the public API
/*
@license
The MIT License (MIT)
Copyright (c) 2016 Christian Adam
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

/*
@author Christian Adam
*/

/* jshint node:true */

var fs = require('fs'),
path = require('path');

/**
* Automatically loads all the .js files under ./lib/resolvers folder
* as registered resolvers. They are exported in the exports object under
* "Resolvers" attr.
* @returns {object} The registered resolvers object.
*/
function loadResolvers() {
var Resolvers = {};
Resolvers.BaseResolver = require('./lib/resolvers/base/base-resolver');
var files = fs.readdirSync('./lib/resolvers');
for (var i = 0, len = files.length; i < len; i++) {
var file = files[i];
if (path.extname(file) === '.js') {
var resolver = require('./lib/resolvers/' + file);
Resolvers[resolver.name] = resolver;
}
}
return Resolvers;
}

module.exports = {
DependencyAnalyzer: require('./lib/dependencies-analyzer.js'),
BaseResolver: require('./lib/resolvers/base/base-resolver')
}
Resolvers: loadResolvers()
};
5 changes: 4 additions & 1 deletion lib/resolvers/node-resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ var BaseResolver = require('./base/base-resolver'),

/**
* Knows how to resolve strings that match to node modules.
* @param {object} opts Options for initialization
* @param {object} opts Options for initialization
* @param {string} opts.package The package.json file where node dependencies are defined
* @param {boolean} [opts.devDependencies=false] A flag telling if devDependencies should be taken into
* account for the analysis.
*/
function NodeResolver(opts) {
BaseResolver.call(this, opts);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dependencies-analyzer",
"version": "0.0.2",
"version": "0.0.3",
"description": "A module for checking node dependencies transitively",
"main": "index.js",
"repository": {
Expand Down

0 comments on commit b91058b

Please sign in to comment.