UPDATE . Please check the short version of this script below if you are in a hurry.
A simple bash script does the job for ya:
#!/bin/bash for FILE in `find . -name "*.js" -type f -o -path './node_modules' -prune -o -path './components' -prune` do if [ -e $FILE ] ; then COFFEE=${FILE//.js/.coffee} echo "converting ${FILE} to ${COFFEE}" js2coffee "$FILE" > "$COFFEE" else echo "File: {$1} does not exist!" fi done
create a file, for example, all2coffee, put it in / usr / local / bin, add the chmod + x flag to the terminal
NEEDS
js2coffee installed globally, if not already instaleld: npm install -g js2coffee
SCRIPT EXPLAINED
for the loop:
for FILE in `find arguments` .... means:
Find the output assigned to the FILE line each time find stumbles upon a .js file
find options:
-name "*.js" capture all files with the completion of .js
-type f must be of type file , since we do not want a .js dir, but only a file
-o -path './node_modules' -prune
excludes files in the ./node_modules directory, adding -prune is critical, otherwise the search will go down to the directory and print *.js files found in the directory
make block:
if [ -e ${FILE} ] ; then
Flag
-e checks if the line from FILE is an existing file in the file system; otherwise, else is executed.
line processing:
COFFEE=${FILE//.js/.coffee}
we pass thte COFFEE variable string where we replace .js with . coffee using Bash string processing: ${STRING//match_this/replace_with}
conversion:
js2coffee "$FILE" > "$COFFEE" we pass js2coffee with FILE and COFFEE as strings
EXTRA
Do you like to move all your converted .coffee files to the new directory, but keep the structure?
Use find with rsync on Linux or ditto on Os X, since cp will not create the directories needed by this command. Here is a little script to execute in the main directory that will do the work
all files. coffee will be in the / coffee directory, copying the hierarchy of .js files
for FILE in `find . -name "*.coffee"` do ditto .${FILE/./} coffee${FILE/./} done
run after , you converted your files to .coffee
UPDATE
you can change ditto or rsync to mv after the first run to move files, since mv , for example cp does not create sub dirs.
UPDATE 2
Added one liner for on time, see my second answer below!
UPDATE 3
Added option to exclude the ./node_modules directory from the conversion, for those who do not want to convert their dependencies