Skip to content

Instantly share code, notes, and snippets.

@koriroys
Last active January 17, 2020 11:20
Show Gist options
  • Save koriroys/efc6123a6a471082f313 to your computer and use it in GitHub Desktop.
Save koriroys/efc6123a6a471082f313 to your computer and use it in GitHub Desktop.
convert all coffeescript files to javascript

find filename.js.coffee and move to filename.coffee

example: projects.js.coffee -> projects.coffee

$ for i in $(find . -name *.js.coffee); do mv ${i} ${i%.*.*}.coffee; done

install dependencies for coffee-asset-pipeline-comments:

$ npm install line-reader

convert single line comments to block comments (since coffeescript to js doesn't keep single line comments and we need them for the asset pipeline)

$ for i in $(find . -name "*.coffee"); do node coffee-asset-pipeline-comments.js ${i}; done

convert all coffeescript files to js

example: projects.coffee -> projects.js

$ for i in $(find . -name "*.coffee"); do coffee --no-header -cb ${i}; done

congrats, you should now have a caffeine free asset pipeline!

var fs = require('fs'),
os = require('os'),
lineReader = require('line-reader');
function help(){
console.log('Usage: node coffee-asset-pipeline-comments.js file1.coffee');
}
function compilefile(pathname, outdir){
var src = '';
lineReader.eachLine(pathname, function(line, last) {
// asset pipeline comments
if ( line.indexOf('#') === 0 && line.indexOf('=') === 1) {
line = line.replace('#', '');
line = '###' + line + ' ###';
}
src += line;
src += os.EOL;
if ( last ) {
fs.writeFile(pathname, src);
}
});
}
function main(){
if (process.argv.length <=2){
help();
return;
}
console.log('Compiling: '+ process.argv[2]);
compilefile(process.argv[2]);
}
main();
@macouella
Copy link

Thanks heaps for this man! Really useful for outdated rails 4 & ember projects!

@scald
Copy link

scald commented Dec 3, 2016

Thanks! I modified find to exclude node_modules $(find . -name ".coffee" -not -path "./node_modules/");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment