1.0.0 • Published 2 years ago

convert-files-extension v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

convert files extension

Convert Files Extension from any to any

hint: this snipped, work on linux base os.

implementation

/**
 * 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.

changeFilesExtension('YOUR_DIRECTORY_PATH', 'SOURCE_EXT', 'DEST_EXT');