diff --git a/README.md b/README.md new file mode 100644 index 0000000..99ef9f6 --- /dev/null +++ b/README.md @@ -0,0 +1,102 @@ +# amdcheck + +Checks unused paths and unused dependencies in AMD modules. + +Reports: +- for every module with an identified problem: + - full path to file + - list of unused paths if any + - list of unused dependencies if any + +- global statistics: + - number of unused paths + - number of files with at least one unused paths + - number of unused dependencies + - number of files with at least one unused dependency + - number of processed files + + ## Installation + + ``` + npm install amdcheck --save-dev + ``` + or + + ``` + npm i -D amdcheck + ``` + + ## Usage + + ### Command + + ``` + amdcheck [options] [, [, ...]] + ``` + ### options + + options are: + + - -h, --help + display this message + + - -s, --stats-only + display only stats (total unused paths, total unused dependencies, + total processed files) + + ### Example + + ``` + amdcheck 'lib/**/*.js' 'tests/**/*.js' + ``` + + ## Configuration + + By default, amdcheck reports every unused paths, but you can specify a list of + paths to exclude from search in either two ways: in package.json file or in + amdcheck.json file at the same level. + + ### Configuration in `package.json` + Add a `config.amdcheck.excludedPaths` entry in your `package.json` and simply specify an array of paths to exclude: + + ``` + { + … + "config": { + "amdcheck": { + "excludedPaths": [ + "jquery.plugin1", + "jquery.plugin2", + "polyfill1", + "polyfill2", + … + ] + } + } + … + } + ``` + + ### Configuration in `amdcheck.json` + Alternatively you can create a `amdcheck.json` at the root of your project with just the amdcheck conf: + + ``` + { + "excludedPaths": [ + "jquery.plugin1", + "jquery.plugin2", + "polyfill1", + "polyfill2", + … + ] + } + ``` + + ### Note about configuration + + If there is a **valid JSON file** named `amdcheck.json` at the root of your project, + any configuration from `package.json` **will be ignored**. + + ## License + + This software is licensed under the MIT license diff --git a/index.js b/index.js index a474bd9..6ba46b8 100755 --- a/index.js +++ b/index.js @@ -9,6 +9,35 @@ const glob = require('glob-promise') let paths = Array.from(process.argv).slice(2) +const confAmdcheck = getConf(process.cwd()) + +const excludedPaths = confAmdcheck && confAmdcheck.excludedPaths + ? confAmdcheck.excludedPaths + : [] + +function getConf(baseDir) { + + const confAmdcheckFilePath = baseDir + '/amdcheck.json' + const confAmdcheckFileExists = fs.existsSync(confAmdcheckFilePath) + + if (confAmdcheckFileExists) { + try { + const confAmdcheck = require(confAmdcheckFilePath) + console.info(`\nUsing conf from ${confAmdcheckFilePath}`) + } catch (e) { + console.error(`${confAmdcheckFilePath} is not a valid JSON file!`) + } + } + + const pkg = require(baseDir + '/package.json') + const confAmdcheck = pkg.config + ? pkg.config.amdcheck + : {} + + console.info('Using conf from', 'package.json') + return confAmdcheck +} + const statsOnly = paths.some((arg) => { return arg === '-s' || arg === '--stats-only' }) @@ -31,17 +60,6 @@ if (paths[0].includes('--help') || paths[0].includes('-h')) { function printUsage() { console.log(`Check unused paths and unused dependencies in AMD modules. -Usage: amdcheck [options] [, [, ...]] - - options: - -h, --help - display this message - - -s, --stats-only - display only stats (total unused paths, total unused dependencies, - total processed files) - -Example: amdcheck 'lib/**/*.js' 'tests/**/*.js' `); } @@ -74,10 +92,14 @@ Promise.all(globs) const error = {} result.results.forEach(function (r) { - totalUnusedPaths += r.unusedPaths.length - if (r.unusedPaths.length > 0) { + const unusedPaths = r.unusedPaths.filter((path) => { + return !excludedPaths.includes(path) + }) + + totalUnusedPaths += unusedPaths.length + if (unusedPaths.length > 0) { totalFilesWithUnusedPaths++ - error.unusedPaths = r.unusedPaths + error.unusedPaths = unusedPaths } totalUnusedDependencies += r.unusedDependencies.length