File size: 2,830 Bytes
4c34106
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
 * This file is part of WPPConnect.
 *
 * WPPConnect is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * WPPConnect is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with WPPConnect.  If not, see <https://www.gnu.org/licenses/>.
 */
import boxen from 'boxen';
import chalk from 'chalk';
import latestVersion from 'latest-version';
import { defaultLogger as logger } from '../utils/logger';
import { upToDate } from '../utils/semver';
const { version } = require('../../package.json');

// Global
let welcomeShown = false;
let updatesChecked = false;

export function welcomeScreen() {
  if (welcomeShown) {
    return;
  }
  welcomeShown = true;
  logger.info(`
   _       ______  ____  ______                            __ 
  | |     / / __ \\/ __ \\/ ____/___  ____  ____  ___  _____/ /_
  | | /| / / /_/ / /_/ / /   / __ \\/ __ \\/ __ \\/ _ \\/ ___/ __/
  | |/ |/ / ____/ ____/ /___/ /_/ / / / / / / /  __/ /__/ /_  
  |__/|__/_/   /_/    \\____/\\____/_/ /_/_/ /_/\\___/\\___/\\__/`);
}

export async function checkUpdates() {
  // Check for updates if needed
  if (!updatesChecked) {
    updatesChecked = true;
    await checkWPPConnectVersion();
  }
}

/**
 * Checks for a new version of WPPConnect and logs the result
 */
async function checkWPPConnectVersion() {
  logger.info('Checking for updates');
  await latestVersion('@wppconnect-team/wppconnect')
    .then((latest) => {
      if (upToDate(version, latest)) {
        logger.info("You're up to date");
      } else {
        logger.info('There is a new version available');
        logUpdateAvailable(version, latest);
      }
    })
    .catch(() => {
      logger.warn('Failed to check for updates');
    });
}

/**
 * Logs a boxen of instructions to update
 * @param current
 * @param latest
 */
function logUpdateAvailable(current: string, latest: string) {
  // prettier-ignore
  const newVersionLog =
      `There is a new version of ${chalk.bold(`Wppconnect`)} ${chalk.gray(current)}${chalk.bold.green(latest)}\n` +
      `Update your package by running:\n\n` +
      `${chalk.bold('\>')} ${chalk.blueBright('npm update @wppconnect-team/wppconnect')}`;

  logger.info(boxen(newVersionLog, { padding: 1 }));
  logger.info(
    `For more info visit: ${chalk.underline(
      'https://github.com/wppconnect-team/wppconnect/blob/master/README.md#update-checking'
    )}\n`
  );
}