Skip to content

Commit

Permalink
added: search-dir to config
Browse files Browse the repository at this point in the history
  • Loading branch information
binjospookie committed Dec 29, 2023
1 parent 9cc8e1f commit c62fb49
Show file tree
Hide file tree
Showing 14 changed files with 57 additions and 23 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ Pure Index supports three ways to define config.
"entry": "index.ts",
"exclude": ["node_modules"],
"extensions": ["ts", "tsx"],
"searchDir": "my-path",
"babelPlugins": ["typescript"],
"batch": 100
}
Expand All @@ -70,6 +71,7 @@ Pure Index supports three ways to define config.
"entry": "index.ts",
"exclude": ["node_modules"],
"extensions": ["ts", "tsx"],
"searchDir": "my-path",
"babelPlugins": ["typescript"],
"batch": 100
}
Expand All @@ -82,6 +84,7 @@ module.exports = {
entry: 'index.ts',
exclude: ['node_modules'],
extensions: ['ts', 'tsx'],
searchDir: 'my-path',
babelPlugins: ['typescript'],
batch: 100
}
Expand All @@ -92,6 +95,7 @@ module.exports = {
- `entry (String)` — path to the package index file. relative to the package directory.
- `extensions (Array<string>)` — list of file extensions to be considered during the search.
- `exclude (Array<string>)` — list of directories that will be excluded when searching for imports.
- `searchDir (string)` — path to the directory where imports should be searched for.
- `babelPlugins (Array<string>)` — list of babel plugins that will be used when parsing files.
- `batch (Number)` — number of files to be traversed in parallel. Changing the value may speed up or slow down the script. Choose the value yourself.

Expand Down Expand Up @@ -132,6 +136,17 @@ Allows to override the config values for package.
}
```

### `--search-dir, -s`

```diff
"scripts": {
"build": "webpack ./webpack.config.js",
- "check-exports": "pure-index",
+ "check-exports": "pure-index --search-dir /Users/user/another-repo",
"test": "vitest"
}
```

### `--babel-plugins, -p`

```diff
Expand Down
6 changes: 4 additions & 2 deletions bin/__tests__/getConfig/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ test('default value', async () => {
collectUsages: 'package-a',
batch: 1,
babelPlugins: 'decorators-legacy,classPrivateProperties',
exclude: 'biba,boba,.cache,www/assets,__tests__'
exclude: 'biba,boba,.cache,www/assets,__tests__',
searchDir: 'dir-from-cli'
}
}))
}))
Expand All @@ -33,7 +34,8 @@ test('default value', async () => {
'.cache',
'www/assets',
'__tests__'
])
]),
searchDir: 'dir-from-cli'
})

vi.resetAllMocks()
Expand Down
9 changes: 6 additions & 3 deletions bin/__tests__/getConfig/config+cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ test('default value', async () => {
batch: 500,
entry: 'src/index.ts',
exclude: ['build'],
extensions: ['js', 'jsx']
extensions: ['js', 'jsx'],
searchDir: 'dir-from-cli'
}
})
})
Expand All @@ -26,7 +27,8 @@ test('default value', async () => {
collectUsages: 'package-a',
batch: 1,
babelPlugins: 'decorators-legacy,classPrivateProperties',
exclude: 'biba,boba,.cache,www/assets,__tests__'
exclude: 'biba,boba,.cache,www/assets,__tests__',
searchDir: 'dir-from-config'
}
}))
}))
Expand All @@ -47,7 +49,8 @@ test('default value', async () => {
'.cache',
'www/assets',
'__tests__'
])
]),
searchDir: 'dir-from-cli'
})

vi.resetAllMocks()
Expand Down
3 changes: 2 additions & 1 deletion bin/__tests__/getConfig/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ export const CONFIG = {
collectUsages: null,
entry: 'index.ts',
exclude: new Set(['node_modules']),
extensions: ['ts', 'tsx']
extensions: ['ts', 'tsx'],
searchDir: 'repo-root'
}
6 changes: 5 additions & 1 deletion bin/__tests__/getConfig/index.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { expect, test } from 'vitest'
import { expect, test, mock, vi } from 'vitest'

import { getConfig } from '../../getConfig.js'
import { CONFIG } from './constants.js'

test('default value', async () => {
vi.mock('../../../bin/utils/getRepoRoot.js', () => ({
getRepoRoot: () => 'repo-root'
}))

const config = await getConfig()

expect(config).toStrictEqual(CONFIG)
Expand Down
6 changes: 4 additions & 2 deletions bin/__tests__/getConfig/withconfig.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ test('default value', async () => {
batch: 500,
entry: 'src/index.ts',
exclude: ['build'],
extensions: ['js', 'jsx']
extensions: ['js', 'jsx'],
searchDir: 'dir-from-config'
}
})
})
Expand All @@ -26,7 +27,8 @@ test('default value', async () => {
batch: 500,
entry: 'src/index.ts',
extensions: ['js', 'jsx'],
exclude: new Set([...CONFIG.exclude, 'build'])
exclude: new Set([...CONFIG.exclude, 'build']),
searchDir: 'dir-from-config'
})

vi.resetAllMocks()
Expand Down
1 change: 1 addition & 0 deletions bin/collectUsages.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { createStatusAPI } from './utils/index.js'
* collectUsages: string
* exclude: Set<string>
* extensions: Array<string>
* searchDir: string
* },
* }}
*
Expand Down
14 changes: 3 additions & 11 deletions bin/fileTraversal/getFiles.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { execSync } from 'node:child_process'
import { join } from 'node:path'
import { fdir } from 'fdir'

const getRepoRoot = () =>
execSync('git rev-parse --show-toplevel', { encoding: 'utf8' }).trim()

const formattedExtensions = list =>
list.reduce((acc, ext) => acc + (acc ? ',' : '') + ext, '')

Expand All @@ -13,6 +9,7 @@ const formattedExtensions = list =>
* config: {
* exclude: Set<string>
* extensions: Array<string>
* searchDir: string
* }
* pkg: {
* name: string
Expand All @@ -33,18 +30,13 @@ const getFiles = async ({ config, pkg }) => {
)
.join('|')
const excludeRegExp = new RegExp(exclude)
const repoRoot = getRepoRoot()
const source = join(
repoRoot,
'**',
`*.{${formattedExtensions(config.extensions)}}`
)
const source = join('**', `*.{${formattedExtensions(config.extensions)}}`)

const files = new fdir()
.exclude(dirName => excludeRegExp.test(dirName))
.globWithOptions([source], { dot: false })
.withFullPaths()
.crawl(repoRoot)
.crawl(config.searchDir)
.sync()

return files
Expand Down
1 change: 1 addition & 0 deletions bin/fileTraversal/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { getFiles } from './getFiles.js'
* batch: number
* exclude: Set<string>
* extensions: Array<string>
* searchDir: string
* }
* pkg: {
* name: string
Expand Down
11 changes: 9 additions & 2 deletions bin/getConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ import { join } from 'node:path'
import { lilconfig } from 'lilconfig'
import meow from 'meow'

import { getRepoRoot } from './utils/index.js'

const BASE_CONFIG = {
babelPlugins: ['typescript'],
batch: 100,
collectUsages: null,
entry: 'index.ts',
searchDir: null,
exclude: new Set(['node_modules']),
extensions: ['ts', 'tsx']
}
Expand All @@ -17,6 +20,7 @@ const cli = meow(
--entry, -e path to the package index file. relative to the package directory
--exclude, -i list of directories that will be excluded when searching for imports
--extensions, -x list of file extensions to be considered during the search
--search-dir, -s path to the directory where imports should be searched for
--babel-plugins, -p list of babel plugins that will be used when parsing files
--batch, -b number of files to be traversed in parallel
--collect-usages, -u outputs a list of all unique uses of the package
Expand All @@ -29,6 +33,7 @@ const cli = meow(
entry: { type: 'string', shortFlag: 'e' },
exclude: { type: 'string', shortFlag: 'i' },
extensions: { type: 'string', shortFlag: 'x' },
searchDir: { type: 'string', shortFlag: 's' },
babelPlugins: { type: 'string', shortFlag: 'p' },
batch: { type: 'number', shortFlag: 'b' },
collectUsages: { type: 'string', shortFlag: 'u' }
Expand All @@ -51,7 +56,8 @@ const getConfig = async () => {
babelPlugins = BASE_CONFIG.babelPlugins,
entry = BASE_CONFIG.entry,
batch = BASE_CONFIG.batch,
extensions = BASE_CONFIG.extensions
extensions = BASE_CONFIG.extensions,
searchDir
} = result.config

return result === null
Expand All @@ -68,7 +74,8 @@ const getConfig = async () => {
collectUsages: cli.flags.collectUsages || BASE_CONFIG.collectUsages,
extensions: cli.flags.extensions
? cli.flags.extensions.split(',')
: extensions
: extensions,
searchDir: cli.flags.searchDir || searchDir || getRepoRoot()
}
}

Expand Down
1 change: 1 addition & 0 deletions bin/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { createStatusAPI, readJSON } from './utils/index.js'
* entry: string
* exclude: Set<string>
* extensions: Array<string>
* searchDir: string
* },
* }}
*
Expand Down
4 changes: 4 additions & 0 deletions bin/utils/getRepoRoot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { execSync } from 'node:child_process'

export const getRepoRoot = () =>
execSync('git rev-parse --show-toplevel', { encoding: 'utf8' }).trim()
1 change: 1 addition & 0 deletions bin/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,5 @@ const createStatusAPI = ({ title }) => {
}

export { readFile, readJSON, createStatusAPI }
export { getRepoRoot } from './getRepoRoot.js'
export { ObservableSet } from './observableSet.js'
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "pure-index",
"type": "module",
"version": "0.0.29",
"version": "0.0.32",
"description": "Utility for monorepos. It helps to find unused exports from packages or get a list of all unique uses of any package",
"main": "./bin/index.js",
"bin": "./bin/index.js",
Expand Down

0 comments on commit c62fb49

Please sign in to comment.