bin.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #!/usr/bin/env node
  2. "use strict";
  3. Object.defineProperty(exports, "__esModule", { value: true });
  4. var path_1 = require("path");
  5. var repl_1 = require("repl");
  6. var util_1 = require("util");
  7. var arrify = require("arrify");
  8. var Module = require("module");
  9. var minimist = require("minimist");
  10. var chalk_1 = require("chalk");
  11. var diff_1 = require("diff");
  12. var vm_1 = require("vm");
  13. var fs_1 = require("fs");
  14. var index_1 = require("./index");
  15. var argv = minimist(process.argv.slice(2), {
  16. stopEarly: true,
  17. string: ['eval', 'print', 'compiler', 'project', 'ignoreDiagnostics', 'require', 'cacheDirectory', 'ignore'],
  18. boolean: ['help', 'typeCheck', 'version', 'cache', 'skipProject', 'skipIgnore'],
  19. alias: {
  20. eval: ['e'],
  21. print: ['p'],
  22. require: ['r'],
  23. help: ['h'],
  24. version: ['v'],
  25. typeCheck: ['type-check'],
  26. cacheDirectory: ['cache-directory'],
  27. ignore: ['I'],
  28. project: ['P'],
  29. skipIgnore: ['skip-ignore'],
  30. skipProject: ['skip-project'],
  31. compiler: ['C'],
  32. ignoreDiagnostics: ['D', 'ignore-diagnostics'],
  33. compilerOptions: ['O', 'compiler-options']
  34. },
  35. default: {
  36. cache: index_1.DEFAULTS.cache,
  37. typeCheck: index_1.DEFAULTS.typeCheck,
  38. skipIgnore: index_1.DEFAULTS.skipIgnore,
  39. skipProject: index_1.DEFAULTS.skipProject
  40. }
  41. });
  42. if (argv.help) {
  43. console.log("\nUsage: ts-node [options] [ -e script | script.ts ] [arguments]\n\nOptions:\n\n -e, --eval [code] Evaluate code\n -p, --print [code] Evaluate code and print result\n -r, --require [path] Require a node module before execution\n\n -h, --help Print CLI usage\n -v, --version Print module version information\n\n --type-check Enable type checking through CLI\n --cache-directory Configure the output file cache directory\n -I, --ignore [pattern] Override the path patterns to skip compilation\n -P, --project [path] Path to TypeScript JSON project file\n -C, --compiler [name] Specify a custom TypeScript compiler\n -D, --ignoreDiagnostics [code] Ignore TypeScript warnings by diagnostic code\n -O, --compilerOptions [opts] JSON object to merge with compiler options\n\n --no-cache Disable the local TypeScript Node cache\n --skip-project Skip project config resolution and loading\n --skip-ignore Skip ignore checks\n");
  44. process.exit(0);
  45. }
  46. var cwd = process.cwd();
  47. var code = argv.eval === undefined ? argv.print : argv.eval;
  48. var isEval = typeof argv.eval === 'string' || !!argv.print;
  49. var isPrinted = argv.print !== undefined;
  50. var service = index_1.register({
  51. typeCheck: argv.typeCheck,
  52. cache: argv.cache,
  53. cacheDirectory: argv.cacheDirectory,
  54. ignore: argv.ignore,
  55. project: argv.project,
  56. skipIgnore: argv.skipIgnore,
  57. skipProject: argv.skipProject,
  58. compiler: argv.compiler,
  59. ignoreDiagnostics: argv.ignoreDiagnostics,
  60. compilerOptions: index_1.parse(argv.compilerOptions),
  61. readFile: isEval ? readFileEval : undefined,
  62. fileExists: isEval ? fileExistsEval : undefined
  63. });
  64. if (argv.version) {
  65. console.log("ts-node v" + index_1.VERSION);
  66. console.log("node " + process.version);
  67. console.log("typescript v" + service.ts.version);
  68. console.log("cache " + JSON.stringify(service.cachedir));
  69. process.exit(0);
  70. }
  71. Module._preloadModules(arrify(argv.require));
  72. var EVAL_FILENAME = "[eval].ts";
  73. var EVAL_PATH = path_1.join(cwd, EVAL_FILENAME);
  74. var EVAL_INSTANCE = { input: '', output: '', version: 0, lines: 0 };
  75. if (isEval) {
  76. evalAndExit(code, isPrinted);
  77. }
  78. else {
  79. if (argv._.length) {
  80. process.argv = ['node'].concat(path_1.resolve(cwd, argv._[0])).concat(argv._.slice(1));
  81. process.execArgv.unshift(__filename);
  82. Module.runMain();
  83. }
  84. else {
  85. if (process.stdin.isTTY) {
  86. startRepl();
  87. }
  88. else {
  89. var code_1 = '';
  90. process.stdin.on('data', function (chunk) { return code_1 += chunk; });
  91. process.stdin.on('end', function () { return evalAndExit(code_1, isPrinted); });
  92. }
  93. }
  94. }
  95. function evalAndExit(code, isPrinted) {
  96. var module = new Module(EVAL_FILENAME);
  97. module.filename = EVAL_FILENAME;
  98. module.paths = Module._nodeModulePaths(cwd);
  99. global.__filename = EVAL_FILENAME;
  100. global.__dirname = cwd;
  101. global.exports = module.exports;
  102. global.module = module;
  103. global.require = module.require.bind(module);
  104. var result;
  105. try {
  106. result = _eval(code, global);
  107. }
  108. catch (error) {
  109. if (error instanceof index_1.TSError) {
  110. console.error(index_1.printError(error));
  111. process.exit(1);
  112. }
  113. throw error;
  114. }
  115. if (isPrinted) {
  116. console.log(typeof result === 'string' ? result : util_1.inspect(result));
  117. }
  118. }
  119. function _eval(input, context) {
  120. var lines = EVAL_INSTANCE.lines;
  121. var isCompletion = !/\n$/.test(input);
  122. var undo = appendEval(input);
  123. var output;
  124. try {
  125. output = service.compile(EVAL_INSTANCE.input, EVAL_PATH, -lines);
  126. }
  127. catch (err) {
  128. undo();
  129. throw err;
  130. }
  131. var changes = diff_1.diffLines(EVAL_INSTANCE.output, output);
  132. if (isCompletion) {
  133. undo();
  134. }
  135. else {
  136. EVAL_INSTANCE.output = output;
  137. }
  138. return changes.reduce(function (result, change) {
  139. return change.added ? exec(change.value, EVAL_FILENAME, context) : result;
  140. }, undefined);
  141. }
  142. function exec(code, filename, context) {
  143. var script = new vm_1.Script(code, { filename: filename });
  144. return script.runInNewContext(context);
  145. }
  146. function startRepl() {
  147. var repl = repl_1.start({
  148. prompt: '> ',
  149. input: process.stdin,
  150. output: process.stdout,
  151. eval: replEval,
  152. useGlobal: false
  153. });
  154. var resetEval = appendEval('');
  155. function reset() {
  156. resetEval();
  157. exec('exports = module.exports', EVAL_FILENAME, repl.context);
  158. }
  159. reset();
  160. repl.on('reset', reset);
  161. repl.defineCommand('type', {
  162. help: 'Check the type of a TypeScript identifier',
  163. action: function (identifier) {
  164. if (!identifier) {
  165. repl.displayPrompt();
  166. return;
  167. }
  168. var undo = appendEval(identifier);
  169. var _a = service.getTypeInfo(EVAL_INSTANCE.input, EVAL_PATH, EVAL_INSTANCE.input.length), name = _a.name, comment = _a.comment;
  170. undo();
  171. repl.outputStream.write(chalk_1.default.bold(name) + "\n" + (comment ? comment + "\n" : ''));
  172. repl.displayPrompt();
  173. }
  174. });
  175. }
  176. function replEval(code, context, _filename, callback) {
  177. var err;
  178. var result;
  179. if (code === '.scope') {
  180. callback();
  181. return;
  182. }
  183. try {
  184. result = _eval(code, context);
  185. }
  186. catch (error) {
  187. if (error instanceof index_1.TSError) {
  188. if (repl_1.Recoverable && isRecoverable(error)) {
  189. err = new repl_1.Recoverable(error);
  190. }
  191. else {
  192. err = index_1.printError(error);
  193. }
  194. }
  195. else {
  196. err = error;
  197. }
  198. }
  199. callback(err, result);
  200. }
  201. function appendEval(input) {
  202. var undoInput = EVAL_INSTANCE.input;
  203. var undoVersion = EVAL_INSTANCE.version;
  204. var undoOutput = EVAL_INSTANCE.output;
  205. var undoLines = EVAL_INSTANCE.lines;
  206. if (undoInput.charAt(undoInput.length - 1) === '\n' && /^\s*[\[\(\`]/.test(input) && !/;\s*$/.test(undoInput)) {
  207. EVAL_INSTANCE.input = EVAL_INSTANCE.input.slice(0, -1) + ";\n";
  208. }
  209. EVAL_INSTANCE.input += input;
  210. EVAL_INSTANCE.lines += lineCount(input);
  211. EVAL_INSTANCE.version++;
  212. return function () {
  213. EVAL_INSTANCE.input = undoInput;
  214. EVAL_INSTANCE.output = undoOutput;
  215. EVAL_INSTANCE.version = undoVersion;
  216. EVAL_INSTANCE.lines = undoLines;
  217. };
  218. }
  219. function lineCount(value) {
  220. var count = 0;
  221. for (var _i = 0, value_1 = value; _i < value_1.length; _i++) {
  222. var char = value_1[_i];
  223. if (char === '\n') {
  224. count++;
  225. }
  226. }
  227. return count;
  228. }
  229. function readFileEval(path) {
  230. if (path === EVAL_PATH)
  231. return EVAL_INSTANCE.input;
  232. try {
  233. return fs_1.readFileSync(path, 'utf8');
  234. }
  235. catch (err) { }
  236. }
  237. function fileExistsEval(path) {
  238. if (path === EVAL_PATH)
  239. return true;
  240. try {
  241. var stats = fs_1.statSync(path);
  242. return stats.isFile() || stats.isFIFO();
  243. }
  244. catch (err) {
  245. return false;
  246. }
  247. }
  248. var RECOVERY_CODES = [
  249. 1003,
  250. 1005,
  251. 1109,
  252. 1126,
  253. 1160,
  254. 1161
  255. ];
  256. function isRecoverable(error) {
  257. return error.diagnostics.every(function (x) { return RECOVERY_CODES.indexOf(x.code) > -1; });
  258. }
  259. //# sourceMappingURL=bin.js.map