UNPKG

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