将多个Coffeescript文件合并到一个文件中?(多个子目录)

dMi*_*Mix 10 terminal coffeescript

我有一堆.coffee文件,我需要加入一个文件.

我有像rails应用程序设置的文件夹:

/src/controller/log_controller.coffee
/src/model/log.coffee
/src/views/logs/new.coffee
Run Code Online (Sandbox Code Playgroud)

Coffeescript有一个命令,允许您将多个coffeescripts连接到一个文件中,但它似乎只能用于一个目录.例如,这工作正常:

coffee --output app/controllers.js --join --compile src/controllers/*.coffee
Run Code Online (Sandbox Code Playgroud)

但我需要能够包含一堆类似于这个非工作命令的子目录:

coffee --output app/all.js --join --compile src/*/*.coffee
Run Code Online (Sandbox Code Playgroud)

有没有办法做到这一点?是否有UNIX方法传递子目录中的所有文件的列表?

我在OSX中使用终端.

它们都必须加在一个文件中,因为否则每个单独的文件都会被编译和包装:

(function() { }).call(this);
Run Code Online (Sandbox Code Playgroud)

这打破了一些函数调用的范围.

ben*_*tah 28

CoffeeScript文档:

-j, - join [FILE]:在编译之前,按照传递的顺序将所有脚本连接在一起,并将它们写入指定的文件.用于构建大型项目.

所以,你可以在命令行中实现你的目标(我使用bash),如下所示:

coffee -cj path/to/compiled/file.js file1 file2 file3 file4
Run Code Online (Sandbox Code Playgroud)

其中file1 - fileN是您要编译的coffeescript文件的路径.


Dar*_*ton 6

您可以先编写一个shell脚本或Rake任务将它们组合在一起,然后进行编译.就像是:

find . -type f -name '*.coffee' -print0 | xargs -0 cat > output.coffee

然后编译 output.coffee

根据需要调整路径.还要确保output.coffee文件不在您正在搜索的相同路径中,find否则您将进入无限循环.

http://man.cx/find | http://www.rubyrake.org/tutorial/index.html

另外,您可能对Stackoverflow上有关搜索目录的其他帖子感兴趣: