{"size": 1389, "ext": "js", "max_stars_repo_path": "lib/utils.js", "max_stars_repo_name": "zhennann/egg-born-scripts", "max_stars_repo_stars_event_min_datetime": "2020-12-21T01:06:51.000Z", "max_stars_repo_stars_event_max_datetime": "2020-12-21T01:06:51.000Z", "max_issues_repo_path": "lib/utils.js", "max_issues_repo_name": "zhennann/egg-born-scripts", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "lib/utils.js", "max_forks_repo_name": "zhennann/egg-born-scripts", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 33.8780487805, "max_line_length": 171, "alphanum_fraction": 0.6133909287}
const urllib = require('urllib');
const semverDiff = require('semver-diff');
const chalk = require('chalk');
const boxen = require('boxen');

const boxenOptions = { padding: 1, margin: 1, align: 'center', borderColor: 'yellow', borderStyle: 'round' };

const utils = {
  async versionCheck(moduleName, scene) {
    try {
      const httpClient = urllib.create();
      const url = `https://admin.cabloy.com/api/cabloy/store/util/version/${moduleName}/${scene}`;
      const options = {
        dataType: 'json',
        followRedirect: true,
        maxRedirects: 5,
        timeout: 20000,
      };
      const res = await httpClient.request(url, options);
      return res.data.data;
    } catch (err) {
      return null;
    }
  },
  versionPrompt(moduleName, moduleVersion) {
    try {
      if (!moduleVersion) return;
      const _pkg = require('cabloy/package.json');
      const diffType = semverDiff(_pkg.version, moduleVersion);
      if (!diffType) return;
      setTimeout(() => {
        // log
        let message = `[${chalk.keyword('cyan')(moduleName)}] new version available: ${chalk.keyword('yellow')(_pkg.version)} → ${chalk.keyword('orange')(moduleVersion)}`;
        message += `\nRun ${chalk.keyword('orange')('npm update')} to update!`;
        console.log('\n' + boxen(message, boxenOptions));
      }, 6000);
    } catch (err) { }
  },
};
module.exports = utils;
