UI for Zipcoin Blue

npm.js 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 shell_1 = require("../shell");
  8. const fs_1 = require("@ionic/cli-framework/utils/fs");
  9. const npm_1 = require("@ionic/cli-framework/utils/npm");
  10. /**
  11. * To be used with a module path resolved from require.resolve().
  12. */
  13. function readPackageJsonFileOfResolvedModule(resolvedModule) {
  14. return tslib_1.__awaiter(this, void 0, void 0, function* () {
  15. const p = path.dirname(path.dirname(resolvedModule)); // "main": <folder>/index.js
  16. try {
  17. return yield npm_1.readPackageJsonFile(path.resolve(p, 'package.json'));
  18. }
  19. catch (e) {
  20. if (e !== fs_1.ERROR_FILE_NOT_FOUND) {
  21. throw e;
  22. }
  23. const p = path.dirname(resolvedModule); // "main": index.js
  24. return yield npm_1.readPackageJsonFile(path.resolve(p, 'package.json'));
  25. }
  26. });
  27. }
  28. exports.readPackageJsonFileOfResolvedModule = readPackageJsonFileOfResolvedModule;
  29. let installer;
  30. /**
  31. * Resolves pkg manager intent with command args.
  32. *
  33. * @return Promise<args> If the args is an empty array, it means the pkg manager doesn't have that command.
  34. */
  35. function pkgManagerArgs(env, options = {}) {
  36. return tslib_1.__awaiter(this, void 0, void 0, function* () {
  37. let vocab;
  38. const config = yield env.config.load();
  39. if (!options.command) {
  40. options.command = 'install';
  41. }
  42. let command = options.command;
  43. if (command === 'dedupe') {
  44. delete options.pkg;
  45. }
  46. if (command === 'dedupe' || command === 'rebuild') {
  47. delete options.global;
  48. delete options.link;
  49. delete options.save;
  50. delete options.saveDev;
  51. }
  52. if (command === 'dedupe' || command === 'rebuild' || command === 'uninstall') {
  53. delete options.saveExact;
  54. }
  55. if (command === 'install' || command === 'uninstall') {
  56. if (options.link) {
  57. options.global = false;
  58. if (command === 'install') {
  59. command = 'link';
  60. }
  61. else if (command === 'uninstall') {
  62. command = 'unlink';
  63. }
  64. }
  65. if (options.global || options.link) {
  66. options.save = false;
  67. options.saveDev = false;
  68. options.saveExact = false;
  69. }
  70. else if (options.pkg && typeof options.save === 'undefined' && typeof options.saveDev === 'undefined') {
  71. options.save = true;
  72. }
  73. if (command === 'install' && options.pkg && typeof options.saveExact === 'undefined') {
  74. options.saveExact = true;
  75. }
  76. }
  77. if (config.yarn) {
  78. if (!installer) {
  79. try {
  80. yield env.shell.run('yarn', ['--version'], { fatalOnNotFound: false, showCommand: false });
  81. installer = 'yarn';
  82. }
  83. catch (e) {
  84. if (e === shell_1.ERROR_SHELL_COMMAND_NOT_FOUND) {
  85. env.log.warn(`You have opted into yarn, but ${chalk_1.default.green('yarn')} was not found in your PATH.`);
  86. }
  87. else {
  88. env.log.debug(() => `Error running yarn: ${e}`);
  89. }
  90. installer = 'npm';
  91. }
  92. }
  93. }
  94. else {
  95. installer = 'npm';
  96. }
  97. const installerArgs = [];
  98. if (installer === 'npm') {
  99. vocab = { install: 'i', bareInstall: 'i', uninstall: 'uninstall', dedupe: 'dedupe', rebuild: 'rebuild', global: '-g', save: '--save', saveDev: '-D', saveExact: '-E', nonInteractive: '' };
  100. }
  101. else if (installer === 'yarn') {
  102. vocab = { install: 'add', bareInstall: 'install', uninstall: 'remove', dedupe: '', rebuild: 'install', global: '', save: '', saveDev: '--dev', saveExact: '--exact', nonInteractive: '--non-interactive' };
  103. if (options.global) {
  104. installerArgs.push('global');
  105. }
  106. }
  107. else {
  108. throw new Error(`unknown installer: ${installer}`);
  109. }
  110. if (command === 'install') {
  111. if (options.pkg) {
  112. installerArgs.push(vocab.install);
  113. }
  114. else {
  115. installerArgs.push(vocab.bareInstall);
  116. }
  117. }
  118. else if (command === 'uninstall') {
  119. installerArgs.push(vocab.uninstall);
  120. }
  121. else if (command === 'dedupe') {
  122. if (vocab.dedupe) {
  123. installerArgs.push(vocab.dedupe);
  124. }
  125. else {
  126. return [];
  127. }
  128. }
  129. else if (command === 'rebuild') {
  130. installerArgs.push(vocab.rebuild);
  131. }
  132. else {
  133. installerArgs.push(command);
  134. }
  135. if (options.global && vocab.global) {
  136. installerArgs.push(vocab.global);
  137. }
  138. if (options.save && vocab.save) {
  139. installerArgs.push(vocab.save);
  140. }
  141. if (options.saveDev && vocab.saveDev) {
  142. installerArgs.push(vocab.saveDev);
  143. }
  144. if (options.saveExact && vocab.saveExact) {
  145. installerArgs.push(vocab.saveExact);
  146. }
  147. if (vocab.nonInteractive) {
  148. installerArgs.push(vocab.nonInteractive);
  149. }
  150. if (options.pkg) {
  151. if (options.link) {
  152. options.pkg = options.pkg.replace(/(.+)@.+/, '$1'); // Removes any dist tags in the pkg name, which link/unlink hate
  153. }
  154. installerArgs.push(options.pkg);
  155. }
  156. if (installer === 'yarn') {
  157. if (options.command === 'rebuild') {
  158. installerArgs.push('--force');
  159. }
  160. }
  161. return [installer, ...installerArgs];
  162. });
  163. }
  164. exports.pkgManagerArgs = pkgManagerArgs;
  165. function pkgLatestVersion(env, pkg, distTag = 'latest') {
  166. return tslib_1.__awaiter(this, void 0, void 0, function* () {
  167. const config = yield env.config.load();
  168. const shellOptions = { fatalOnError: false, showCommand: false };
  169. try {
  170. if (config.yarn) {
  171. const cmdResult = yield env.shell.run('yarn', ['info', pkg, `dist-tags.${distTag}`, '--json'], shellOptions);
  172. return JSON.parse(cmdResult).data;
  173. }
  174. else {
  175. const cmdResult = yield env.shell.run('npm', ['view', pkg, `dist-tags.${distTag}`, '--json'], shellOptions);
  176. if (cmdResult) {
  177. return JSON.parse(cmdResult);
  178. }
  179. }
  180. }
  181. catch (e) {
  182. if (e.fatal || !guards_1.isExitCodeException(e)) {
  183. throw e;
  184. }
  185. }
  186. });
  187. }
  188. exports.pkgLatestVersion = pkgLatestVersion;