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