Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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] <glob> [<glob>, [<glob>, ...]]
```
### 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
50 changes: 36 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
})
Expand All @@ -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] <glob> [<glob>, [<glob>, ...]]

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'
`);
}

Expand Down Expand Up @@ -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
Expand Down