Skip to content

Commit

Permalink
✨ Ignore checking babel/webpack plugins by default
Browse files Browse the repository at this point in the history
Add a new option `--no-regex-filtering` to retain previous behavior.
  • Loading branch information
obahareth committed Aug 11, 2019
1 parent 8054112 commit f0667c6
Show file tree
Hide file tree
Showing 2,005 changed files with 200,702 additions and 13 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ Usage: are-you-es5 check [options] <path>
Checks if all node_modules (including monorepos) at <path> are ES5
Options:
-a, --all Check all node_modules instead of just direct dependencies
-v, --verbose Log all messages (including modules that are ES5)
-r, --regex Get babel-loader exclude regex to ignore all node_modules except non-ES5 ones
-h, --help output usage information
-a, --all Check all node_modules instead of just direct dependencies
-v, --verbose Log all messages (including modules that are ES5)
--no-regex-filtering Stops all filtering on babel-loader exclude regex (does not hide anything)
-r, --regex Get babel-loader exclude regex to ignore all node_modules except non-ES5 ones, by default does not show any babel or webpack modules, use with --no-regex-filtering if you want to see everything
-h, --help output usage information
```

### Example
Expand Down
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@ program
'Check all node_modules instead of just direct dependencies'
)
.option('-v, --verbose', 'Log all messages (including modules that are ES5)')
.option(
'--no-regex-filtering',
'Stops all filtering on babel-loader exclude regex (does not hide anything) '
)
.option(
'-r, --regex',
'Get babel-loader exclude regex to ignore all node_modules except non-ES5 ones'
'Get babel-loader exclude regex to ignore all node_modules except non-ES5 ones, by default does not show any babel or webpack modules, use with --no-regex-filtering if you want to see everything'
)
.action((path: string, cmd: any) => {
const config: IModuleCheckerConfig = {
checkAllNodeModules: cmd.all === true,
ignoreBabelAndWebpackPackages: cmd.regexFiltering,
logEs5Packages: cmd.verbose === true
}

Expand Down
14 changes: 13 additions & 1 deletion src/modules-checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { IPackageJSON } from './types/package-json'

export class ModulesChecker {
public static readonly defaultConfig: IModuleCheckerConfig = {
checkAllNodeModules: false,
ignoreBabelAndWebpackPackages: true,
logEs5Packages: false
}

Expand Down Expand Up @@ -48,12 +50,16 @@ export class ModulesChecker {
}

public getDeps(): string[] | null {
const deps = this.getDepsFromRootPackageJson()
let deps = this.getDepsFromRootPackageJson()

if (this.config.checkAllNodeModules) {
deps.push(...this.getAllNodeModules())
}

if (this.config.ignoreBabelAndWebpackPackages) {
deps = this.dependenciesWithoutBabelAndWebpackPages(deps)
}

// convert to and from a Set to remove duplicates
return [...new Set(deps)].sort()
}
Expand All @@ -77,6 +83,12 @@ export class ModulesChecker {
return true
}

private dependenciesWithoutBabelAndWebpackPages(dependencies: string[]) {
const ignoreRegex = /(babel|webpack)|(loader$)/

return dependencies.filter(dep => !ignoreRegex.test(dep))
}

private getDepsFromRootPackageJson() {
const packageJsonPath = path.join(this.dir, 'package.json')
const packageJson = require(packageJsonPath)
Expand Down
1 change: 1 addition & 0 deletions src/types/module-checker-config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export default interface IModuleCheckerConfig {
logEs5Packages?: boolean
checkAllNodeModules?: boolean
ignoreBabelAndWebpackPackages?: boolean
}
34 changes: 32 additions & 2 deletions tests/modules-checker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@ import {
allDependencies,
allDependenciesWithEntryPaths,
directDependencies,
subpackageDependencies
subpackageDependencies,
directDependenciesWithoutBabelAndWebpack,
allDependenciesWithoutBabelAndWebpack
} from './support/helpers/dependencies'

jest.mock('acorn')

describe('static vars', () => {
it('has a defaultConfig with logEs5Packages set to false', () => {
expect(ModulesChecker.defaultConfig).toEqual({
checkAllNodeModules: false,
ignoreBabelAndWebpackPackages: true,
logEs5Packages: false
})
})
Expand All @@ -35,7 +39,11 @@ describe('constructor', () => {
})

it('can receive a second optional config argument that overrides default config', () => {
const config: IModulesCheckerConfig = { logEs5Packages: true }
const config: IModulesCheckerConfig = {
checkAllNodeModules: false,
ignoreBabelAndWebpackPackages: true,
logEs5Packages: true // This is the overrideen value
}

expect(new ModulesChecker('', config).config).toEqual(config)
})
Expand All @@ -49,6 +57,17 @@ describe('getDeps', () => {

const parsedDeps = modulesChecker.getDeps()

expect(parsedDeps).toEqual(directDependenciesWithoutBabelAndWebpack)
})

it('returns direct dependencies including babel and webpack if ignoreBabelAndWebpackPackages is passed as false', () => {
const modulesChecker = new ModulesChecker(
path.join(__dirname, '/support/fixtures/root'),
{ ignoreBabelAndWebpackPackages: false }
)

const parsedDeps = modulesChecker.getDeps()

expect(parsedDeps).toEqual(directDependencies)
})

Expand All @@ -60,6 +79,17 @@ describe('getDeps', () => {

const parsedDeps = modulesChecker.getDeps()

expect(parsedDeps).toEqual(allDependenciesWithoutBabelAndWebpack)
})

it('returns all node_modules including babel and webpack when checkAllNodeModules is true and ignoreBabelAndWebpackPackages is false', () => {
const modulesChecker = new ModulesChecker(
path.join(__dirname, '/support/fixtures/root'),
{ checkAllNodeModules: true, ignoreBabelAndWebpackPackages: false }
)

const parsedDeps = modulesChecker.getDeps()

expect(parsedDeps).toEqual(allDependencies)
})
})
Expand Down
1 change: 1 addition & 0 deletions tests/support/fixtures/root/node_modules/.bin/cssesc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tests/support/fixtures/root/node_modules/.bin/jsesc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tests/support/fixtures/root/node_modules/.bin/json5

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tests/support/fixtures/root/node_modules/.bin/parser

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tests/support/fixtures/root/node_modules/.bin/semver

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions tests/support/fixtures/root/node_modules/@babel/code-frame/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f0667c6

Please sign in to comment.