-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
57 lines (53 loc) · 1.34 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
'use strict';
const _ = require('lodash');
const fs = require('fs');
const path = require('path');
const co = require('co');
const semver = require('semver');
const loadPkg = dir => {
let pkg;
try {
pkg = fs.readFileSync(path.join(dir, 'package.json'), 'utf-8');
} catch (e) {
if (e.message.indexOf('Cannot find module') !== -1)
throw new Error(`Can not find package.json in ${dir}`);
throw e;
}
try {
pkg = JSON.parse(pkg);
} catch (e) {
throw new Error(`Can not parse package.json in ${dir}`);
}
return pkg;
};
const check = _.curryRight((dependencies, cwd) => {
return _(dependencies)
.pairs()
.map(pair => {
const name = pair[0];
const ver = pair[1];
const dir = path.join(cwd, 'node_modules', name);
const depPkg = loadPkg(dir);
if (ver && !semver.satisfies(depPkg.version, ver))
return {
name: depPkg.name,
version: depPkg.version,
expected: ver
};
return null;
})
.filter(v => !!v)
.value();
});
module.exports = co.wrap(function*(cwd) {
cwd = cwd || process.cwd();
const pkg = loadPkg(cwd);
let rst = [];
const checker = check(cwd);
rst = rst.concat(checker(pkg.dependencies || {}));
rst = rst.concat(checker(pkg.devDependencies || {}));
return {
success: rst.length === 0,
detail: rst
};
});