updates.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.runUpdateNotify = exports.runNotify = exports.IONIC_CLOUD_CLI_MIGRATION = exports.runUpdateCheck = exports.getUpdateConfig = exports.writeUpdateConfig = exports.readUpdateConfig = void 0;
  4. const tslib_1 = require("tslib");
  5. const utils_fs_1 = require("@ionic/utils-fs");
  6. const utils_terminal_1 = require("@ionic/utils-terminal");
  7. const path = tslib_1.__importStar(require("path"));
  8. const semver = tslib_1.__importStar(require("semver"));
  9. const color_1 = require("./color");
  10. const helper_1 = require("./helper");
  11. const npm_1 = require("./utils/npm");
  12. const UPDATE_CONFIG_FILE = 'update.json';
  13. const UPDATE_CHECK_INTERVAL = 60 * 60 * 24 * 1000; // 1 day
  14. const UPDATE_NOTIFY_INTERVAL = 60 * 60 * 12 * 1000; // 12 hours
  15. const PACKAGES = ['@ionic/cli', 'native-run', 'cordova-res'];
  16. async function readUpdateConfig(dir) {
  17. return (0, utils_fs_1.readJson)(path.resolve(dir, UPDATE_CONFIG_FILE));
  18. }
  19. exports.readUpdateConfig = readUpdateConfig;
  20. async function writeUpdateConfig(dir, config) {
  21. await (0, utils_fs_1.writeJson)(path.resolve(dir, UPDATE_CONFIG_FILE), config, { spaces: 2 });
  22. }
  23. exports.writeUpdateConfig = writeUpdateConfig;
  24. async function getUpdateConfig({ config }) {
  25. const dir = path.dirname(config.p);
  26. try {
  27. return await readUpdateConfig(dir);
  28. }
  29. catch (e) {
  30. if (e.code !== 'ENOENT') {
  31. process.stderr.write(`${e.stack ? e.stack : e}\n`);
  32. }
  33. return { packages: [] };
  34. }
  35. }
  36. exports.getUpdateConfig = getUpdateConfig;
  37. async function runUpdateCheck({ config }) {
  38. const dir = path.dirname(config.p);
  39. const pkgs = (await Promise.all(PACKAGES.map(pkg => (0, npm_1.pkgFromRegistry)(config.get('npmClient'), { pkg }))))
  40. .filter((pkg) => typeof pkg !== 'undefined');
  41. const updateConfig = await getUpdateConfig({ config });
  42. const newUpdateConfig = {
  43. ...updateConfig,
  44. lastUpdate: new Date().toISOString(),
  45. packages: pkgs.map(pkg => ({
  46. name: pkg.name,
  47. version: pkg.version,
  48. })),
  49. };
  50. await writeUpdateConfig(dir, newUpdateConfig);
  51. }
  52. exports.runUpdateCheck = runUpdateCheck;
  53. exports.IONIC_CLOUD_CLI_MIGRATION = (() => `${(0, color_1.strong)((0, color_1.WARN)('Deprecated: Ionic Appflow functionality has moved to the new Ionic Cloud CLI'))}.\n` +
  54. `Existing functionality in the Ionic CLI is deprecated as of ${(0, color_1.WARN)('v6.18.0')}. ` +
  55. `This functionality will be removed from the Ionic CLI in the next major version. ` +
  56. `Please visit our simple guide to migrate to the Ionic Cloud CLI, available now.\n` +
  57. `${(0, color_1.strong)('https://ionic.io/docs/appflow/cli/migration/')}\n`)();
  58. async function runNotify(env, pkg, latestVersion) {
  59. const dir = path.dirname(env.config.p);
  60. const args = await (0, npm_1.pkgManagerArgs)(env.config.get('npmClient'), { command: 'install', pkg: pkg.name, global: true });
  61. const lines = [
  62. `Ionic CLI update available: ${(0, color_1.weak)(pkg.version)} → ${(0, color_1.success)(latestVersion)}`,
  63. `Run ${(0, color_1.input)(args.join(' '))} to update`,
  64. ];
  65. // TODO: Pull this into utils/format
  66. const padding = 3;
  67. const longestLineLength = Math.max(...lines.map(line => (0, utils_terminal_1.stringWidth)(line)));
  68. const horizontalRule = ` ${'─'.repeat(longestLineLength + padding * 2)}`;
  69. const output = (`\n${horizontalRule}\n\n` +
  70. `${lines.map(line => ` ${' '.repeat((longestLineLength - (0, utils_terminal_1.stringWidth)(line)) / 2 + padding)}${line}`).join('\n')}\n\n` +
  71. `${horizontalRule}\n\n`);
  72. process.stderr.write(output);
  73. const updateConfig = await getUpdateConfig(env);
  74. updateConfig.lastNotify = new Date().toISOString();
  75. await writeUpdateConfig(dir, updateConfig);
  76. }
  77. exports.runNotify = runNotify;
  78. async function runUpdateNotify(env, pkg) {
  79. const { name, version } = pkg;
  80. const { lastUpdate, lastNotify, packages } = await getUpdateConfig(env);
  81. const latestPkg = packages.find(pkg => pkg.name === name);
  82. const latestVersion = latestPkg ? latestPkg.version : undefined;
  83. if ((!lastNotify || new Date(lastNotify).getTime() + UPDATE_NOTIFY_INTERVAL < new Date().getTime()) && latestVersion && semver.gt(latestVersion, version)) {
  84. await runNotify(env, pkg, latestVersion);
  85. }
  86. if (!lastUpdate || new Date(lastUpdate).getTime() + UPDATE_CHECK_INTERVAL < new Date().getTime()) {
  87. await (0, helper_1.sendMessage)(env, { type: 'update-check' });
  88. }
  89. }
  90. exports.runUpdateNotify = runUpdateNotify;