-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
[Babel 8] Use more native fs methods #16459
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
Changes from all commits
87c8371
ef15ee1
04cd4ad
4b36240
6305792
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,22 +15,54 @@ export function chmod(src: string, dest: string): void { | |
| } | ||
| } | ||
|
|
||
| export function alphasort(a: string, b: string) { | ||
| return a.localeCompare(b, "en"); | ||
| } | ||
|
|
||
| type ReaddirFilter = (filename: string) => boolean; | ||
|
|
||
| export function readdir( | ||
| dirname: string, | ||
| includeDotfiles: boolean, | ||
| filter?: ReaddirFilter, | ||
| ): Array<string> { | ||
| return readdirRecursive(dirname, (filename, index, currentDirectory) => { | ||
| const stat = fs.statSync(path.join(currentDirectory, filename)); | ||
|
|
||
| if (stat.isDirectory()) return true; | ||
|
|
||
| if (process.env.BABEL_8_BREAKING) { | ||
| return ( | ||
| (includeDotfiles || filename[0] !== ".") && (!filter || filter(filename)) | ||
| fs | ||
| .readdirSync(dirname, { recursive: true, withFileTypes: true }) | ||
| .filter(dirent => { | ||
| // exclude directory entries from readdir results | ||
| if (dirent.isDirectory()) return false; | ||
| const filename = dirent.name; | ||
| return ( | ||
| (includeDotfiles || filename[0] !== ".") && | ||
| (!filter || filter(filename)) | ||
| ); | ||
| }) | ||
| .map(dirent => path.join(dirent.path, dirent.name)) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This all seems ok to me.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, I will open a new PR to bump node and replace |
||
| // readdirSyncRecursive conducts BFS, sort the entries so we can match the DFS behaviour of fs-readdir-recursive | ||
| // https://github.com/nodejs/node/blob/d6b12f5b77e35c58a611d614cf0aac674ec2c3ed/lib/fs.js#L1421 | ||
|
Comment on lines
+43
to
+44
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Q: does this matter in practice?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The ordering are different as files in sub folders will be visited after files in top level folders, see #16459 (comment) |
||
| .sort(alphasort) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Babel cli does not rely on the filenames ordering. The |
||
| ); | ||
| }); | ||
| } else { | ||
| return readdirRecursive( | ||
| "", | ||
| (filename, index, currentDirectory) => { | ||
| const stat = fs.statSync(path.join(currentDirectory, filename)); | ||
|
|
||
| // ensure we recurse into .* folders | ||
| if (stat.isDirectory()) return true; | ||
|
|
||
| return ( | ||
| (includeDotfiles || filename[0] !== ".") && | ||
| (!filter || filter(filename)) | ||
| ); | ||
| }, | ||
| // @ts-expect-error improve @types/fs-readdir-recursive typings | ||
| [], | ||
| dirname, | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. DefinitelyTyped PR: DefinitelyTyped/DefinitelyTyped#69489 |
||
| ); | ||
| } | ||
| } | ||
|
|
||
| export function readdirForCompilable( | ||
|
|
@@ -109,19 +141,7 @@ export async function compile(filename: string, opts: InputOptions) { | |
| } | ||
|
|
||
| export function deleteDir(path: string): void { | ||
| if (fs.existsSync(path)) { | ||
| fs.readdirSync(path).forEach(function (file) { | ||
| const curPath = path + "/" + file; | ||
| if (fs.lstatSync(curPath).isDirectory()) { | ||
| // recurse | ||
| deleteDir(curPath); | ||
| } else { | ||
| // delete file | ||
| fs.unlinkSync(curPath); | ||
| } | ||
| }); | ||
| fs.rmdirSync(path); | ||
| } | ||
| fs.rmSync(path, { force: true, recursive: true }); | ||
| } | ||
|
|
||
| process.on("uncaughtException", function (err) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
util.readdirnow returns the joined path, so we don't have to join it here.