TypeScript/tests/cases/compiler/APISample_compile.ts

50 lines
1.6 KiB
TypeScript
Raw Normal View History

2015-01-29 20:23:02 +01:00
// @module: commonjs
// @skipLibCheck: true
2018-06-12 21:52:06 +02:00
// @includebuiltfile: typescriptServices.d.ts
// @noImplicitAny:true
// @strictNullChecks:true
2015-01-29 20:23:02 +01:00
2018-06-12 21:52:06 +02:00
// @filename: node_modules/typescript/index.d.ts
declare module "typescript" {
export = ts;
}
// @filename: APISample_compile.ts
2015-01-29 20:23:02 +01:00
/*
* Note: This test is a public API sample. The sample sources can be found
2018-06-12 21:52:06 +02:00
* at: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#a-minimal-compiler
2015-01-29 20:23:02 +01:00
* Please log a "breaking change" issue for any API breaking change affecting this issue
*/
declare var process: any;
declare var console: any;
declare var os: any;
2015-01-29 20:23:02 +01:00
import ts = require("typescript");
export function compile(fileNames: string[], options: ts.CompilerOptions): void {
var program = ts.createProgram(fileNames, options);
var emitResult = program.emit();
2015-01-29 20:23:02 +01:00
2016-02-17 07:01:28 +01:00
var allDiagnostics = ts.getPreEmitDiagnostics(program);
2015-01-29 20:23:02 +01:00
allDiagnostics.forEach(diagnostic => {
var message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n');
if (!diagnostic.file) {
console.log(message);
return;
}
var { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start!);
console.log(`${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`);
2015-01-29 20:23:02 +01:00
});
var exitCode = emitResult.emitSkipped ? 1 : 0;
console.log(`Process exiting with code '${exitCode}'.`);
process.exit(exitCode);
2015-01-29 20:23:02 +01:00
}
compile(process.argv.slice(2), {
noEmitOnError: true, noImplicitAny: true,
target: ts.ScriptTarget.ES5, module: ts.ModuleKind.CommonJS
});