Skip to content
This repository was archived by the owner on Mar 13, 2024. It is now read-only.

Commit da6c139

Browse files
committedJul 28, 2019
Add resolving of package.json directly from files
Making it enough to specify which files to check the dependencies of
1 parent 011184c commit da6c139

File tree

4 files changed

+77
-33
lines changed

4 files changed

+77
-33
lines changed
 

‎cli.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,16 @@ if (args.version) {
3131
}
3232

3333
if (args.help || args._.length === 0) {
34-
console.log('\nUsage: dependency-check <path to package.json or module folder> <additional entries to add> <options>')
34+
console.log('\nUsage: dependency-check <path to module files, package.json or module folder> <additional entries to add> <options>')
3535

3636
console.log('\nOptions:')
3737
console.log('--missing (default) Check to make sure that all modules in your code are listed in your package.json')
3838
console.log('--unused, --extra The inverse of the --missing check and will tell you which modules in your package.json *were not* used in your code')
3939
console.log("--no-dev Won't tell you about devDependencies that are missing or unused")
4040
console.log("--no-peer Won't tell you about peerDependencies that are missing or unused")
4141
console.log("--ignore-module, -i Won't tell you about these module names when missing or unused. Supports globbing")
42-
console.log('--entry By default your main and bin entries from package.json will be parsed, but you can add more the list of entries by passing them in as --entry')
43-
console.log("--no-default-entries Won't parse your main and bin entries from package.json")
42+
console.log('--entry If a package.json or module folder was set, then by default the main and bin entries in the package.json will be parsed, but you can add more the list of entries by passing them in as --entry. Supports globbing')
43+
console.log("--no-default-entries Won't parse your main and bin entries from package.json even when a package.json or module folder has been defined")
4444
console.log('--detective Requireable path containing an alternative implementation of the detective module that supports alternate syntaxes')
4545
console.log("--extensions, -e List of file extensions with detective to use when resolving require paths. Eg. 'js,jsx:detective-es6'")
4646
console.log('--version Show current version')

‎index.js

+52-21
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const debug = require('debug')('dependency-check')
99
const isRelative = require('is-relative')
1010
const globby = require('globby')
1111
const micromatch = require('micromatch')
12+
const pkgUp = require('pkg-up')
1213

1314
const promisedReadPackage = function (pkgPath) {
1415
return new Promise((resolve, reject) => {
@@ -19,20 +20,62 @@ const promisedReadPackage = function (pkgPath) {
1920
})
2021
}
2122

23+
const resolveGlobbedPath = function (entries, cwd) {
24+
const paths = []
25+
26+
if (typeof entries === 'string') entries = [entries]
27+
28+
debug('globby resolving', entries)
29+
30+
globby.sync(entries, {
31+
cwd,
32+
absolute: true,
33+
expandDirectories: false
34+
}).forEach(entry => {
35+
// Globby yields unix-style paths.
36+
const normalized = path.resolve(entry)
37+
38+
if (paths.indexOf(normalized) === -1) {
39+
paths.push(normalized)
40+
}
41+
})
42+
43+
debug('globby resolved', paths)
44+
45+
return paths
46+
}
47+
2248
module.exports = function (opts, cb) {
2349
let pkgPath = opts.path
50+
let entries
51+
2452
const result = promisedReadPackage(pkgPath)
2553
.catch(err => {
26-
if (err && err.code === 'EISDIR') {
54+
if (!err) {
55+
return Promise.reject(new Error('Failed to read package.json, but received no error'))
56+
} else if (pkgPath.endsWith('/package.json') || pkgPath === 'package.json') {
57+
return Promise.reject(new Error('Failed to read package.json: ' + err.message))
58+
} else if (err.code === 'EISDIR') {
2759
pkgPath = path.join(pkgPath, 'package.json')
2860
return promisedReadPackage(pkgPath)
2961
}
30-
return Promise.reject(err)
62+
63+
// We've likely been given entries rather than a package.json or module path, try resolving that instead
64+
entries = resolveGlobbedPath(pkgPath)
65+
66+
if (!entries[0]) {
67+
return Promise.reject(new Error('Failed to find package.json, could not find any matching files'))
68+
}
69+
70+
opts.noDefaultEntries = true
71+
pkgPath = pkgUp.sync({ cwd: path.dirname(entries[0]) })
72+
73+
return promisedReadPackage(pkgPath)
3174
})
3275
.then(pkg => parse({
3376
path: pkgPath,
3477
package: pkg,
35-
entries: opts.entries,
78+
entries: (entries || []).concat(opts.entries),
3679
noDefaultEntries: opts.noDefaultEntries,
3780
builtins: opts.builtins,
3881
extensions: getExtensions(opts.extensions, opts.detective)
@@ -146,10 +189,12 @@ function parse (opts) {
146189
const extensions = opts.extensions
147190

148191
const deps = {}
149-
const paths = []
150192
const seen = []
151193
const core = []
152194
const mainPath = path.resolve(pkg.main || path.join(path.dirname(pkgPath), 'index.js'))
195+
196+
let paths = []
197+
153198
if (!opts.noDefaultEntries && fs.existsSync(mainPath)) paths.push(mainPath)
154199

155200
if (!opts.noDefaultEntries && pkg.bin) {
@@ -165,23 +210,9 @@ function parse (opts) {
165210

166211
// pass in custom additional entries e.g. ['./test.js']
167212
if (opts.entries) {
168-
if (typeof opts.entries === 'string') opts.entries = [opts.entries]
169-
170-
debug('globby resolving', opts.entries)
171-
172-
globby.sync(opts.entries, {
173-
cwd: path.dirname(pkgPath),
174-
absolute: true,
175-
expandDirectories: false
176-
}).forEach(entry => {
177-
// Globby yields unix-style paths.
178-
const normalized = path.resolve(entry)
179-
180-
if (paths.indexOf(normalized) === -1) {
181-
debug('globby resolved', normalized)
182-
paths.push(normalized)
183-
}
184-
})
213+
paths = paths.concat(
214+
resolveGlobbedPath(opts.entries, path.dirname(pkgPath))
215+
)
185216
}
186217

187218
debug('entry paths', paths)

‎package.json

+5-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
"check:dependencies": "node cli.js . && node cli.js . --missing --extra --no-dev -e js:detective-cjs",
1111
"check:node-versions": "installed-check --engine-check --no-version-check",
1212
"lint": "standard",
13-
"test-cli-glob": "node cli.js test/ --entry '**/*.js' --no-default-entries",
14-
"test-cli-simple": "node cli.js test/",
15-
"test-cli": "npm-run-all --parallel test-cli-glob test-cli-simple",
13+
"test-cli:main-as-file": "node cli.js test/index.js",
14+
"test-cli:glob": "node cli.js test/ --entry '**/*.js' --no-default-entries",
15+
"test-cli:simple": "node cli.js test/",
16+
"test-cli": "npm-run-all --parallel test-cli:*",
1617
"test": "npm-run-all lint test-cli check:*"
1718
},
1819
"engines": {
@@ -27,6 +28,7 @@
2728
"is-relative": "^1.0.0",
2829
"micromatch": "^4.0.2",
2930
"minimist": "^1.2.0",
31+
"pkg-up": "^3.1.0",
3032
"read-package-json": "^2.0.10",
3133
"resolve": "^1.1.7"
3234
},

‎readme.md

+17-6
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ dependency-check `2.x` supports Node.js 0.10 and later (Dev note: published usin
1717

1818
## how it works
1919

20-
`dependency-check` parses your module code starting from the default entry files (e.g. `index.js` or `main` and any `bin` commands defined in package.json) and traverses through all relatively required JS files, ultimately producing a list of non-relative modules
20+
`dependency-check` parses your module code starting from the default entry files (e.g. `index.js` or `main` and any `bin` commands defined in package.json or if specific files has been defined, then those) and traverses through all relatively required JS files, ultimately producing a list of non-relative modules
2121

2222
* **relative** - e.g. `require('./a-relative-file.js')`, if one of these are encountered the required file will be recursively parsed by the `dependency-check` algorithm
2323
* **non-relative** - e.g. `require('a-module')`, if one of these are encountered it will get added to the list of dependencies, but subdependencies of the module will not get recursively parsed
@@ -28,14 +28,24 @@ the goal of this module is to simply check that all non-relative modules that ge
2828

2929
```
3030
$ npm install dependency-check -g
31-
$ dependency-check <package.json file or module folder path>
31+
$ dependency-check <path to module file(s), package.json or module folder>
3232
3333
# e.g.
3434
3535
$ dependency-check ./package.json
3636
Success! All dependencies used in the code are listed in package.json
3737
$ dependency-check ./package.json --unused
3838
Success! All dependencies in package.json are used in the code
39+
40+
# or with file input instead:
41+
42+
$ dependency-check ./index.js
43+
Success! All dependencies used in the code are listed in package.json
44+
45+
# even with globs and multiple inputs:
46+
47+
$ dependency-check ./test/**/*.js ./lib/*.js
48+
Success! All dependencies used in the code are listed in package.json
3949
```
4050

4151
`dependency-check` exits with code 1 if there are discrepancies, in addition to printing them out
@@ -64,23 +74,23 @@ ignores a module. This works for both `--unused` and `--missing`. You can specif
6474

6575
### --entry
6676

67-
by default your `main` and `bin` entries from package.json will be parsed, but you can add more the list of entries by passing them in as `--entry`, e.g.:
77+
adds more files to be checked to any of the default ones already added, like `tests.js` to the default ones resolved from package.json:
6878

6979
```
7080
dependency-check package.json --entry tests.js
7181
```
7282

73-
in the above example `tests.js` will get added to the entries that get parsed + checked in addition to the defaults. You can specify as many separate `--entry` arguments as you want
83+
you can specify as many separate `--entry` arguments as you want. `--entry` also supports globbing like `**/*.js` and similar.
7484

75-
you can also instead add additional entries directly after your package definition, like:
85+
you can also instead add additional entries directly after your main path, like:
7686

7787
```
7888
dependency-check package.json tests.js
7989
```
8090

8191
### --no-default-entries
8292

83-
running `dependency-check package.json --no-default-entries --entry tests.js` won't parse any entries other than `tests.js`. None of the entries from your package.json `main` and `bin` will be parsed
93+
running eg. `dependency-check package.json --no-default-entries --entry tests.js` won't add any default entries despite the main path given being one to a package.json or module folder. So only the `tests.js` file will be checked
8494

8595
### --extensions, -e
8696

@@ -126,3 +136,4 @@ See [grunt-dependency-check](https://github.com/sindresorhus/grunt-dependency-ch
126136

127137
- [detective](https://www.npmjs.org/package/detective) is used for parsing `require()` statements, which means it only does **static requires**. this means you should convert things like `var foo = "bar"; require(foo)` to be static, e.g. `require("bar")`
128138
- you can specify as many entry points as you like with multiple `--entry foo.js` arguments
139+
- use globbing to effectively add all the files you want to check

0 commit comments

Comments
 (0)
This repository has been archived.