UNPKG

3.74 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', '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 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 // TODO: Pull this into utils/format
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}
70exports.runNotify = runNotify;
71async 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}
83exports.runUpdateNotify = runUpdateNotify;