Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(scan): options for custom filePatterns, remove nested glob by default #95

Merged
merged 3 commits into from
Jul 21, 2022
Merged
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
2 changes: 1 addition & 1 deletion playground/composables/nested/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export default function () {
return 'nested'
return 'from nested composables'
}
4 changes: 4 additions & 0 deletions playground/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const count = ref(1)
function inc () {
count.value += 1
}

</script>

<template>
Expand All @@ -15,6 +16,9 @@ function inc () {
<button @click="bump">
x1
</button>
<div>
{{ nested() }}
</div>
</div>
</template>

Expand Down
2 changes: 1 addition & 1 deletion playground/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default defineConfig({
'vue'
],
dirs: [
'./composables'
'./composables/**'
],
addons: {
vueTemplate: true
Expand Down
4 changes: 2 additions & 2 deletions src/scan-dirs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ export async function scanDirExports (dir: string | string[], options?: ScanDirE
const dirs = Array.isArray(dir) ? dir : [dir]

const fileFilter = options?.fileFilter || (() => true)
const filePatterns = options?.filePatterns || ['*.{ts,js,mjs,cjs,mts,cts}']
const files = await fg(
dirs.flatMap(i => [
i,
join(i, '*.{ts,js,mjs,cjs,mts,cts}'),
join(i, '*/index.{ts,js,mjs,cjs,mts,cts}')
...filePatterns.map(p => join(i, p))
]),
{
absolute: true,
Expand Down
14 changes: 14 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,21 @@ export interface UnimportOptions {
export type PathFromResolver = (_import: Import) => string | undefined

export interface ScanDirExportsOptions {
/**
* Glob patterns for matching files
*
* @default ['*.{ts,js,mjs,cjs,mts,cts}']
*/
filePatterns?: string[]
/**
* Custom function to filter scanned files
*/
fileFilter?: (file: string) => boolean
/**
* Current working directory
*
* @default process.cwd()
*/
cwd?: string
}

Expand Down
19 changes: 14 additions & 5 deletions test/scan-dirs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@ describe('scan-dirs', () => {
"from": "index.ts",
"name": "multiplier",
},
{
"as": "nested",
"from": "nested/index.ts",
"name": "default",
},
{
"as": "useDoubled",
"from": "index.ts",
Expand All @@ -52,4 +47,18 @@ describe('scan-dirs', () => {
]
`)
})

test('scanDirExports nested', async () => {
const dir = join(__dirname, '../playground/composables')
expect((await scanDirExports(dir, {
filePatterns: [
'*.{ts,js,mjs,cjs,mts,cts}',
'*/index.{ts,js,mjs,cjs,mts,cts}'
]
}))
.map(i => relative(dir, i.from))
.sort()
)
.toContain('nested/index.ts')
})
})