61 lines
2.5 KiB
TypeScript
61 lines
2.5 KiB
TypeScript
//// [APISample_compile.ts]
|
|
|
|
/*
|
|
* Note: This test is a public API sample. The sample sources can be found
|
|
at: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#a-minimal-compiler
|
|
* 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;
|
|
|
|
import ts = require("typescript");
|
|
|
|
export function compile(fileNames: string[], options: ts.CompilerOptions): void {
|
|
var program = ts.createProgram(fileNames, options);
|
|
var emitResult = program.emit();
|
|
|
|
var allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
|
|
|
|
allDiagnostics.forEach(diagnostic => {
|
|
var { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
|
|
var message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n');
|
|
console.log(`${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`);
|
|
});
|
|
|
|
var exitCode = emitResult.emitSkipped ? 1 : 0;
|
|
console.log(`Process exiting with code '${exitCode}'.`);
|
|
process.exit(exitCode);
|
|
}
|
|
|
|
compile(process.argv.slice(2), {
|
|
noEmitOnError: true, noImplicitAny: true,
|
|
target: ts.ScriptTarget.ES5, module: ts.ModuleKind.CommonJS
|
|
});
|
|
|
|
//// [APISample_compile.js]
|
|
/*
|
|
* Note: This test is a public API sample. The sample sources can be found
|
|
at: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#a-minimal-compiler
|
|
* Please log a "breaking change" issue for any API breaking change affecting this issue
|
|
*/
|
|
var ts = require("typescript");
|
|
function compile(fileNames, options) {
|
|
var program = ts.createProgram(fileNames, options);
|
|
var emitResult = program.emit();
|
|
var allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
|
|
allDiagnostics.forEach(function (diagnostic) {
|
|
var _a = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start), line = _a.line, character = _a.character;
|
|
var message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n');
|
|
console.log(diagnostic.file.fileName + " (" + (line + 1) + "," + (character + 1) + "): " + message);
|
|
});
|
|
var exitCode = emitResult.emitSkipped ? 1 : 0;
|
|
console.log("Process exiting with code '" + exitCode + "'.");
|
|
process.exit(exitCode);
|
|
}
|
|
exports.compile = compile;
|
|
compile(process.argv.slice(2), {
|
|
noEmitOnError: true, noImplicitAny: true,
|
|
target: 1 /* ES5 */, module: 1 /* CommonJS */
|
|
});
|