UNPKG

3.91 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.runUpdateNotify = exports.runNotify = exports.runUpdateCheck = exports.getUpdateConfig = exports.writeUpdateConfig = exports.readUpdateConfig = void 0;
4const utils_fs_1 = require("@ionic/utils-fs");
5const utils_terminal_1 = require("@ionic/utils-terminal");
6const path = require("path");
7const semver = require("semver");
8const color_1 = require("./color");
9const helper_1 = require("./helper");
10const npm_1 = require("./utils/npm");
11const UPDATE_CONFIG_FILE = 'update.json';
12const UPDATE_CHECK_INTERVAL = 60 * 60 * 24 * 1000; // 1 day
13const UPDATE_NOTIFY_INTERVAL = 60 * 60 * 12 * 1000; // 12 hours
14const PACKAGES = ['@ionic/cli', 'native-run', 'cordova-res'];
15async function readUpdateConfig(dir) {
16 return utils_fs_1.readJson(path.resolve(dir, UPDATE_CONFIG_FILE));
17}
18exports.readUpdateConfig = readUpdateConfig;
19async function writeUpdateConfig(dir, config) {
20 await utils_fs_1.writeJson(path.resolve(dir, UPDATE_CONFIG_FILE), config, { spaces: 2 });
21}
22exports.writeUpdateConfig = writeUpdateConfig;
23async 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}
35exports.getUpdateConfig = getUpdateConfig;
36async 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}
51exports.runUpdateCheck = runUpdateCheck;
52async 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 // TODO: Pull this into utils/format
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}
71exports.runNotify = runNotify;
72async 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}
84exports.runUpdateNotify = runUpdateNotify;