Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting whole project to CoffeeScript with js2coffee

Problem:

js2coffe does work only with single file input/output redirection. to convert a whole project and it's directory structure can be a real pain.

Want to convert a ExpressJS default application project incl. it's directory structure?

Check out the script below

like image 401
Inoperable Avatar asked Dec 30 '12 15:12

Inoperable


People also ask

Should you use CoffeeScript?

CoffeeScript is something that makes even good JavaScript code better. CoffeeScript compiled code can do everything that natively written JavaScript code can, only the code produced by using CoffeeScript is way shorter, and much easier to read.

How do I use CoffeeScript in HTML?

You simple need to add a <script type="text/coffeescript" src="app. coffee"></script> to execute coffee script code in an HTML file. In other cases, I've seen people use the attributes of type="coffeescript" and type="coffee" , so they might work for you as well. Save this answer.


1 Answers

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

make a file, for example all2coffee, put it in /usr/local/bin, add an chmod + x flag it in terminal

REQUIREMENTS

js2coffee installed globally, if not yet instaleld do: npm install -g js2coffee

SCRIPT EXPLAINED

for loop:

for FILE in `find arguments` .... means:

find output is assigned to FILE string every time find stumbles upon a .js file

find parameters:

-name "*.js" grab all files with .js ending

-type f must be of type file since we don't want .js dir's but file's only

-o -path './node_modules' -prune

excludes files in dir's ./node_modules adding -prune is crucial, otherwise find will descend into the dir and print *.js files found in the directory

do block:

if [ -e ${FILE} ] ; then

-e flag checks if the string from FILE is a existing file on the filesystem, otherwise else is executed.

string manipulation:

COFFEE=${FILE//.js/.coffee}

we assing thte COFFEE variable a string where we replace .js with .coffee through the bash string manipulation: ${STRING//match_this/replace_with}

conversion:

js2coffee "$FILE" > "$COFFEE" we feed js2coffee with FILE and COFFEE as strings

EXTRA:

You like to move all of your converted .coffee files to a new directory, but keep the structure?

Use find with rsync in Linux or ditto on Os X since cp won't create directories needed by this command. Here a little script to execute in the main dir that will do the job

all .coffee files will in the /coffee dir copying the .js files hierarchy

for FILE in `find . -name "*.coffee"`
do 
    ditto .${FILE/./} coffee${FILE/./}    
done

execute this after you converted your files to .coffee

UPDATE

you can swap ditto or rsync with mv after the first run to move the files since mv like cp does not create sub dirs.

UPDATE 2

added an one liner for those on time, see my second answer below!

UPDATE 3

added an option to exclude ./node_modules directory from conversion, for those who don't want to convert their dependencies

like image 197
Inoperable Avatar answered Oct 31 '22 09:10

Inoperable