Converting an entire project to CoffeeScript using js2coffee

Problem

js2coffe only works with I / O redirection with a single file . to transform an entire project, and its directory structure can be a real pain.

Want to convert your default ExpressJS application design, including. is this a directory structure?

Check out the script below

+7
source share
2 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 

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

+23
source

For those who are for a short time - use this great airliner! It starts in the current directory. / And goes through subdirs shredding each .js file to .coffee

find . -type f -name '*.js' | while read f; do echo "grinding $f to ${f/.js/.coffee} "; js2coffee "$f" > "${f/.js/.coffee}"; done

Want to exclude ./node_modules directory from conversion? Instead, use the one below:

find . -path ./node_modules -prune -o -type f -name '*.js' | while read f; do echo "grinding $f to ${f/.js/.coffee} "; js2coffee "$f" > "${f/.js/.coffee}"; done

small tip: you can use this loop to move or copy files: replace the js2coffee command with what you need to do. find output (list of found files) can simply be changed and filtered.

In bash, extracting (or replacing) substrings is simple - as in the above example:

IMPORTANT (long version) bash variables are accessible by adding $ in front of the variable name - BUT they are first defined without $ , so for your mental health remember this and check your code for the syntax $ var / var = if you get some non-logical errors . I repeat this error over and over without doing anything in Bash for a while, so I thought it would be wise to share this simple truth (it may also be me, being a lousy developer)

IMPORTANT (short version) definition of a variable: var_name=value access to the variable: $var_name=value , bad idea: $var_name=value , later accessed by $var_name .

In the example, the variable f is accessed by $ f , accessed by wth $ {f} , we replace .js/.js (/ is seperator) s . coffee/.coffee as follows: ${f/.js/.coffee} .

 apple="green"; echo $apple; 

outputs:

green

+9
source

All Articles