Join multiple Coffeescript files into one file? (Multiple subdirectories)

I’ve got a bunch of .coffee files that I need to join into one file.

I have folders set up like a rails app:


Coffeescript has a command that lets you join multiple coffeescripts into one file, but it only seems to work with one directory. For example this works fine:

coffee --output app/controllers.js --join --compile src/controllers/*.coffee

But I need to be able to include a bunch of subdirectories kind of like this non-working command:

coffee --output app/all.js --join --compile src/*/*.coffee

Is there a way to do this? Is there a UNIXy way to pass in a list of all the files in the subdirectories?

I’m using terminal in OSX.

They all have to be joined in one file because otherwise each separate file gets compiled & wrapped with this:

(function() { }).call(this);

Which breaks the scope of some function calls.

Linux – Join multiple CSV files into one

I’m trying to join together multiple results stored in CSV files, with the first column as the index. The problem is that I have a various number of files, and the index results can vary from file to

Organizing multiple CoffeeScript files

I am working on an implementation of a web service where we are writing our front end code in CoffeeScript. The problem I have stumbled on is while the project is growing functionality has to be separ

Multiple Files communication with coffeescript

When I create a new coffeescript file, I cannot access the code in the compiled code from another file because it gets wrapped in some function scope. For example: CoffeeScript: class ChatService cons

How to upload multiple files using one file input element

I’m trying to use one file input element to upload multiple files to Drive using html form. This seems to work only for one file, although the file picker allows selecting multiple files. Back in the

Include multiple JavaScript files in CoffeeScript

does CoffeeScript have someting that can say: include this, that file. so the JS files are spread and modular but a compiling brings them together.

Unix: Batch move files from multiple subdirectories up 1 level

How can I move files from several subdirectories up 1 level in one terminal command? File Structure: path/to/files/A/remove/image.png path/to/files/B/remove/image.png path/to/files/C/remove/image.png

join multiple files

I am using the standard join command to join two sorted files based on column1. The command is simple join file1 file2 > output_file. But how do I join 3 or more files using the same technique ? jo

how to join multiple 3 xmls in one file?

how to join multiple 3 xmls in one file? In XML FILE : 01.xml <xmlResponse> <Person> <FirstName>FirstName_1</FirstName> <LastName>LastName_1</LastName> </Person

Replacing a file into multiple folders/subdirectories

Is there a way in command prompt to take one file and copy it into another folder and it’s subdirectories based on it’s name? I have an image named 5.jpg that has been put in a sub-folder that is in e

Multiple output files from one input file

I’m trying to take this file input, which is a multi-file input. <input name=userfile[] type=file multiple=/> And after my files are selected, i want to put them into separate individual


You could write a shell script or Rake task to combine them together first, then compile. Something like:

find . -type f -name ‘*.coffee’ -print0 | xargs -0 cat >

Then compile

Adjust the paths to your needs. Also make sure that the file is not in the same path you’re searching with find or you will get into an infinite loop. |

Additionally you may be interested in these other posts on Stackoverflow concerning searching across directories:

Alternatively, you could use the –bare flag, compile to JavaScript, and then perhaps wrap the JS if necessary. But this would likely create problems; for instance, if you have one file with the code

i = 0
foo = -> i++

then there’s only one var i declaration in the resulting JavaScript, and i will be incremented. But if you moved the foo function declaration to another CoffeeScript file, then its i would live in the foo scope, and the outer i would be unaffected.

So concatenating the CoffeeScript is a wiser solution, but there’s still potential for confusion there; the order in which you concatenate your code is almost certainly going to matter. I strongly recommend modularizing your code instead.

I’ve just release an alpha release of CoffeeToaster, I think it may help you.

From the CoffeeScript documentation:

-j, –join [FILE] : Before compiling, concatenate all scripts together in the order they were passed, and write them into the specified file. Useful for building large projects.

So, you can achieve your goal at the command line (I use bash) like this:

coffee -cj path/to/compiled/file.js file1 file2 file3 file4

where file1 – fileN are the paths to the coffeescript files you want to compile.

Use cake to compile them all in one (or more) resulting .js file(s). Cakefile is used as configuration which controls in which order your coffee scripts are compiled – quite handy with bigger projects.

Cake is quite easy to install and setup, invoking cake from vim while you are editing your project is then simply

:!cake build

and you can refresh your browser and see results.

As I’m also busy to learn the best way of structuring the files and use coffeescript in combination with backbone and cake, I have created a small project on github to keep it as a reference for myself, maybe it will help you too around cake and some basic things. All compiled files are in www folder so that you can open them in your browser and all source files (except for cake configuration) are in src folder. In this example, all .coffee files are compiled and combined in one output .js file which is then included in html.

The most easy way to use coffee command line tool.

coffee –output public –join –compile app

app is my working directory holding multiple subdirectories and public is where ~output.js file will be placed. Easy to automate this process if writing app in nodejs

This helped me (-o output directory, -j join to project.js, -cw compile and watch coffeescript directory in full depth):

coffee -o web/js -j project.js -cw coffeescript