Skip to content

Commit

Permalink
feat: scanDirExports utils, close #9 (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Mar 14, 2022
1 parent 5e183a8 commit e6f7c71
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 1 deletion.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,12 @@
"dependencies": {
"@rollup/pluginutils": "^4.1.2",
"escape-string-regexp": "^5.0.0",
"globby": "^13.1.1",
"local-pkg": "^0.4.1",
"magic-string": "^0.26.0",
"mlly": "^0.4.3",
"pathe": "^0.2.0",
"scule": "^0.2.1",
"unplugin": "^0.3.3"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from './preset'
export * from './utils'
export * from './types'
export * from './scan'
export * from './context'
export { builtinPresets } from './presets'
export type { BuiltinPresetName } from './presets'
59 changes: 59 additions & 0 deletions src/scan.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { promises as fs } from 'fs'
import { globby } from 'globby'
import { resolve, parse as parsePath } from 'pathe'
import { findExports } from 'mlly'
import { camelCase } from 'scule'
import { Import, ScanDirExportsOptions } from './types'

export async function resolveFiles (path: string, pattern: string | string[]) {
const files = await globby(pattern, { cwd: path, followSymbolicLinks: true })
return files.map(p => resolve(path, p))
}

export async function scanDirExports (dir: string | string[], options?: ScanDirExportsOptions) {
const dirs = Array.isArray(dir) ? dir : [dir]

const fileFilter = options?.fileFilter || (() => true)
const files = await Promise.all(
dirs.map(i => resolveFiles(i, [
'*.{ts,js,mjs,cjs,mts,cts}',
'*/index.{ts,js,mjs,cjs,mts,cts}'
]))
).then(r => r.flat().filter(fileFilter))

const imports: Import[] = []

await Promise.all(
files.map(async (path) => {
imports.push(...await scanExports(path))
})
)

return imports
}

export async function scanExports (filepath: string) {
const imports: Import[] = []
const code = await fs.readFile(filepath, 'utf-8')
const exports = findExports(code)
const defaultExport = exports.find(i => i.type === 'default')

if (defaultExport) {
let name = parsePath(filepath).name
if (name === 'index') {
name = parsePath(filepath.split('/').slice(0, -1).join('/')).name
}
imports.push({ name: 'default', as: camelCase(name), from: filepath })
}
for (const exp of exports) {
if (exp.type === 'named') {
for (const name of exp.names) {
imports.push({ name, as: name, from: filepath })
}
} else if (exp.type === 'declaration') {
imports.push({ name: exp.name, as: exp.name, from: filepath })
}
}

return imports
}
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,7 @@ export interface UnimportOptions {
}

export type PathFromResolver = (_import: Import) => string | undefined

export interface ScanDirExportsOptions {
fileFilter?: (file: string) => boolean
}
25 changes: 24 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2515,7 +2515,7 @@ c8@latest:
languageName: node
linkType: hard

"fast-glob@npm:^3.2.9":
"fast-glob@npm:^3.2.11, fast-glob@npm:^3.2.9":
version: 3.2.11
resolution: "fast-glob@npm:3.2.11"
dependencies:
Expand Down Expand Up @@ -2893,6 +2893,19 @@ c8@latest:
languageName: node
linkType: hard

"globby@npm:^13.1.1":
version: 13.1.1
resolution: "globby@npm:13.1.1"
dependencies:
dir-glob: ^3.0.1
fast-glob: ^3.2.11
ignore: ^5.2.0
merge2: ^1.4.1
slash: ^4.0.0
checksum: e6c43409c6c31b374fbd1c01a8c1811de52336928be9c697e472d2a89a156c9cbf1fb33863755c0447b4db16485858aa57f16628d66a6b7c7131669c9fbe76cd
languageName: node
linkType: hard

"graceful-fs@npm:^4.1.2, graceful-fs@npm:^4.1.6, graceful-fs@npm:^4.2.0, graceful-fs@npm:^4.2.6":
version: 4.2.9
resolution: "graceful-fs@npm:4.2.9"
Expand Down Expand Up @@ -4731,6 +4744,13 @@ c8@latest:
languageName: node
linkType: hard

"slash@npm:^4.0.0":
version: 4.0.0
resolution: "slash@npm:4.0.0"
checksum: da8e4af73712253acd21b7853b7e0dbba776b786e82b010a5bfc8b5051a1db38ed8aba8e1e8f400dd2c9f373be91eb1c42b66e91abb407ff42b10feece5e1d2d
languageName: node
linkType: hard

"smart-buffer@npm:^4.2.0":
version: 4.2.0
resolution: "smart-buffer@npm:4.2.0"
Expand Down Expand Up @@ -5263,9 +5283,12 @@ unbuild@latest:
c8: latest
escape-string-regexp: ^5.0.0
eslint: ^8.10.0
globby: ^13.1.1
local-pkg: ^0.4.1
magic-string: ^0.26.0
mlly: ^0.4.3
pathe: ^0.2.0
scule: ^0.2.1
standard-version: latest
typescript: latest
unbuild: latest
Expand Down

0 comments on commit e6f7c71

Please sign in to comment.