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: scanDirs respects input dirs order #148

Merged
merged 2 commits into from
Nov 15, 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
3 changes: 3 additions & 0 deletions playground/composables-override/foo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function () {
return 'bar'
}
39 changes: 20 additions & 19 deletions src/scan-dirs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,29 @@ export async function scanDirExports (dir: string | string[], options?: ScanDirE

const fileFilter = options?.fileFilter || (() => true)
const filePatterns = options?.filePatterns || ['*.{ts,js,mjs,cjs,mts,cts}']
const files = await fg(
dirs.flatMap(i => [
i,
...filePatterns.map(p => join(i, p))
]),
{
absolute: true,
cwd: options?.cwd || process.cwd(),
onlyFiles: true,
followSymbolicLinks: true
}
).then(r => r.map(f => normalize(f)).sort().filter(fileFilter))

const imports: Import[] = []

await Promise.all(
files.map(async (path) => {
imports.push(...await scanExports(path))
})
const result = await Promise.all(
// Do multiple glob searches to persist the order of input dirs
dirs.map(async i => await fg(
[i, ...filePatterns].map(p => join(i, p)),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's a mistake here, it should be [i, ...filePatterns.map(p => join(i, p))] instead

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Fixed in v1.0.1

{
absolute: true,
cwd: options?.cwd || process.cwd(),
onlyFiles: true,
followSymbolicLinks: true
})
.then(r => r
.map(f => normalize(f))
.filter(fileFilter)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't think it's necessary to filter here since you're filtering after flattening the array

.sort()
)
)
)

return imports
const files = Array.from(new Set(result.flat())).filter(fileFilter)
const fileExports = await Promise.all(files.map(scanExports))

return fileExports.flat()
}

export async function scanExports (filepath: string) {
Expand Down
19 changes: 19 additions & 0 deletions test/scan-dirs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,23 @@ describe('scan-dirs', () => {
)
.toContain('nested/index.ts')
})

test('scanDirs should respect dirs order', async () => {
const firstFolder = join(__dirname, '../playground/composables')
const secondFolder = join(__dirname, '../playground/composables-override')
const options = {
filePatterns: [
'*.{ts,js,mjs,cjs,mts,cts}',
'*/index.{ts,js,mjs,cjs,mts,cts}'
]
}

const [result1, result2] = await Promise.all([
scanDirExports([firstFolder, secondFolder], options),
scanDirExports([secondFolder, firstFolder], options)
])

expect(result1.at(-1)?.from).toBe(join(secondFolder, 'foo.ts'))
expect(result2.at(0)?.from).toBe(join(secondFolder, 'foo.ts'))
})
})