I had a task to convert a project which was written using AMD and partially in ES6 (new code).
This gist describes my approach in details.
###Prerequisites The codebase is written in AMD and paritally in ES6. All AMD modules have simple .js extension, whereas all ES6 modules have .es6.js extension.
###Used tools
- bash
- find
- grep
- sed
- mv
- beautify-web/js-beautify
- jonbretman/amd-to-as6
###Lets go
So, we have N .js files total. M of them are written in ES6 and have extension .es6.js. The rest N-M are written in AMD.
Number of all .js files
find -type f -name "*.js" | wc -l
Number of all ES6 files
find -type f -name "*.js" | grep "es6" | wc -l
Number of all AMD files
find -type f -name "*.js" | grep -v "es6" | wc -l
Convert AMD file to ES6
amdtoes6 $file > $dest
Beautify file
js-beautify -f $dest -r -s 2 -c " " -n
Rename output as original file
mv MOVE $f
Composed command
find -type f -name "*.js" | grep -v "es6" | while read f; do amdtoes6 $f > MOVE && js-beautify -f MOVE -r -s 2 -c " " -n && mv MOVE $f; done
find -type f -name "*.js" | grep "es6" | while read f; do git mv $f `echo $f | sed s/.es6//`; done