This repository has been archived by the owner on Oct 15, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
76 lines (68 loc) · 2.32 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
var path = require( 'path' );
var fs = require( 'fs' );
var log = require( 'npmlog' );
var Promise = require( 'bluebird' );
var semver = require( 'semver' );
function run ( pathToRepo ) {
pathToRepo = path.resolve( pathToRepo );
var packageJson = require( path.resolve( pathToRepo, './package.json' ) );
var dependenciesSeen = {};
if ( packageJson.devDependencies ) {
Object.keys( packageJson.devDependencies ).forEach( function ( packageName ) {
dependenciesSeen[ packageName ] = {
name: packageName,
specified: packageJson.devDependencies[ packageName ]
};
} );
}
if ( packageJson.dependencies ) {
Object.keys( packageJson.dependencies ).forEach( function ( packageName ) {
dependenciesSeen[ packageName ] = {
name: packageName,
specified: packageJson.dependencies[ packageName ]
};
} );
}
var promises = [];
Object.keys( dependenciesSeen ).forEach( function ( dependencySeen ) {
promises.push( new Promise( function ( resolve, reject ) {
var dependencyPath = path.resolve( pathToRepo, 'node_modules/' + dependencySeen );
fs.stat( dependencyPath, function ( err, stat ) {
if ( !err || err.code !== 'ENOENT' ) {
var dependencyVersion = require( dependencyPath + '/package.json' ).version;
dependenciesSeen[ dependencySeen ].actual = dependencyVersion;
} else if ( err && err.code === 'ENOENT' ) {
dependenciesSeen[ dependencySeen ].actual = undefined; // missing
} else {
reject( err );
}
resolve();
} );
} ) );
} );
return Promise.all( promises ).then( function () {
// Update the dependencies list with more detail
Object.keys( dependenciesSeen ).forEach( function ( dependencyKey ) {
var dependency = dependenciesSeen[ dependencyKey ];
if ( dependency.actual === undefined ) {
dependency.match = 'missing';
} else {
var clean = semver.clean( dependency.specified );
if ( clean === dependency.actual ) {
// Perfect match between specified version and actual version
dependency.match = 'perfect';
} else if ( semver.satisfies( dependency.actual, dependency.specified ) ) {
// Technically satisfies semver, but is using a range
dependency.match = 'match';
} else {
// Totally fails semver!
dependency.match = 'invalid';
}
}
} );
return dependenciesSeen;
} );
}
module.exports = {
run: run
};