Skip to content

Commit

Permalink
Merge 98caf4f into 8a23b65
Browse files Browse the repository at this point in the history
  • Loading branch information
s0ph1e committed Jul 21, 2017
2 parents 8a23b65 + 98caf4f commit 02da3a4
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 23 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
@@ -0,0 +1,2 @@
coverage
test/mock
6 changes: 6 additions & 0 deletions .eslintrc.js
@@ -0,0 +1,6 @@
module.exports = {
extends: '@debitoor/eslint-config-debitoor',
env: {
mocha: true
}
};
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -25,3 +25,5 @@ build/Release
# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules

.idea
5 changes: 5 additions & 0 deletions .travis.yml
@@ -0,0 +1,5 @@
language: node_js
node_js:
- 6
after_success: "npm run coveralls"
sudo: false
2 changes: 2 additions & 0 deletions README.md
@@ -1,6 +1,8 @@
# parse-dependencies
Fast and easy way to get dependencies for your code.

[![npm version](https://badge.fury.io/js/parse-dependencies.svg)](http://badge.fury.io/js/parse-dependencies) [![Build Status](https://travis-ci.org/debitoor/parse-dependencies.svg?branch=master)](https://travis-ci.org/debitoor/parse-dependencies) [![Dependency Status](https://david-dm.org/debitoor/parse-dependencies.svg)](https://david-dm.org/debitoor/parse-dependencies) [![devDependency Status](https://david-dm.org/debitoor/parse-dependencies/dev-status.svg)](https://david-dm.org/debitoor/parse-dependencies#info=devDependencies) [![Coverage Status](https://coveralls.io/repos/debitoor/parse-dependencies/badge.svg?branch=master&service=github)](https://coveralls.io/github/debitoor/parse-dependencies?branch=master)

## Install
npm instal --save parse-dependencies

Expand Down
43 changes: 23 additions & 20 deletions index.js
@@ -1,33 +1,36 @@
var fs = require('fs');
var normalize = require('path').normalize;
var async = require('async');
var builtin = process.binding('natives')
var getDirname = require('path').dirname;
const fs = require('fs');
const normalize = require('path').normalize;
const async = require('async');
const builtin = process.binding('natives');
const getDirname = require('path').dirname;

function parse(src) {
var re = /require\((['"])(.*)\1\)/g;
var deps = [];
var match;
while ((match = re.exec(src))) {
const commentRegexp = /\/\*(.*?)\*\/|\/\/(.*?)(\n|$)/g;
const dependencyRegexp = /require\((['"])(.*)\1\)/g;

src = src.replace(commentRegexp, '');
const deps = [];
let match;
while ((match = dependencyRegexp.exec(src))) {
deps.push(match[2]);
}
return deps;
}

function group(file, deps) {
var dir = getDirname(file);
var notBuildin = deps.filter(function (item) {
const dir = getDirname(file);
const notBuildin = deps.filter(function (item) {
return !(item in builtin);
});
var internal = notBuildin
const internal = notBuildin
.filter(function (item) {
return item[0] === '.';
})
.map(function (item) {
return dir + '/' + item;
})
.map(normalize);
var external = notBuildin
const external = notBuildin
.filter(function (item) {
return item[0] !== '.';
})
Expand All @@ -43,34 +46,34 @@ function group(file, deps) {
}

function find(file, cb) {
var resolved = [];
const resolved = [];
return resolve(normalize(file), cb);

function resolve(file, cb) {
var path
let path;
try {
path = require.resolve(file);
} catch (e) {
return cb(e);
};
}
if (resolved.indexOf(path) !== -1) {
return cb(null, []);
}
resolved.push(path)
resolved.push(path);

fs.readFile(path, function (err, src) {
if (err) {
return cb(err);
}

var deps = parse(src.toString());
var groups = group(path, deps);
const deps = parse(src.toString());
const groups = group(path, deps);

async.mapLimit(groups.internal, 10, resolve, function (err, result) {
if (err) {
return cb(new Error(err.message + ' at ' + file));
}
var uniqDeps = Array.prototype.concat.apply(groups.external, result)
const uniqDeps = Array.prototype.concat.apply(groups.external, result)
.filter(function (item, index, arr) {
return arr.indexOf(item) === index; // only uniq items
});
Expand Down
16 changes: 13 additions & 3 deletions package.json
@@ -1,10 +1,11 @@
{
"name": "parse-dependencies",
"version": "0.2.1",
"version": "1.0.0",
"description": "finding node code dependencies",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "istanbul cover _mocha",
"coveralls": "coveralls < ./coverage/lcov.info || echo 'problem with reporting to coveralls'"
},
"repository": {
"type": "git",
Expand All @@ -21,6 +22,15 @@
},
"homepage": "https://github.com/debitoor/find-dependencies",
"dependencies": {
"async": "^1.4.2"
"async": "2.5.0"
},
"devDependencies": {
"@debitoor/eslint-config-debitoor": "1.5.0",
"@debitoor/mocha-strict-dependencies": "1.1.0",
"chai": "4.1.0",
"coveralls": "2.13.1",
"istanbul": "0.4.5",
"mocha": "3.4.2",
"mocha-eslint": "4.0.0"
}
}
1 change: 1 addition & 0 deletions test/eslint.spec.js
@@ -0,0 +1 @@
require('mocha-eslint')(['.']);
3 changes: 3 additions & 0 deletions test/mock/internal-dependency.js
@@ -0,0 +1,3 @@
const _ = require('lodash');
const async = require('async');

12 changes: 12 additions & 0 deletions test/mock/main.js
@@ -0,0 +1,12 @@
const express = require("express");
const bodyParser = require('body-parser');
const _ = require('lodash');

// require('commented');
require('./internal-dependency');

/*
const anotherCommented = require("another-commented");
*/


25 changes: 25 additions & 0 deletions test/parse-dependencies.spec.js
@@ -0,0 +1,25 @@
const expect = require('chai').expect;
const parse = require('../index');

describe('parse-dependencies', () => {
let deps;

before((done) => {
parse(__dirname + '/mock/main.js', (err, parsedDeps) => {
deps = parsedDeps;
done();
});
});

it('should return dependencies from main file and its internal dependencies', () => {
expect(deps).to.include.members(['express', 'body-parser', 'lodash', 'async']);
});

it('should return unique dependencies', () => {
expect(deps.indexOf('lodash')).to.eql(deps.lastIndexOf('lodash'));
});

it('should ignore commented dependencies', () => {
expect(deps).to.not.include.members(['commented', 'another-commented']);
});
});
1 change: 1 addition & 0 deletions test/strict-dependencies.spec.js
@@ -0,0 +1 @@
require('@debitoor/mocha-strict-dependencies')();

0 comments on commit 02da3a4

Please sign in to comment.