Skip to content

Instantly share code, notes, and snippets.

@maruf89
Last active January 17, 2017 15:48
Show Gist options
  • Save maruf89/6f7768b8680e81b84e5c to your computer and use it in GitHub Desktop.
Save maruf89/6f7768b8680e81b84e5c to your computer and use it in GitHub Desktop.
Compiles all coffeescript files into JS while preserving single line comments then it beautifies the compiled javascript before saving it alongside the coffeescript files.
"use strict";
var fs = require('fs'),
cs = require('coffee-script'),
os = require('os'),
glob = require('glob'),
beautify = require('js-beautify').js_beautify,
mkdirp = require('node-mkdirp');
var path = require('path');
var lineReader = require('line-reader');
/*
process.stdin.resume();
carrier.carry(process.stdin, function(line) {
console.log('got one line: ' + line);
});
*/
function help(){
console.log('Usage: xxx [ -d outdir ] file1.coffee [file2.coffee]');
console.log('Compile an entire directory recursively like:');
console.log('node coffee2js.js /path/to/scripts/**/*.coffee');
}
function compilefile(p, outdir, replacePrepend){
var outfile=path.dirname(p)+path.sep+path.basename(p,path.extname(p))+ '.js';
if (outdir){
if (replacePrepend) {
outfile = outfile.replace(replacePrepend, outdir);
} else {
outfile = outdir + path.sep + path.basename(outfile);
}
console.log('outfile='+ outfile);
console.log('-----');
}
var inblockcmts=false;
var src='';
var linenum=0;
lineReader.eachLine(p, function(line, last) {
var commentStartLine,
spaces,
commentStart = '###',
commentEnd = ' ' + commentStart;
linenum ++;
var before = line,
after = null;
//block comment always start from 0?
if (/^\s*#/.test(line)) {
// marke sure it doesn't end in 3 hashes already & it's not a comment start
if (!/###$/.test(line) &&
!/###*/.test(line)
) {
commentStartLine = line.indexOf('#');
spaces = (new Array(commentStartLine + 1)).join(' ');
line = spaces + commentStart + line.replace(/^\s*#*/, '') + commentEnd;
after = line
}
}
src += line;
src += os.EOL;
if(last) {
// or check if it's the last one
//for debugging purpose
//fs.writeFile(p+'.tmp.coffee', src);
var dst='';
try {
dst=cs.compile(src, {bare: true});
} catch (e){
console.log('/*****');
console.log(' error file: ' + p);
console.log('*****/');
console.log(e);
console.log('/*****');
console.log(' end-error ');
console.log('*****/');
}
var lines = dst.split(/\r\n|\r|\n/g);
var dst2='';
for (var i = 0; i < lines.length; i++){
line=lines[i];
//console.log('line='+line);
var x=line.indexOf('/*');
var y=line.indexOf('*/');
//console.log('x='+x+',y='+y);
if (x>=0 && y>0){
line = line.replace('/*', '//');
line =line.replace('*/', '');
dst2 += line;
dst2 += os.EOL;
line = line.trim();
if (line.length==0){
line = lines[++i];
line = line.trim();
if (line.length==0){
continue;
}
}
continue;
}
dst2 += line;
dst2 += os.EOL;
}
dst2 = beautify(dst2, {
indent_size: 4,
wrap_line_length: 85,
end_with_newline: true,
break_chained_methods: true
});
fs.writeFile(outfile, dst2);
}
});
}
function main(){
// print process.argv
if (process.argv.length <=2){
help();
return;
}
var i=2,
outdir,
replacePrepend;
// var outdir = '/Users/mac/git/coffee2js-comment';
if (process.argv[i]=== '-d'){
outdir=process.argv[++i];
console.log('outdir='+ outdir);
++i;
}
process.argv.slice(i).forEach(function (val, index, array) {
console.log('Compiling file '+ index + ': ' + val);
compilefile(val,outdir, replacePrepend);
});
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment