index.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. "use strict";
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = Object.setPrototypeOf ||
  4. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  5. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  6. return function (d, b) {
  7. extendStatics(d, b);
  8. function __() { this.constructor = d; }
  9. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  10. };
  11. })();
  12. Object.defineProperty(exports, "__esModule", { value: true });
  13. var path_1 = require("path");
  14. var fs_1 = require("fs");
  15. var os_1 = require("os");
  16. var sourceMapSupport = require("source-map-support");
  17. var chalk_1 = require("chalk");
  18. var mkdirp = require("mkdirp");
  19. var crypto = require("crypto");
  20. var yn = require("yn");
  21. var arrify = require("arrify");
  22. var make_error_1 = require("make-error");
  23. var pkg = require('../package.json');
  24. var shouldDebug = yn(process.env.TS_NODE_DEBUG);
  25. var debug = shouldDebug ? console.log.bind(console, 'ts-node') : function () { return undefined; };
  26. var debugFn = shouldDebug ?
  27. function (key, fn) {
  28. return function (x) {
  29. debug(key, x);
  30. return fn(x);
  31. };
  32. } :
  33. function (_, fn) { return fn; };
  34. exports.VERSION = pkg.version;
  35. exports.DEFAULTS = {
  36. cache: yn(process.env['TS_NODE_CACHE'], { default: true }),
  37. cacheDirectory: process.env['TS_NODE_CACHE_DIRECTORY'],
  38. compiler: process.env['TS_NODE_COMPILER'],
  39. compilerOptions: parse(process.env['TS_NODE_COMPILER_OPTIONS']),
  40. ignore: split(process.env['TS_NODE_IGNORE']),
  41. project: process.env['TS_NODE_PROJECT'],
  42. skipIgnore: yn(process.env['TS_NODE_SKIP_IGNORE']),
  43. skipProject: yn(process.env['TS_NODE_SKIP_PROJECT']),
  44. ignoreDiagnostics: split(process.env['TS_NODE_IGNORE_DIAGNOSTICS']),
  45. typeCheck: yn(process.env['TS_NODE_TYPE_CHECK'])
  46. };
  47. var DEFAULT_COMPILER_OPTIONS = {
  48. sourceMap: true,
  49. inlineSourceMap: false,
  50. inlineSources: true,
  51. declaration: false,
  52. noEmit: false,
  53. outDir: '$$ts-node$$'
  54. };
  55. function split(value) {
  56. return typeof value === 'string' ? value.split(/ *, */g) : undefined;
  57. }
  58. exports.split = split;
  59. function parse(value) {
  60. return typeof value === 'string' ? JSON.parse(value) : undefined;
  61. }
  62. exports.parse = parse;
  63. function normalizeSlashes(value) {
  64. return value.replace(/\\/g, '/');
  65. }
  66. exports.normalizeSlashes = normalizeSlashes;
  67. var TSError = (function (_super) {
  68. __extends(TSError, _super);
  69. function TSError(diagnostics) {
  70. var _this = _super.call(this, "\u2A2F Unable to compile TypeScript\n" + diagnostics.map(function (x) { return x.message; }).join('\n')) || this;
  71. _this.diagnostics = diagnostics;
  72. _this.name = 'TSError';
  73. return _this;
  74. }
  75. return TSError;
  76. }(make_error_1.BaseError));
  77. exports.TSError = TSError;
  78. function getTmpDir() {
  79. var hash = crypto.createHash('sha256').update(os_1.homedir(), 'utf8').digest('hex');
  80. return path_1.join(os_1.tmpdir(), "ts-node-" + hash);
  81. }
  82. function register(opts) {
  83. if (opts === void 0) { opts = {}; }
  84. var options = Object.assign({}, exports.DEFAULTS, opts);
  85. var cacheDirectory = options.cacheDirectory || getTmpDir();
  86. var originalJsHandler = require.extensions['.js'];
  87. var ignoreDiagnostics = arrify(options.ignoreDiagnostics).concat([
  88. 6059,
  89. 18002,
  90. 18003
  91. ]).map(Number);
  92. var memoryCache = {
  93. contents: Object.create(null),
  94. versions: Object.create(null),
  95. outputs: Object.create(null)
  96. };
  97. var ignore = options.skipIgnore ? [] : arrify(options.ignore || '/node_modules/').map(function (str) { return new RegExp(str); });
  98. sourceMapSupport.install({
  99. environment: 'node',
  100. retrieveFile: function (path) {
  101. return memoryCache.outputs[path];
  102. }
  103. });
  104. var cwd = process.cwd();
  105. var compiler = options.compiler || 'typescript';
  106. var typeCheck = options.typeCheck || false;
  107. var ts = require(compiler);
  108. var transformers = options.transformers || undefined;
  109. var readFile = options.readFile || ts.sys.readFile;
  110. var fileExists = options.fileExists || ts.sys.fileExists;
  111. var config = readConfig(cwd, ts, options.compilerOptions, fileExists, readFile, options.project, options.skipProject);
  112. var configDiagnostics = filterDiagnostics(config.errors, ignoreDiagnostics);
  113. var extensions = ['.ts', '.tsx'];
  114. var cachedir = path_1.join(path_1.resolve(cwd, cacheDirectory), getCompilerDigest({ version: ts.version, typeCheck: typeCheck, ignoreDiagnostics: ignoreDiagnostics, config: config, compiler: compiler }));
  115. if (configDiagnostics.length) {
  116. throw new TSError(formatDiagnostics(configDiagnostics, cwd, ts, 0));
  117. }
  118. if (config.options.allowJs) {
  119. extensions.push('.js');
  120. extensions.push('.jsx');
  121. }
  122. for (var _i = 0, _a = config.fileNames; _i < _a.length; _i++) {
  123. var fileName = _a[_i];
  124. memoryCache.versions[fileName] = 1;
  125. }
  126. var getExtension = config.options.jsx === ts.JsxEmit.Preserve ?
  127. (function (path) { return /\.[tj]sx$/.test(path) ? '.jsx' : '.js'; }) :
  128. (function (_) { return '.js'; });
  129. var getOutput = function (code, fileName, lineOffset) {
  130. if (lineOffset === void 0) { lineOffset = 0; }
  131. var result = ts.transpileModule(code, {
  132. fileName: fileName,
  133. transformers: transformers,
  134. compilerOptions: config.options,
  135. reportDiagnostics: true
  136. });
  137. var diagnosticList = result.diagnostics ?
  138. filterDiagnostics(result.diagnostics, ignoreDiagnostics) :
  139. [];
  140. if (diagnosticList.length) {
  141. throw new TSError(formatDiagnostics(diagnosticList, cwd, ts, lineOffset));
  142. }
  143. return [result.outputText, result.sourceMapText];
  144. };
  145. var getTypeInfo = function (_code, _fileName, _position) {
  146. throw new TypeError("Type information is unavailable without \"--type-check\"");
  147. };
  148. if (typeCheck) {
  149. var updateMemoryCache_1 = function (code, fileName) {
  150. if (memoryCache.contents[fileName] !== code) {
  151. memoryCache.contents[fileName] = code;
  152. memoryCache.versions[fileName] = (memoryCache.versions[fileName] || 0) + 1;
  153. }
  154. };
  155. var serviceHost = {
  156. getScriptFileNames: function () { return Object.keys(memoryCache.versions); },
  157. getScriptVersion: function (fileName) {
  158. var version = memoryCache.versions[fileName];
  159. return version === undefined ? undefined : String(version);
  160. },
  161. getScriptSnapshot: function (fileName) {
  162. if (!memoryCache.contents[fileName]) {
  163. var contents = readFile(fileName);
  164. if (!contents)
  165. return;
  166. memoryCache.contents[fileName] = contents;
  167. }
  168. return ts.ScriptSnapshot.fromString(memoryCache.contents[fileName]);
  169. },
  170. fileExists: debugFn('fileExists', fileExists),
  171. readFile: debugFn('getFile', readFile),
  172. readDirectory: debugFn('readDirectory', ts.sys.readDirectory),
  173. getDirectories: debugFn('getDirectories', ts.sys.getDirectories),
  174. directoryExists: debugFn('directoryExists', ts.sys.directoryExists),
  175. getNewLine: function () { return os_1.EOL; },
  176. getCurrentDirectory: function () { return cwd; },
  177. getCompilationSettings: function () { return config.options; },
  178. getDefaultLibFileName: function () { return ts.getDefaultLibFilePath(config.options); },
  179. getCustomTransformers: function () { return transformers; }
  180. };
  181. var service_1 = ts.createLanguageService(serviceHost);
  182. getOutput = function (code, fileName, lineOffset) {
  183. if (lineOffset === void 0) { lineOffset = 0; }
  184. updateMemoryCache_1(code, fileName);
  185. var output = service_1.getEmitOutput(fileName);
  186. var diagnostics = service_1.getCompilerOptionsDiagnostics()
  187. .concat(service_1.getSyntacticDiagnostics(fileName))
  188. .concat(service_1.getSemanticDiagnostics(fileName));
  189. var diagnosticList = filterDiagnostics(diagnostics, ignoreDiagnostics);
  190. if (diagnosticList.length) {
  191. throw new TSError(formatDiagnostics(diagnosticList, cwd, ts, lineOffset));
  192. }
  193. if (output.emitSkipped) {
  194. throw new TypeError(path_1.relative(cwd, fileName) + ": Emit skipped");
  195. }
  196. if (output.outputFiles.length === 0) {
  197. throw new TypeError('Unable to require `.d.ts` file.\n' +
  198. 'This is usually the result of a faulty configuration or import. ' +
  199. 'Make sure there is a `.js`, `.json` or another executable extension and ' +
  200. 'loader (attached before `ts-node`) available alongside ' +
  201. ("`" + path_1.basename(fileName) + "`."));
  202. }
  203. return [output.outputFiles[1].text, output.outputFiles[0].text];
  204. };
  205. getTypeInfo = function (code, fileName, position) {
  206. updateMemoryCache_1(code, fileName);
  207. var info = service_1.getQuickInfoAtPosition(fileName, position);
  208. var name = ts.displayPartsToString(info ? info.displayParts : []);
  209. var comment = ts.displayPartsToString(info ? info.documentation : []);
  210. return { name: name, comment: comment };
  211. };
  212. }
  213. var compile = readThrough(cachedir, options.cache === true, memoryCache, getOutput, getExtension);
  214. var register = { cwd: cwd, compile: compile, getTypeInfo: getTypeInfo, extensions: extensions, cachedir: cachedir, ts: ts };
  215. extensions.forEach(function (extension) {
  216. registerExtension(extension, ignore, register, originalJsHandler);
  217. });
  218. return register;
  219. }
  220. exports.register = register;
  221. function shouldIgnore(filename, ignore) {
  222. var relname = normalizeSlashes(filename);
  223. return ignore.some(function (x) { return x.test(relname); });
  224. }
  225. function registerExtension(ext, ignore, register, originalHandler) {
  226. var old = require.extensions[ext] || originalHandler;
  227. require.extensions[ext] = function (m, filename) {
  228. if (shouldIgnore(filename, ignore)) {
  229. return old(m, filename);
  230. }
  231. var _compile = m._compile;
  232. m._compile = function (code, fileName) {
  233. debug('module._compile', fileName);
  234. return _compile.call(this, register.compile(code, fileName), fileName);
  235. };
  236. return old(m, filename);
  237. };
  238. }
  239. function fixConfig(ts, config) {
  240. delete config.options.out;
  241. delete config.options.outFile;
  242. delete config.options.declarationDir;
  243. if (config.options.target === undefined) {
  244. config.options.target = ts.ScriptTarget.ES5;
  245. }
  246. if (config.options.module === undefined) {
  247. config.options.module = ts.ModuleKind.CommonJS;
  248. }
  249. return config;
  250. }
  251. function readConfig(cwd, ts, compilerOptions, fileExists, readFile, project, noProject) {
  252. var config = { compilerOptions: {} };
  253. var basePath = cwd;
  254. var configFileName = undefined;
  255. if (!noProject) {
  256. configFileName = project ? path_1.resolve(cwd, project) : ts.findConfigFile(cwd, fileExists);
  257. if (configFileName) {
  258. var result = ts.readConfigFile(configFileName, readFile);
  259. if (result.error) {
  260. throw new TSError([formatDiagnostic(result.error, cwd, ts, 0)]);
  261. }
  262. config = result.config;
  263. basePath = normalizeSlashes(path_1.dirname(configFileName));
  264. }
  265. }
  266. config.compilerOptions = Object.assign({}, config.compilerOptions, compilerOptions, DEFAULT_COMPILER_OPTIONS);
  267. return fixConfig(ts, ts.parseJsonConfigFileContent(config, ts.sys, basePath, undefined, configFileName));
  268. }
  269. function readThrough(cachedir, shouldCache, memoryCache, compile, getExtension) {
  270. if (shouldCache === false) {
  271. return function (code, fileName, lineOffset) {
  272. debug('readThrough', fileName);
  273. var _a = compile(code, fileName, lineOffset), value = _a[0], sourceMap = _a[1];
  274. var output = updateOutput(value, fileName, sourceMap, getExtension);
  275. memoryCache.outputs[fileName] = output;
  276. return output;
  277. };
  278. }
  279. mkdirp.sync(cachedir);
  280. return function (code, fileName, lineOffset) {
  281. debug('readThrough', fileName);
  282. var cachePath = path_1.join(cachedir, getCacheName(code, fileName));
  283. var extension = getExtension(fileName);
  284. var outputPath = "" + cachePath + extension;
  285. try {
  286. var output_1 = fs_1.readFileSync(outputPath, 'utf8');
  287. if (isValidCacheContent(output_1)) {
  288. memoryCache.outputs[fileName] = output_1;
  289. return output_1;
  290. }
  291. }
  292. catch (err) { }
  293. var _a = compile(code, fileName, lineOffset), value = _a[0], sourceMap = _a[1];
  294. var output = updateOutput(value, fileName, sourceMap, getExtension);
  295. memoryCache.outputs[fileName] = output;
  296. fs_1.writeFileSync(outputPath, output);
  297. return output;
  298. };
  299. }
  300. function updateOutput(outputText, fileName, sourceMap, getExtension) {
  301. var base64Map = new Buffer(updateSourceMap(sourceMap, fileName), 'utf8').toString('base64');
  302. var sourceMapContent = "data:application/json;charset=utf-8;base64," + base64Map;
  303. var sourceMapLength = (path_1.basename(fileName) + ".map").length + (getExtension(fileName).length - path_1.extname(fileName).length);
  304. return outputText.slice(0, -sourceMapLength) + sourceMapContent;
  305. }
  306. function updateSourceMap(sourceMapText, fileName) {
  307. var sourceMap = JSON.parse(sourceMapText);
  308. sourceMap.file = fileName;
  309. sourceMap.sources = [fileName];
  310. delete sourceMap.sourceRoot;
  311. return JSON.stringify(sourceMap);
  312. }
  313. function getCacheName(sourceCode, fileName) {
  314. return crypto.createHash('sha256')
  315. .update(path_1.extname(fileName), 'utf8')
  316. .update('\x001\x00', 'utf8')
  317. .update(sourceCode, 'utf8')
  318. .digest('hex');
  319. }
  320. function isValidCacheContent(contents) {
  321. return /(?:9|0=|Q==)$/.test(contents.slice(-3));
  322. }
  323. function getCompilerDigest(obj) {
  324. return crypto.createHash('sha256').update(JSON.stringify(obj), 'utf8').digest('hex');
  325. }
  326. function filterDiagnostics(diagnostics, ignore) {
  327. return diagnostics.filter(function (x) { return ignore.indexOf(x.code) === -1; });
  328. }
  329. function formatDiagnostics(diagnostics, cwd, ts, lineOffset) {
  330. return diagnostics.map(function (x) { return formatDiagnostic(x, cwd, ts, lineOffset); });
  331. }
  332. exports.formatDiagnostics = formatDiagnostics;
  333. function formatDiagnostic(diagnostic, cwd, ts, lineOffset) {
  334. var messageText = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n');
  335. var code = diagnostic.code;
  336. if (diagnostic.file) {
  337. var path = path_1.relative(cwd, diagnostic.file.fileName);
  338. if (diagnostic.start) {
  339. var _a = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start), line = _a.line, character = _a.character;
  340. var message = path + " (" + (line + 1 + lineOffset) + "," + (character + 1) + "): " + messageText + " (" + code + ")";
  341. return { message: message, code: code };
  342. }
  343. return { message: path + ": " + messageText + " (" + code + ")", code: code };
  344. }
  345. return { message: messageText + " (" + code + ")", code: code };
  346. }
  347. exports.formatDiagnostic = formatDiagnostic;
  348. function printError(error) {
  349. var title = chalk_1.default.red('⨯') + " Unable to compile TypeScript";
  350. return chalk_1.default.bold(title) + "\n" + error.diagnostics.map(function (x) { return x.message; }).join('\n');
  351. }
  352. exports.printError = printError;
  353. //# sourceMappingURL=index.js.map