UI for Zipcoin Blue

plugins.js 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const tslib_1 = require("tslib");
  4. const fs = require("fs");
  5. const path = require("path");
  6. const chalk_1 = require("chalk");
  7. const guards_1 = require("../guards");
  8. const errors_1 = require("./errors");
  9. const fs_1 = require("@ionic/cli-framework/utils/fs");
  10. const http_1 = require("./utils/http");
  11. const npm_1 = require("./utils/npm");
  12. exports.ERROR_PLUGIN_NOT_INSTALLED = 'PLUGIN_NOT_INSTALLED';
  13. exports.ERROR_PLUGIN_NOT_FOUND = 'PLUGIN_NOT_FOUND';
  14. exports.ERROR_PLUGIN_INVALID = 'PLUGIN_INVALID';
  15. exports.KNOWN_PLUGINS = ['proxy'];
  16. const ORG_PREFIX = '@ionic';
  17. const PLUGIN_PREFIX = 'cli-plugin-';
  18. function formatFullPluginName(name) {
  19. return `${ORG_PREFIX}/${PLUGIN_PREFIX}${name}`;
  20. }
  21. exports.formatFullPluginName = formatFullPluginName;
  22. function promptToInstallPlugin(env, pluginName, { message, global = false, reinstall = false }) {
  23. return tslib_1.__awaiter(this, void 0, void 0, function* () {
  24. if (!global && !env.project.directory) {
  25. return;
  26. }
  27. try {
  28. return yield loadPlugin(env, pluginName, {
  29. askToInstall: true,
  30. global,
  31. reinstall,
  32. message,
  33. });
  34. }
  35. catch (e) {
  36. if (e !== exports.ERROR_PLUGIN_NOT_INSTALLED) {
  37. throw e;
  38. }
  39. }
  40. });
  41. }
  42. exports.promptToInstallPlugin = promptToInstallPlugin;
  43. function registerPlugin(env, plugin) {
  44. if (plugin.registerHooks) {
  45. plugin.registerHooks(env.hooks);
  46. }
  47. env.plugins[plugin.meta.name] = plugin;
  48. }
  49. exports.registerPlugin = registerPlugin;
  50. function unregisterPlugin(env, plugin) {
  51. env.hooks.deleteSource(plugin.meta.name);
  52. delete env.plugins[plugin.meta.name];
  53. }
  54. exports.unregisterPlugin = unregisterPlugin;
  55. function loadPlugins(env) {
  56. return tslib_1.__awaiter(this, void 0, void 0, function* () {
  57. const global = !env.meta.local;
  58. const modulesDir = path.resolve(global ? path.dirname(path.dirname(path.dirname(env.meta.libPath))) : path.join(env.project.directory, 'node_modules'));
  59. const pluginPkgs = yield Promise.all(exports.KNOWN_PLUGINS
  60. .map(formatFullPluginName)
  61. .map((pkgName) => tslib_1.__awaiter(this, void 0, void 0, function* () {
  62. const pluginPath = path.resolve(modulesDir, path.normalize(pkgName));
  63. const exists = yield fs_1.pathExists(pluginPath);
  64. return [pkgName, exists];
  65. })));
  66. const [, proxyVar] = http_1.getGlobalProxy();
  67. if (proxyVar) {
  68. const proxyPluginPkg = formatFullPluginName('proxy');
  69. env.log.debug(() => `Detected ${chalk_1.default.green(proxyVar)} in environment`);
  70. if (!pluginPkgs.find(v => v[0] === proxyPluginPkg && v[1])) {
  71. const canInstall = yield fs_1.pathAccessible(env.plugins.ionic.meta.filePath, fs.constants.W_OK);
  72. const proxyInstallArgs = yield npm_1.pkgManagerArgs(env, { pkg: proxyPluginPkg, global });
  73. const installMsg = `Detected ${chalk_1.default.green(proxyVar)} in environment, but to proxy CLI requests, you'll need ${chalk_1.default.cyan(proxyPluginPkg)} installed.`;
  74. if (canInstall) {
  75. yield promptToInstallPlugin(env, proxyPluginPkg, {
  76. message: `${installMsg} Install now?`,
  77. reinstall: true,
  78. global,
  79. });
  80. }
  81. else {
  82. env.log.warn(`${installMsg}\nYou can install it manually:\n\n${chalk_1.default.green(proxyInstallArgs.join(' '))}\n`);
  83. }
  84. }
  85. }
  86. const pluginPromises = pluginPkgs.map((pkg) => tslib_1.__awaiter(this, void 0, void 0, function* () {
  87. const [pkgName, exists] = pkg;
  88. if (exists) {
  89. try {
  90. return yield loadPlugin(env, pkgName, { askToInstall: false, global });
  91. }
  92. catch (e) {
  93. if (e !== exports.ERROR_PLUGIN_INVALID) {
  94. throw e;
  95. }
  96. }
  97. }
  98. }));
  99. for (let p of pluginPromises) {
  100. const plugin = yield p;
  101. if (plugin) {
  102. registerPlugin(env, plugin);
  103. }
  104. }
  105. });
  106. }
  107. exports.loadPlugins = loadPlugins;
  108. function loadPlugin(env, pluginName, { message, askToInstall = true, reinstall = false, global = false }) {
  109. return tslib_1.__awaiter(this, void 0, void 0, function* () {
  110. const config = yield env.config.load();
  111. const modulesDir = path.resolve(global ? path.dirname(path.dirname(path.dirname(env.meta.libPath))) : path.join(env.project.directory, 'node_modules'));
  112. let mResolvedPath;
  113. let m;
  114. if (!message) {
  115. message = `The plugin ${chalk_1.default.cyan(pluginName)} is not installed. Would you like to install it and continue?`;
  116. }
  117. env.log.debug(() => `Loading ${global ? 'global' : 'local'} plugin ${chalk_1.default.bold(pluginName)}`);
  118. try {
  119. mResolvedPath = require.resolve(path.resolve(modulesDir, pluginName));
  120. delete require.cache[mResolvedPath];
  121. m = require(mResolvedPath);
  122. }
  123. catch (e) {
  124. if (e.code !== 'MODULE_NOT_FOUND') {
  125. throw e;
  126. }
  127. if (!askToInstall) {
  128. env.log.debug(() => `${chalk_1.default.red(exports.ERROR_PLUGIN_NOT_INSTALLED)}: ${global ? 'global' : 'local'} ${chalk_1.default.bold(pluginName)}`);
  129. throw exports.ERROR_PLUGIN_NOT_INSTALLED;
  130. }
  131. }
  132. if (!m || reinstall) {
  133. const confirm = yield env.prompt({
  134. type: 'confirm',
  135. name: 'confirm',
  136. message,
  137. });
  138. if (confirm) {
  139. const [installer, ...installerArgs] = yield pkgInstallPluginArgs(env, pluginName, { global });
  140. yield env.shell.run(installer, installerArgs, {});
  141. m = yield loadPlugin(env, pluginName, { askToInstall: false, global });
  142. mResolvedPath = require.resolve(path.resolve(modulesDir, pluginName));
  143. }
  144. else {
  145. config.state.lastNoResponseToUpdate = new Date().toISOString();
  146. throw exports.ERROR_PLUGIN_NOT_INSTALLED;
  147. }
  148. }
  149. if (m.version || !guards_1.isPlugin(m) || !mResolvedPath) {
  150. env.log.debug(() => `${chalk_1.default.red(exports.ERROR_PLUGIN_INVALID)}: ${global ? 'global' : 'local'} ${chalk_1.default.bold(pluginName)}`);
  151. throw exports.ERROR_PLUGIN_INVALID;
  152. }
  153. const meta = yield getPluginMeta(mResolvedPath);
  154. m.meta = meta;
  155. if (config.daemon.updates) {
  156. const latestVersion = yield getLatestPluginVersion(env, meta.name, meta.version);
  157. env.log.debug(() => `Latest plugin version of ${chalk_1.default.bold(meta.name + (meta.distTag === 'latest' ? '' : '@' + meta.distTag))} is ${chalk_1.default.bold(latestVersion || 'unknown')}, according to daemon file.`);
  158. m.meta.latestVersion = latestVersion;
  159. m.meta.updateAvailable = latestVersion ? yield versionNeedsUpdating(meta.version, latestVersion) : undefined;
  160. }
  161. return m;
  162. });
  163. }
  164. exports.loadPlugin = loadPlugin;
  165. function getPluginMeta(p) {
  166. return tslib_1.__awaiter(this, void 0, void 0, function* () {
  167. const packageJson = yield npm_1.readPackageJsonFileOfResolvedModule(p);
  168. const name = packageJson.name;
  169. const version = packageJson.version || '';
  170. const distTag = determineDistTag(version);
  171. return {
  172. distTag,
  173. filePath: p,
  174. name,
  175. version,
  176. };
  177. });
  178. }
  179. exports.getPluginMeta = getPluginMeta;
  180. function versionNeedsUpdating(version, latestVersion) {
  181. return tslib_1.__awaiter(this, void 0, void 0, function* () {
  182. const semver = yield Promise.resolve().then(() => require('semver'));
  183. const distTag = determineDistTag(version);
  184. return semver.gt(latestVersion, version) || (['canary', 'testing'].includes(distTag) && latestVersion !== version);
  185. });
  186. }
  187. exports.versionNeedsUpdating = versionNeedsUpdating;
  188. function facilitateIonicUpdate(env, ionicPlugin, latestVersion) {
  189. return tslib_1.__awaiter(this, void 0, void 0, function* () {
  190. const config = yield env.config.load();
  191. const global = !env.meta.local;
  192. const ionicInstallArgs = yield pkgInstallPluginArgs(env, 'ionic', { global });
  193. const updateMsg = `The Ionic CLI ${global ? '' : '(local version) '}has an update available (${chalk_1.default.cyan(ionicPlugin.meta.version)} => ${chalk_1.default.cyan(latestVersion)})!`;
  194. const canInstall = global ? yield fs_1.pathAccessible(ionicPlugin.meta.filePath, fs.constants.W_OK) : true;
  195. if (canInstall) {
  196. const confirm = yield env.prompt({
  197. name: 'confirm',
  198. type: 'confirm',
  199. message: `${updateMsg} Would you like to install it?`,
  200. });
  201. if (confirm) {
  202. const [installer, ...installerArgs] = ionicInstallArgs;
  203. yield env.shell.run(installer, installerArgs, {});
  204. const revertArgs = yield npm_1.pkgManagerArgs(env, { pkg: `ionic@${ionicPlugin.meta.version}`, global });
  205. env.log.nl();
  206. env.log.ok(`Updated Ionic CLI to ${chalk_1.default.bold(latestVersion)}! 🎉`);
  207. env.log.nl();
  208. env.log.msg(chalk_1.default.bold('Please re-run your command.'));
  209. env.log.nl();
  210. throw new errors_1.FatalException(`${chalk_1.default.bold('Note')}: You can downgrade to your old version by running: ${chalk_1.default.green(revertArgs.join(' '))}`, 0);
  211. }
  212. else {
  213. config.state.lastNoResponseToUpdate = new Date().toISOString();
  214. env.log.info(`Not automatically updating your CLI.`);
  215. }
  216. }
  217. else {
  218. env.log.info(updateMsg);
  219. env.log.nl();
  220. env.log.warn(`No write permissions for ${global ? 'global' : 'local'} ${chalk_1.default.bold('node_modules')}--automatic CLI updates are disabled.\n` +
  221. `To fix, see ${chalk_1.default.bold('https://docs.npmjs.com/getting-started/fixing-npm-permissions')}\n\n` +
  222. `Or, install the CLI update manually:\n\n${chalk_1.default.green(ionicInstallArgs.join(' '))}\n`);
  223. }
  224. });
  225. }
  226. function facilitatePluginUpdate(env, ionicPlugin, plugin, latestVersion) {
  227. return tslib_1.__awaiter(this, void 0, void 0, function* () {
  228. const global = !env.meta.local;
  229. const startMsg = `${global ? 'Global' : 'Local'} plugin ${chalk_1.default.cyan(plugin.meta.name)}`;
  230. const updateMsg = `${startMsg} has an update available (${chalk_1.default.cyan(plugin.meta.version)} => ${chalk_1.default.cyan(latestVersion)})!`;
  231. const canInstall = global ? yield fs_1.pathAccessible(plugin.meta.filePath, fs.constants.W_OK) : true;
  232. if (canInstall) {
  233. const message = ionicPlugin.meta.distTag === plugin.meta.distTag ?
  234. `${updateMsg} Would you like to install it?` :
  235. `${startMsg} has a different dist-tag (${chalk_1.default.cyan('@' + plugin.meta.distTag)}) than the Ionic CLI (${chalk_1.default.cyan('@' + ionicPlugin.meta.distTag)}). Would you like to install the appropriate plugin version?`;
  236. const okmessage = ionicPlugin.meta.distTag === plugin.meta.distTag ?
  237. `Updated ${chalk_1.default.bold(plugin.meta.name)} to ${chalk_1.default.bold(latestVersion)}! 🎉` :
  238. `Installed ${chalk_1.default.bold(plugin.meta.name + '@' + ionicPlugin.meta.distTag)}`;
  239. const p = yield promptToInstallPlugin(env, plugin.meta.name, {
  240. message,
  241. reinstall: true,
  242. global,
  243. });
  244. if (p) {
  245. unregisterPlugin(env, plugin);
  246. registerPlugin(env, p);
  247. env.log.ok(okmessage);
  248. return true;
  249. }
  250. env.log.info(`Not automatically updating ${chalk_1.default.bold(plugin.meta.name)}.`);
  251. }
  252. else {
  253. env.log.info(updateMsg);
  254. env.log.nl();
  255. env.log.warn(`No write permissions for ${global ? 'global' : 'local'}${chalk_1.default.bold('node_modules')}--automatic plugin updates are disabled.\n` +
  256. `To fix, see ${chalk_1.default.bold('https://docs.npmjs.com/getting-started/fixing-npm-permissions')}\n`);
  257. }
  258. return false;
  259. });
  260. }
  261. function checkForUpdates(env) {
  262. return tslib_1.__awaiter(this, void 0, void 0, function* () {
  263. const [config,] = yield Promise.all([env.config.load(), env.daemon.load()]);
  264. if (!config.daemon.updates) {
  265. return [];
  266. }
  267. yield env.daemon.save();
  268. if (env.plugins.ionic.meta.updateAvailable && env.plugins.ionic.meta.latestVersion) {
  269. yield facilitateIonicUpdate(env, env.plugins.ionic, env.plugins.ionic.meta.latestVersion);
  270. }
  271. const values = yield Promise.resolve().then(() => require('lodash/values'));
  272. const plugins = values(env.plugins).filter(p => p !== env.plugins.ionic);
  273. const updates = [];
  274. for (let plugin of plugins) {
  275. // TODO: differing dist-tags?
  276. if ((yield env.config.isUpdatingEnabled()) && plugin.meta.updateAvailable && plugin.meta.latestVersion) {
  277. const installed = yield facilitatePluginUpdate(env, env.plugins.ionic, plugin, plugin.meta.latestVersion);
  278. if (installed) {
  279. updates.push(plugin.meta.name);
  280. }
  281. }
  282. }
  283. if (updates.length > 0) {
  284. const [installer, ...dedupeArgs] = yield npm_1.pkgManagerArgs(env, { command: 'dedupe' });
  285. if (dedupeArgs.length > 0) {
  286. try {
  287. yield env.shell.run(installer, dedupeArgs, { fatalOnError: false });
  288. }
  289. catch (e) {
  290. env.log.warn('Error while deduping npm dependencies. Attempting to continue...');
  291. }
  292. }
  293. }
  294. return updates;
  295. });
  296. }
  297. exports.checkForUpdates = checkForUpdates;
  298. function getLatestPluginVersion(env, name, version) {
  299. return tslib_1.__awaiter(this, void 0, void 0, function* () {
  300. const daemon = yield env.daemon.load();
  301. const distTag = determineDistTag(version);
  302. if (typeof daemon.latestVersions[distTag] === 'object') {
  303. if (daemon.latestVersions[distTag][name]) {
  304. const version = daemon.latestVersions[distTag][name];
  305. return version;
  306. }
  307. }
  308. else {
  309. env.daemon.populateDistTag(distTag);
  310. }
  311. });
  312. }
  313. exports.getLatestPluginVersion = getLatestPluginVersion;
  314. function pkgInstallPluginArgs(env, name, options = {}) {
  315. return tslib_1.__awaiter(this, void 0, void 0, function* () {
  316. const releaseChannelName = determineDistTag(env.plugins.ionic.meta.version);
  317. let pluginInstallVersion = `${name}@${releaseChannelName}`;
  318. options.pkg = pluginInstallVersion;
  319. options.saveDev = true;
  320. return npm_1.pkgManagerArgs(env, options);
  321. });
  322. }
  323. exports.pkgInstallPluginArgs = pkgInstallPluginArgs;
  324. function determineDistTag(version) {
  325. if (version.includes('-alpha')) {
  326. return 'canary';
  327. }
  328. if (version.includes('-testing')) {
  329. return 'testing';
  330. }
  331. return 'latest';
  332. }
  333. exports.determineDistTag = determineDistTag;
  334. function detectAndWarnAboutDeprecatedPlugin(env, plugin) {
  335. return tslib_1.__awaiter(this, void 0, void 0, function* () {
  336. const packageJson = yield env.project.loadPackageJson();
  337. if (packageJson.devDependencies && packageJson.devDependencies[plugin]) {
  338. const { pkgManagerArgs } = yield Promise.resolve().then(() => require('../lib/utils/npm'));
  339. const args = yield pkgManagerArgs(env, { pkg: plugin, command: 'uninstall', saveDev: true });
  340. env.log.warn(`Detected ${chalk_1.default.bold(plugin)} in your ${chalk_1.default.bold('package.json')}.\n` +
  341. `As of CLI 3.8, it is no longer needed. You can uninstall it:\n\n${chalk_1.default.green(args.join(' '))}\n`);
  342. }
  343. });
  344. }
  345. exports.detectAndWarnAboutDeprecatedPlugin = detectAndWarnAboutDeprecatedPlugin;