UI for Zipcoin Blue

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const tslib_1 = require("tslib");
  4. const path = require("path");
  5. const chalk_1 = require("chalk");
  6. const guards_1 = require("../guards");
  7. const errors_1 = require("./errors");
  8. const shell_1 = require("./utils/shell");
  9. exports.ERROR_SHELL_COMMAND_NOT_FOUND = 'SHELL_COMMAND_NOT_FOUND';
  10. class Shell {
  11. constructor({ tasks, log, project }) {
  12. this.tasks = tasks;
  13. this.log = log;
  14. this.project = project;
  15. }
  16. run(command, args, _a) {
  17. var { showCommand = true, showError = true, fatalOnNotFound = true, fatalOnError = true, showExecution, showSpinner = true, truncateErrorOutput } = _a, crossSpawnOptions = tslib_1.__rest(_a, ["showCommand", "showError", "fatalOnNotFound", "fatalOnError", "showExecution", "showSpinner", "truncateErrorOutput"]);
  18. return tslib_1.__awaiter(this, void 0, void 0, function* () {
  19. const fullCmd = command + ' ' + (args.length > 0 ? args.map(a => a.includes(' ') ? `"${a}"` : a).join(' ') : '');
  20. const truncatedCmd = fullCmd.length > 80 ? fullCmd.substring(0, 80) + '...' : fullCmd;
  21. const options = Object.assign({}, crossSpawnOptions);
  22. if (showExecution) {
  23. options.stdoutPipe = this.log.stream;
  24. options.stderrPipe = this.log.stream;
  25. }
  26. if (!options.env) {
  27. options.env = {};
  28. }
  29. options.env.PATH = this.supplementPATH(process.env.PATH);
  30. if (showCommand) {
  31. if (this.log.shouldLog('info')) {
  32. this.log.msg(`> ${chalk_1.default.green(fullCmd)}`);
  33. }
  34. if (!showExecution && showSpinner) {
  35. // We use tasks on a short sentence such as this instead of the command
  36. // string above because the commands can get quite long, and then
  37. // inquirer dies. See
  38. // https://github.com/ionic-team/ionic-cli/issues/2649.
  39. this.tasks.next('Running command');
  40. }
  41. }
  42. try {
  43. try {
  44. const out = yield shell_1.runcmd(command, args, options);
  45. if (showExecution) {
  46. this.log.nl();
  47. }
  48. if (showCommand && !showExecution && showSpinner) {
  49. this.tasks.end();
  50. }
  51. return out;
  52. }
  53. catch (e) {
  54. if (e.code === 'ENOENT') {
  55. if (fatalOnNotFound) {
  56. throw new errors_1.FatalException(`Command not found: ${chalk_1.default.green(command)}`, 127);
  57. }
  58. else {
  59. throw exports.ERROR_SHELL_COMMAND_NOT_FOUND;
  60. }
  61. }
  62. if (!guards_1.isExitCodeException(e)) {
  63. throw e;
  64. }
  65. let err = e.message || '';
  66. if (truncateErrorOutput && err.length > truncateErrorOutput) {
  67. err = `${chalk_1.default.bold('(truncated)')} ... ` + err.substring(err.length - truncateErrorOutput);
  68. }
  69. const helpLine = showExecution ? '.\n' : (err ? `:\n\n${err}` : ' with no output.\n');
  70. const publicErrorMsg = `An error occurred while running ${chalk_1.default.green(truncatedCmd)} (exit code ${e.exitCode})` + helpLine;
  71. const privateErrorMsg = `Subprocess (${chalk_1.default.green(command)}) encountered an error (exit code ${e.exitCode}).`;
  72. if (fatalOnError) {
  73. if (showError) {
  74. throw new errors_1.FatalException(publicErrorMsg, e.exitCode);
  75. }
  76. else {
  77. throw new errors_1.FatalException(privateErrorMsg, e.exitCode);
  78. }
  79. }
  80. else {
  81. if (showError) {
  82. this.log.error(publicErrorMsg);
  83. }
  84. }
  85. throw e;
  86. }
  87. }
  88. catch (e) {
  89. if (showCommand && !showExecution && showSpinner) {
  90. this.tasks.fail();
  91. }
  92. throw e;
  93. }
  94. });
  95. }
  96. cmdinfo(cmd, args = []) {
  97. return tslib_1.__awaiter(this, void 0, void 0, function* () {
  98. try {
  99. const out = yield shell_1.runcmd(cmd, args, { env: { PATH: this.supplementPATH(process.env.PATH) } });
  100. return out.split('\n').join(' ');
  101. }
  102. catch (e) { }
  103. });
  104. }
  105. supplementPATH(p) {
  106. return this.project.directory ? `${path.resolve(this.project.directory, 'node_modules', '.bin')}${path.delimiter}${p}` : p;
  107. }
  108. }
  109. exports.Shell = Shell;