Last active
August 29, 2015 14:00
-
-
Save fdecampredon/74ab0d2f19137bbab19f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*jshint node:true*/ | |
var fs = require('fs'), | |
path = require('path'); | |
var walk = function(dir, done) { | |
var results = []; | |
fs.readdir(dir, function(err, list) { | |
if (err) return done(err); | |
var pending = list.length; | |
if (!pending) return done(null, results); | |
list.forEach(function(file) { | |
file = dir + '/' + file; | |
fs.stat(file, function(err, stat) { | |
if (stat && stat.isDirectory()) { | |
walk(file, function(err, res) { | |
results = results.concat(res); | |
if (!--pending) done(null, results); | |
}); | |
} else { | |
results.push(file); | |
if (!--pending) done(null, results); | |
} | |
}); | |
}); | |
}); | |
}; | |
var refPath = path.join(__dirname, './_references.ts'), | |
JavaToTypescriptJS = path.join(__dirname, './JavaToTypescript.js'); | |
function addReference(file, content) { | |
var indexComment = content.indexOf('*/') +2; | |
return content.slice(0, indexComment) + '\n\n/// <reference path="'+ path.relative(path.dirname(file), refPath) +'" />\n\n' + content.slice(indexComment); | |
} | |
function transformPackage(file, content) { | |
var indexPackage = content.indexOf('package '), | |
indexOfSemilicon = content.indexOf(';', indexPackage); | |
return ( | |
content.slice(0, indexPackage) + | |
'module ' + content.slice(indexPackage + 'package '.length, indexOfSemilicon) + ' {' + | |
content.slice(indexOfSemilicon + 1).replace(/\n/g, '\n ') + | |
'\n}' | |
); | |
} | |
function transformPublicClass(file, content) { | |
var indexClass= content.indexOf('public class'); | |
if (indexClass === -1) { | |
return content; | |
} | |
return ( | |
content.slice(0, indexClass) + | |
'export class' + content.slice(indexClass + 'public class'.length) | |
); | |
} | |
function transformArgList(match) { | |
match = match.slice(1, match.length -1); | |
var results = [], | |
currentargs = '', | |
genericCount = 0, | |
inType = false, | |
inName = false; | |
while (match.length) { | |
var char = match[0]; | |
match = match.slice(1); | |
if (genericCount === 0 && char === ',') { | |
results.push(currentargs.replace(/(\S+)\s+(\S+)/g, '$2: $1')); | |
currentargs = ''; | |
} else { | |
currentargs+= char; | |
} | |
if (char === '>') { | |
genericCount--; | |
} else if (char === '<') { | |
genericCount++; | |
} | |
} | |
if (currentargs) { | |
results.push(currentargs.replace(/(\S+)\s+(\S+)/g, '$2: $1')); | |
} | |
return '(' + results.join(',') + ')'; | |
} | |
function transformMethod(file, content) { | |
return content.replace( | |
/^(\s*)(public\s+|protected\s+|private\s+|final\s+)(static\s+)?(final\s+||abstract\s+)?([\w][\w_><, ]+)\s+([\w_]+)\s*(\(.*\))(\s*{\s*)?$/mg, | |
//'$1$2$3$6$7: $5$8', | |
function (match, $1, $2, $3, $4, $5, $6, $7, $8) { | |
$1 = $1 || ''; | |
$2 = $2 || ''; | |
$3 = $3 || ''; | |
$4 = $4 || ''; | |
$5 = $5 || ''; | |
$6 = $6 || ''; | |
$7 = $7 || ''; | |
$8 = $8 || ''; | |
return $1 + $2 + $3 + $6 + transformArgList($7) + ':' + $5 + $8; | |
} | |
); | |
} | |
function transformConstructor(file, content) { | |
var fileName = path.basename(file,'.ts'); | |
return content.replace( | |
new RegExp('^(\\s*)(public\\s+|protected\\s+|private\\s+)?'+ fileName +'\\s*(\\(.*\\))(\\s*{\\s*)?$','mg'), | |
//'$1constructor$3$4', | |
function (match, $1, $2, $3, $4) { | |
$1 = $1 || ''; | |
$2 = $2 || ''; | |
$3 = $3 || ''; | |
$4 = $4 || ''; | |
return $1 + 'constructor' + transformArgList($3) + $4; | |
} | |
); | |
} | |
function transformPropertiesDeclaration(file, content) { | |
return content.replace( | |
/^(\s*)(public\s+|protected\s+|private\s+|final\s+)(static\s+)?(final\s+||abstract\s+)?([\w][\w_><, ]+)\s+([\w_]+)(\s*=.*)?;(.*)$/mg, | |
'$1$2$3$6: $5$7;$8' | |
); | |
} | |
function transformVarDef(file, content) { | |
return content.replace( | |
/^(\s*)([A-Z][\w_><, ]+)\s+([\w_]+)(\s*=.*)?;(.*)$/mg, | |
'$1var $3: $2$4;$5' | |
); | |
} | |
function transformImportDecl(file, content) { | |
return content.replace( | |
/^(\s*)(import\s+)(static\s+)?(([\w*]+\.?)+);/mg, | |
function (match, $1 ,$2, $3, $4) { | |
$4 = $4.split('.'); | |
if ($4[$4.length - 1].indexOf('*') === 0) { | |
$4.pop(); | |
} | |
return $1 + $2 + $4[$4.length - 1] + ' = ' + $4.join('.'); | |
} | |
); | |
} | |
function transformBasicType(file, content) { | |
return content | |
.replace(/(\s+)(String|char)(\s+)/g, '$1string$3') | |
.replace(/(\s+)(byte|short|int|long|float|double)(\s+)/g, '$1number$3'); | |
} | |
function transformFile(file, content) { | |
return [ | |
addReference, | |
transformImportDecl, | |
transformPackage, | |
transformPublicClass, | |
transformMethod, | |
transformConstructor, | |
transformPropertiesDeclaration, | |
transformVarDef, | |
transformBasicType | |
].reduce(function (content, transformer) { | |
console.log(transformer.name); | |
return transformer(file, content); | |
}, content); | |
} | |
walk(__dirname, function (err, results) { | |
results.forEach(function (file) { | |
if (file !== refPath && file !== JavaToTypescriptJS) { | |
console.log('transforming :' + file); | |
var content = fs.readFileSync(file, 'utf-8'); | |
fs.writeFileSync(file, transformFile(file, content), 'utf-8'); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment