From within a node app I would like to do:
var typeScript = require(\'typescript\');
typeScript.compile(\'...\')
I\'m looking to implemen
Currently it is not possible to achieve a compilation just by having a require and calling compile. If you can look at the harness.ts there is a compiler module that provides a rather simple way of doing it but I would suggest you to call tsc externally.
///<reference path='node.d.ts'/>
import exec = module('child_process');
var child = exec.exec('tsc foo.ts',
function (error, stdout, stderr) {
if (error !== null) {
console.log('exec error: ' + error);
}
});
I believe this would do the job.
better-require could help you achieve this if all you want is executing/accessing the typescript file.
It lets you require() typescript files - no pre-compilation needed - and a bunch of other file formats (coffeescript, clojurescript, yaml, xml, etc.)
require('better-require')();
var myModule = require('./mymodule.ts');
Disclosure: I wrote better-require.
You could try out https://github.com/sinclairzx81/typescript.api. This project does the require() extension stuff, but also has some functionality to compile ts source manually. It should be possible to create a automated build system with it.
note tho its built on the typescript 0.9 compiler, so you may or may not have success compiling 0.8.3 source given various updates to the language.
Check this github project by niutech, it can convert TypeScript code to JS code on the fly in browser, but I guess it can be easily be modified to work in node.js.
This one is a bit hacky but it will work.
I thought about this very same just yesterday and I was checking their code. If you check bin/typscript.js from their sourcecode (It is a very very large file, with nearly 21k lines of code), you will see it creates TypeScript.TypeScriptCompiler, and then you will find that this DOES expose a way of compiling.
var compiler = new TypeScript.TypeScriptCompiler(outfile, errorfile,
new TypeScript.NullLogger(), settings);
Now, you need an easy way to expose it. To do this, you will have to modify their code, which is why this is hacky. To do this, you could modify typescript.js by adding:
module.exports = exports = TypeScript;
Right at the end of the file.
Then, you can create an index.js file in the root of the module (notice: install the module in a local scope for all of this: "npm install typescript"), which exposes the object.
exports.TypeScript = require("bin/typescript");
And ready! Now you can just call it and compile your code using it. You can check how to use the API for compilation in the tsc.js file.
I apologize in advance for the horrible code ahead:
var fs = require("fs");
var TypeScript = require("typescript");
var path = "test.ts";
var pathout = "test.js";
var content = fs.readFileSync(path, "utf-8");
var fd = fs.openSync(pathout, 'w');
var outFile = {
Write: function (str) {
fs.writeSync(fd, str);
},
WriteLine: function (str) {
console.log(fd, str);
fs.writeSync(fd, str + '\r\n');
},
Close: function () {
fs.closeSync(fd);
fd = null;
}
};
var createFile = function (path) {
function mkdirRecursiveSync(path) {
var stats = fs.statSync(path);
if(stats.isFile()) {
throw "\"" + path + "\" exists but isn't a directory.";
} else {
if(stats.isDirectory()) {
return;
} else {
mkdirRecursiveSync(_path.dirname(path));
fs.mkdirSync(path, 509);
}
}
}
mkdirRecursiveSync(_path.dirname(path));
console.log(path)
var fd = fs.openSync(path, 'w');
return {
Write: function (str) {
fs.writeSync(fd, str);
},
WriteLine: function (str) {
fs.writeSync(fd, str + '\r\n');
},
Close: function () {
fs.closeSync(fd);
fd = null;
}
};
};
var stderr = {
Write: function (str) {
process.stderr.write(str);
},
WriteLine: function (str) {
process.stderr.write(str + '\n');
},
Close: function () {
}
}
var compiler = new TypeScript.TypeScriptCompiler(outFile, outFile);
compiler.setErrorOutput(stderr);
compiler.addUnit(content, path);
compiler.typeCheck();
compiler.emit(false, createFile);
outFile.Close();
For some reason whoever wrote the code was a real fan of C# and proceeded to go ahead and use methods called WriteLine, Close and Write, which are in fact just wrappers. You could get this of the overhead of having to add this functions, but you would have to modify a lot of code in the module and it isn't worth it. I think it is best to have a class to extend (or if you are still on JS, inherit the prototype) and let it do that for you, to make it DRY.
Something really nice is that if you want to translate 500 TypeScript files and put them all into a single .js file, you can just call compiler.addUnit(anothercontent, anotherpath); 500 times and then see it all go into a single file :)
Focusing on better things: if you check tsc.js code, you will find a batch compiler class. If you want this for a build process, it might be better to use something more robust like it. It provides watching files and more.
Having browsed the code, I think I will just submit a ticket to the development team and ask them to provide a clearer API ¬¬
Note: All file reads in here are done in a synchronous way. This is bad, very bad, in terms of performance. I don't know exactly what you plan to do, but I couldn't recommend more that you find a way to make this async if possible.