npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

typescript.api

v0.7.7

Published

A compiler as a service api enabling nodejs developers to programatically resolve, compile, reflect and run typescript 0.9 source files in memory.

Downloads

18

Readme

typescript.api

A compiler as a service api enabling nodejs developers to resolve, compile, reflect and run typescript 0.9 source files.

install

npm install typescript.api

compiler version

TypeScript 0.9 alpha

quick start

registering typescript extension

The following will register the *.ts extension with require(). When calls to require() are made to *.ts files, any source resolution and/or compilation errors will be written out to the console by default.

If resolution or compilation errors do exist, the call to require() will return an empty object.

require("typescript.api").register();

var program = require("./program.ts");

manual compilation

The following is an example of using the api to compile a source file named 'program.ts'.

The process will first resolve 'program.ts' and all its referenced sources files. The resolved sources (units) then checked prior to being sent to the compiler for compilation. Once compiled, the compilation is checked again for problems prior to being run.

var typescript = require("typescript.api");

// show diagnostic errors.
function show_diagnostics (units) {

	for(var n in units) {
	
		for(var m in units[n].diagnostics) {
		
			console.log( units[n].diagnostics[m].toString() );
		}
	}
}

typescript.resolve(['./program.ts'], function(units) {
	
	if(!typescript.check(units)) {
	
		show_diagnostics(units);
	}
	else {
		
		typescript.compile(units, function(compilation) {
			
			if(!typescript.check(compilation)) {
			
				show_diagnostics (compilation);
			}
			else
			{			
				typescript.run(compilation, null, function(context) {
				
					 // exports are available on the context...
				});
			}
		});
	}
});

reference

typescript.resolve (sources, callback)

Will resolve source units by traversing each source files reference element.

arguments

  • sources - A filename, or a array of filenames to resolve.
  • callback(units) - A callback with the resolved units.

example

The following will resolve 'program.ts' and log each referenced source file to the console.

var typescript = require("typescript.api");

typescript.resolve(["program.ts"], function(units) { 

	for(var n in units) {
	
		console.log( units[n].path );
		
		console.log( units[n].content );
		
		for(var m in units[n].references) {
		
			console.log( units[n].references[m] )
			
		}
	}
});

typescript.check (units)

Checks source units for diagnostic errors.

arguments

  • units - units to be checked.
  • returns - true if ok.

example

The following example will check if both a resolve() and compile() is successful.

var typescript = require("typescript.api");

typescript.resolve(["program.ts"], function(units) { 

	if(typescript.check (units)) {
		
		typescript.compile(units, function(compilation) {
		
			if( typescript.check (compilation) ) {
			
				typescript.run(compilation, null, function(context) {
					
				});
			}
		});
	}
});

typescript.create ( filename, code )

Will create a unit from the supplied filename and source code.

arguments

  • filename - A filename that other units can reference.
  • code - The source code for this unit.

example

The following will create a unit. and send to the compiler for compilation. The compilation is then run.

var typescript = require("typescript.api");

var unit = typescript.create("temp.ts", "console.log('hello world');");

typescript.compile([unit], function(compilation) {

	typescript.run(compilation, null, function(context) { 
		
		// will output hello world..
	});
	
});

typescript.compile ( units, callback )

Compiles source units.

arguments

  • units - An array of source units.
  • callback - A callback that passes the compiled output.

example

The following will first create and compile a unit, and compiled source is written to the console.

var typescript = require("typescript.api");

var unit = typescript.create("temp.ts", "var value:number = 123;");

typescript.compile([unit], function(compilation) {

	for(var n in compilation){
	
		console.log(compilation[n].content);
	}
});

typescript.reflect ( compilation, callback )

Reflects compilation AST and produces meta data about the modules, classes, methods and variables contained within the compilation.

arguments

  • units - The compilation to be reflected.
  • callback - A callback that passes the reflected metadata.

example

The following will resolve the source file 'program.ts', compile it, then reflect its meta data to the console as a JSON string.

var typescript = require("typescript.api");

typescript.resolve(['program.ts'], function(units){

	typescript.compile(units, function(compilation) {
		
		typescript.reflect(compilation, function(reflection) {
			
			var json = JSON.stringify(reflection, null, ' ');
			
			console.log(json);
		});
	});
});

typescript.run ( compilation, sandbox, callback )

Runs a compilation.

arguments

  • compilation - The compilation to be run.
  • sandbox - A sandbox. pass null to inherit the current sandbox.
  • callback - A callback that passes a context containing any exported variables and function.

example

The following will first create and compile a unit, then send it off for compilation.

var typescript = require("typescript.api");	

var unit = typescript.create("temp.ts", "export var value:number = 123;");

typescript.compile([unit], function(compilation) {

	typescript.run(compilation, null, function(context) { 
	
		console.log(context.value);
		
	});
});