Skip to content

Instantly share code, notes, and snippets.

@slinkardbrandon
Last active February 24, 2025 04:46
Show Gist options
  • Save slinkardbrandon/2a693695fb3044036b78a16e82ecdfcc to your computer and use it in GitHub Desktop.
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
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);
}
})()
@slinkardbrandon
Copy link
Author

Simply execute with node ./convertJsToJs.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment