1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const format_1 = require("@ionic/cli-framework/utils/format");
|
4 | const utils_fs_1 = require("@ionic/utils-fs");
|
5 | const path = require("path");
|
6 | const semver = require("semver");
|
7 | const color_1 = require("./color");
|
8 | const helper_1 = require("./helper");
|
9 | const npm_1 = require("./utils/npm");
|
10 | const UPDATE_CONFIG_FILE = 'update.json';
|
11 | const UPDATE_CHECK_INTERVAL = 60 * 60 * 24 * 1000;
|
12 | const UPDATE_NOTIFY_INTERVAL = 60 * 60 * 12 * 1000;
|
13 | const PACKAGES = ['ionic', 'native-run', 'cordova-res'];
|
14 | async function readUpdateConfig(dir) {
|
15 | return utils_fs_1.readJson(path.resolve(dir, UPDATE_CONFIG_FILE));
|
16 | }
|
17 | exports.readUpdateConfig = readUpdateConfig;
|
18 | async function writeUpdateConfig(dir, config) {
|
19 | await utils_fs_1.writeJson(path.resolve(dir, UPDATE_CONFIG_FILE), config, { spaces: 2 });
|
20 | }
|
21 | exports.writeUpdateConfig = writeUpdateConfig;
|
22 | async 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 | }
|
34 | exports.getUpdateConfig = getUpdateConfig;
|
35 | async 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 | }
|
50 | exports.runUpdateCheck = runUpdateCheck;
|
51 | async function runNotify(env, pkg, latestVersion) {
|
52 | const dir = path.dirname(env.config.p);
|
53 | const args = await npm_1.pkgManagerArgs(env.config.get('npmClient'), { command: 'install', pkg: pkg.name, global: true });
|
54 | const lines = [
|
55 | `Ionic CLI update available: ${color_1.weak(pkg.version)} → ${color_1.success(latestVersion)}`,
|
56 | `Run ${color_1.input(args.join(' '))} to update`,
|
57 | ];
|
58 |
|
59 | const padding = 3;
|
60 | const longestLineLength = Math.max(...lines.map(line => format_1.stringWidth(line)));
|
61 | const horizontalRule = ` ${'─'.repeat(longestLineLength + padding * 2)}`;
|
62 | const output = (`\n${horizontalRule}\n\n` +
|
63 | `${lines.map(line => ` ${' '.repeat((longestLineLength - format_1.stringWidth(line)) / 2 + padding)}${line}`).join('\n')}\n\n` +
|
64 | `${horizontalRule}\n\n`);
|
65 | process.stderr.write(output);
|
66 | const updateConfig = await getUpdateConfig(env);
|
67 | updateConfig.lastNotify = new Date().toISOString();
|
68 | await writeUpdateConfig(dir, updateConfig);
|
69 | }
|
70 | exports.runNotify = runNotify;
|
71 | async function runUpdateNotify(env, pkg) {
|
72 | const { name, version } = pkg;
|
73 | const { lastUpdate, lastNotify, packages } = await getUpdateConfig(env);
|
74 | const latestPkg = packages.find(pkg => pkg.name === name);
|
75 | const latestVersion = latestPkg ? latestPkg.version : undefined;
|
76 | if ((!lastNotify || new Date(lastNotify).getTime() + UPDATE_NOTIFY_INTERVAL < new Date().getTime()) && latestVersion && semver.gt(latestVersion, version)) {
|
77 | await runNotify(env, pkg, latestVersion);
|
78 | }
|
79 | if (!lastUpdate || new Date(lastUpdate).getTime() + UPDATE_CHECK_INTERVAL < new Date().getTime()) {
|
80 | await helper_1.sendMessage(env, { type: 'update-check' });
|
81 | }
|
82 | }
|
83 | exports.runUpdateNotify = runUpdateNotify;
|