-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: separate fs dependency from core logic
- Loading branch information
Showing
4 changed files
with
85 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import fs from 'fs' | ||
import parseInterface, { Method } from './parseInterface' | ||
|
||
type FileData = { | ||
name: string | ||
isDir: false | ||
methods: Method[] | ||
} | ||
|
||
type DirData = { | ||
name: string | ||
isDir: true | ||
tree: DirentTree | ||
} | ||
|
||
export type DirentTree = { path: string; children: (FileData | DirData)[] } | ||
|
||
export const getDirentTree = (input: string) => { | ||
const tree: DirentTree = { path: input, children: [] } | ||
|
||
fs.readdirSync(input, { withFileTypes: true }) | ||
.filter(dirent => !dirent.name.startsWith('$') && !dirent.name.startsWith('@')) | ||
.sort((a, b) => (a.name < b.name ? -1 : 1)) | ||
.forEach(dirent => { | ||
if (dirent.isDirectory()) { | ||
tree.children.push({ | ||
name: dirent.name, | ||
isDir: true, | ||
tree: getDirentTree(`${input}/${dirent.name}`) | ||
}) | ||
} else if (dirent.name.endsWith('.ts')) { | ||
const methodsInterface = parseInterface( | ||
fs.readFileSync(`${input}/${dirent.name}`, 'utf8'), | ||
'Methods' | ||
) | ||
if (!methodsInterface || methodsInterface.every(({ props }) => !Object.keys(props).length)) | ||
return | ||
|
||
tree.children.push({ | ||
name: dirent.name, | ||
isDir: false, | ||
methods: methodsInterface | ||
}) | ||
} | ||
}) | ||
|
||
return tree | ||
} |