├── .editorconfig ├── .gitattributes ├── .github └── workflows │ └── main.yml ├── .gitignore ├── fixture ├── index.js └── package.json ├── gruntfile.js ├── license ├── package.json ├── readme.md └── tasks └── dependency-check.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [{package.json,*.yml}] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 14 14 | - 12 15 | - 10 16 | - 8 17 | - 6 18 | - 4 19 | - 0.12 20 | - 0.1 21 | steps: 22 | - uses: actions/checkout@v2 23 | - uses: actions/setup-node@v1 24 | with: 25 | node-version: ${{ matrix.node-version }} 26 | - run: npm install 27 | - run: npm test 28 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /fixture/index.js: -------------------------------------------------------------------------------- 1 | var chalk = require('chalk'); 2 | -------------------------------------------------------------------------------- /fixture/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "async": "*" 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /gruntfile.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | module.exports = function (grunt) { 3 | grunt.initConfig({ 4 | dependencyCheck: { 5 | files: ['fixture/index.js'], 6 | options: { 7 | package: 'fixture' 8 | } 9 | } 10 | }); 11 | 12 | grunt.loadTasks('tasks'); 13 | 14 | grunt.registerTask('default', ['dependencyCheck']); 15 | 16 | var match = false; 17 | 18 | grunt.warn = function (str) { 19 | if (/Dependencies not listed/.test(str)) { 20 | match = true; 21 | } 22 | }; 23 | 24 | process.on('exit', function () { 25 | if (!match) { 26 | process.exit(1); 27 | } 28 | }); 29 | }; 30 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) Sindre Sorhus (sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "grunt-dependency-check", 3 | "version": "1.1.0", 4 | "description": "Checks which modules you have used in your code and then makes sure they are listed as dependencies in your package.json and vice-versa", 5 | "license": "MIT", 6 | "repository": "sindresorhus/grunt-dependency-check", 7 | "author": { 8 | "name": "Sindre Sorhus", 9 | "email": "sindresorhus@gmail.com", 10 | "url": "sindresorhus.com" 11 | }, 12 | "engines": { 13 | "node": ">=0.10.0" 14 | }, 15 | "scripts": { 16 | "test": "xo && grunt" 17 | }, 18 | "files": [ 19 | "tasks" 20 | ], 21 | "keywords": [ 22 | "gruntplugin", 23 | "dependency", 24 | "dependencies", 25 | "check", 26 | "module", 27 | "modules", 28 | "package", 29 | "packages", 30 | "match", 31 | "detect" 32 | ], 33 | "dependencies": { 34 | "dependency-check": "^2.4.0" 35 | }, 36 | "devDependencies": { 37 | "grunt": "^1.0.1", 38 | "grunt-cli": "^1.2.0", 39 | "xo": "*" 40 | }, 41 | "peerDependencies": { 42 | "grunt": ">=0.4.0" 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # grunt-dependency-check 2 | 3 | > Checks which modules you have used in your code and then makes sure they are listed as dependencies in your package.json and vice-versa, using [`dependency-check`](https://github.com/maxogden/dependency-check) 4 | 5 | *Issues with the output should be reported on the `dependency-check` [issue tracker](https://github.com/maxogden/dependency-check/issues).* 6 | 7 | 8 | ## Install 9 | 10 | ``` 11 | $ npm install --save-dev grunt-dependency-check 12 | ``` 13 | 14 | 15 | ## Usage 16 | 17 | ```js 18 | require('load-grunt-tasks')(grunt); // npm install --save-dev load-grunt-tasks 19 | 20 | grunt.initConfig({ 21 | dependencyCheck: { 22 | files: ['**/*.js'], // same as --entry 23 | options: { 24 | package: '.', // module folder path 25 | missing: true, // same as --missing 26 | unused: true, // same as --unused 27 | excludeUnusedDev: false, // same as --no-dev 28 | ignoreUnused: [], // same as --ignore-module 29 | noDefaultEntries: true // same as --no-default-entries 30 | } 31 | } 32 | }); 33 | 34 | grunt.registerTask('default', ['dependencyCheck']); 35 | ``` 36 | 37 | 38 | ## Options 39 | 40 | See the `dependency-check` [docs](https://github.com/maxogden/dependency-check). 41 | 42 | In addition you can supply a `package` option. 43 | 44 | 45 | ## License 46 | 47 | MIT © [Sindre Sorhus](https://sindresorhus.com) 48 | -------------------------------------------------------------------------------- /tasks/dependency-check.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | var path = require('path'); 3 | var dependencyCheck = require('dependency-check'); 4 | 5 | module.exports = function (grunt) { 6 | grunt.registerMultiTask('dependencyCheck', 'Ensure used dependencies matches with listed ones', function () { 7 | var done = this.async(); 8 | var opts = this.options({ 9 | package: '.', 10 | missing: true, 11 | unused: true, 12 | excludeUnusedDev: false, 13 | ignoreUnused: [], 14 | noDefaultEntries: true 15 | }); 16 | 17 | dependencyCheck({ 18 | entries: this.filesSrc.map(function (el) { 19 | return path.relative(opts.package, el); 20 | }), 21 | path: opts.package, 22 | noDefaultEntries: opts.noDefaultEntries 23 | }, function (err, data) { 24 | if (err) { 25 | grunt.warn(err); 26 | done(); 27 | return; 28 | } 29 | 30 | var res; 31 | var pkg = data.package; 32 | var deps = data.used; 33 | 34 | if (opts.unused) { 35 | res = dependencyCheck.extra(pkg, deps, { 36 | excludeDev: opts.excludeUnusedDev, 37 | ignore: opts.ignoreUnused 38 | }); 39 | 40 | if (res.length > 0) { 41 | grunt.log.error('Packages in package.json not used in your code:', grunt.log.wordlist(res, {color: 'red'})); 42 | } 43 | } 44 | 45 | if (opts.missing) { 46 | res = dependencyCheck.missing(pkg, deps); 47 | 48 | if (res.length > 0) { 49 | grunt.warn('Dependencies not listed in package.json:', grunt.log.wordlist(res, {color: 'red'})); 50 | } 51 | } 52 | 53 | done(); 54 | }); 55 | }); 56 | }; 57 | --------------------------------------------------------------------------------