0

I'm trying to do a slightly different thing than the example on the TypeScript compiler API wiki:

var host = ts.createCompilerHost(options);
var program = ts.createProgram(filenames, options, host);
var checker = ts.createTypeChecker(program, /*produceDiagnostics*/ true);
var result = checker.emitFiles();

The question is twofold:

1) how to make emitFiles to write to a specific path?

2) how to catch and handle compilation errors?

1 Answer 1

3

how to make emitFiles to write to a specific path

The emitFiles doesn't do any actual writing. It returns it as a result and you need to write it, so you can write it anywhere.

e.g.

output.outputFiles.forEach(o => {
    mkdirp.sync(path.dirname(o.name));
    fs.writeFileSync(o.name, o.text, "utf8");
});

how to catch and handle compilation errors?

Using diagnostics:

var allDiagnostics = services.getCompilerOptionsDiagnostics()
    .concat(services.getSyntacticDiagnostics(filePath))
    .concat(services.getSemanticDiagnostics(filePath));

Source : https://github.com/TypeStrong/atom-typescript/blob/master/lib/main/lang/modules/building.ts#L28-L30 don't forget to star ;)

Sign up to request clarification or add additional context in comments.

4 Comments

hey, that's cool! how did you know? are there any docs of the compiler APIs or you just read the sources?
Sources and experimentation e.g. mkdirp was added by someone else. You can see code in this package and the brackets one as a reference for how stuff works
on a closer look those are atom-typescript objects and methods, not typescripts. I can't use those.
services is just the LanguageService documented here: github.com/Microsoft/TypeScript/wiki/… Not AtomTS specific in any way

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.