UNPKG

tsc-transpile-only

Version:
96 lines 4.68 kB
#!/usr/bin/env node "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var ts = tslib_1.__importStar(require("typescript")); function main() { // Parse command line var commandLine = ts.sys.args; var tempCompilerHost1 = ts.createCompilerHost({}); var parsedCommandLine = ts.parseCommandLine(commandLine, tempCompilerHost1.readFile); handleConfigParsingErrors(parsedCommandLine, tempCompilerHost1, parsedCommandLine); ['build', 'init', 'version', 'help', 'all'].forEach(function (flag) { if (parsedCommandLine.options[flag]) { console.error("--" + flag + " option not supported."); ts.sys.exit(1); } }); // Parse config file, informed by command-line flags var tempCompilerHost2 = ts.createCompilerHost(parsedCommandLine.options, false); // from here https://github.com/Microsoft/TypeScript/blob/6fb0f6818ad48bf4f685e86c03405ddc84b530ed/src/compiler/program.ts#L2812 var configParsingHost = { fileExists: function (f) { return tempCompilerHost2.fileExists(f); }, readDirectory: function (root, extensions, includes, depth) { return tempCompilerHost2.readDirectory ? tempCompilerHost2.readDirectory(root, extensions, includes, depth) : []; }, readFile: function (f) { return tempCompilerHost2.readFile(f); }, useCaseSensitiveFileNames: tempCompilerHost2.useCaseSensitiveFileNames(), getCurrentDirectory: function () { return tempCompilerHost2.getCurrentDirectory(); }, onUnRecoverableConfigFileDiagnostic: function () { return undefined; } }; var configFilePath = ts.findConfigFile(parsedCommandLine.options.project || configParsingHost.getCurrentDirectory(), configParsingHost.fileExists, parsedCommandLine.options.project); var parsedConfig = parsedCommandLine; if (configFilePath) { parsedConfig = ts.getParsedCommandLineOfConfigFile(configFilePath, parsedCommandLine.options, tslib_1.__assign({}, configParsingHost, { onUnRecoverableConfigFileDiagnostic: function (d) { handleDiagnostics([d], tempCompilerHost2, parsedCommandLine); } })); handleConfigParsingErrors(parsedConfig, tempCompilerHost2, parsedCommandLine); } if (parsedConfig) { var compilerHost = ts.createCompilerHost(parsedConfig.options); var program = ts.createProgram({ options: parsedConfig.options, configFileParsingDiagnostics: parsedConfig.errors, rootNames: parsedConfig.fileNames, host: compilerHost }); var diagnostics = program.getSyntacticDiagnostics(); handleDiagnostics(diagnostics, compilerHost, parsedConfig); var result = program.emit(); handleDiagnostics(result.diagnostics, compilerHost, parsedConfig); // console.dir(result.emitSkipped); // console.dir(result.emittedFiles); } } function formatDiagnostics(d, host, config) { if (shouldBePretty(config.options)) { return ts.formatDiagnosticsWithColorAndContext(d, { getCanonicalFileName: function (fileName) { return host.getCanonicalFileName(fileName); }, getCurrentDirectory: function () { return host.getCurrentDirectory(); }, getNewLine: function () { return host.getNewLine(); } }); } else { return ts.formatDiagnostics(d, { getCanonicalFileName: function (fileName) { return host.getCanonicalFileName(fileName); }, getCurrentDirectory: function () { return host.getCurrentDirectory(); }, getNewLine: function () { return host.getNewLine(); } }); } } // from https://github.com/Microsoft/TypeScript/blob/6fb0f6818ad48bf4f685e86c03405ddc84b530ed/src/tsc/tsc.ts function shouldBePretty(options) { if (!options || typeof options.pretty === "undefined") { return defaultIsPretty(); } return options.pretty; function defaultIsPretty() { return !!ts.sys.writeOutputIsTTY && ts.sys.writeOutputIsTTY(); } } function handleDiagnostics(diagnostics, host, config) { if (diagnostics.length) { console.error(formatDiagnostics(diagnostics, host, config)); ts.sys.exit(1); } } function handleConfigParsingErrors(parsedCommandLine, host, config) { if (parsedCommandLine && parsedCommandLine.errors.length) { console.error(formatDiagnostics(parsedCommandLine.errors, host, config)); ts.sys.exit(1); } if (!parsedCommandLine) { console.error('Unknown error parsing config.'); ts.sys.exit(1); } } main(); //# sourceMappingURL=tsc-transpile-only.js.map