Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mohsenfallahnjd/convert-files-extension
Convert Files Extension from any to any
https://github.com/mohsenfallahnjd/convert-files-extension
convert-files-extension js npm npm-package
Last synced: about 1 month ago
JSON representation
Convert Files Extension from any to any
- Host: GitHub
- URL: https://github.com/mohsenfallahnjd/convert-files-extension
- Owner: mohsenfallahnjd
- Created: 2021-12-29T11:45:46.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2022-09-17T17:16:08.000Z (over 2 years ago)
- Last Synced: 2024-08-09T17:54:46.352Z (5 months ago)
- Topics: convert-files-extension, js, npm, npm-package
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/convert-files-extension
- Size: 5.86 KB
- Stars: 7
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# convert files extension
Convert Files Extension from any to any## install
```js
npm i convert-files-extension
```
```js
yarn add convert-files-extension
```> hint: this snipped, work on linux base os.
### implementation
```js
/**
* Convert Files Extension
*
* @param {string} path - Directory that contains the files
* @param {string} from - current extension
* @param {string} to - destination extension
*/
const changeFilesExtension = async (path, from = 'js', to = 'ts') => {
try {
const files = fs.readdirSync(path);
await files.forEach(f => {
if (f.split('.')[f.split('.').length - 1] === from) {
const oldPath = `${path}/${f}`;
const newPath = `${path}/${f.replace(`.${from}`, `.${to}`)}`;
exec(`mv ${oldPath} ${newPath}`);
}
});
await console.log('😇 Change extensions successfully');
} catch (e) {
await console.log('🛑 Change extensions failed');
await console.log(e);
}
}```
### usage
Replace your directory path with `YOUR_DIRECTORY_PATH`, your source extention with `SOURCE_EXT` and destination extention with `DEST_EXT`.
```js
changeFilesExtension('YOUR_DIRECTORY_PATH', 'SOURCE_EXT', 'DEST_EXT');```