Last active
February 24, 2025 04:46
-
-
Save slinkardbrandon/2a693695fb3044036b78a16e82ecdfcc to your computer and use it in GitHub Desktop.
Recursively rename all file extensions in a given directory from javascript to typescript
This file contains hidden or 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
| const { resolve } = require('path'); | |
| const { readdir, rename } = require('fs').promises; | |
| const DIRECTORY = 'src'; | |
| async function* getFiles(dir) { | |
| const dirents = await readdir(dir, { withFileTypes: true }); | |
| for (const dirent of dirents) { | |
| const res = resolve(dir, dirent.name); | |
| if (dirent.isDirectory()) { | |
| yield* getFiles(res); | |
| } else { | |
| yield res; | |
| } | |
| } | |
| } | |
| (async () => { | |
| for await (const originalFilePath of getFiles(DIRECTORY)) { | |
| const parts = originalFilePath.split('.'); | |
| parts[parts.length - 1] = parts[parts.length - 1].replace(/js/, 'ts'); | |
| const newFilePath = parts.join('.'); | |
| await rename(originalFilePath, newFilePath); | |
| } | |
| })() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Simply execute with
node ./convertJsToJs.js