wodax-update-notifier
Update notifications for WodaX Project
Last updated 7 months ago by sunzhifeng .
MIT · Repository · Original npm · Tarball · package.json
$ cnpm install wodax-update-notifier 
SYNC missed versions from official npm registry.

wodax-update-notifier

NPM version NPM downloads

简介

通过 集成 wodax-update-notifier 来实现以下功能:

  • 检测 工具wodax-update-notifier 是否为最新?
  • 检测基于脚手架create-wodax-project 生成的项目模板是否有更新?
  • 检测基于脚手架create-wodax-project 生成的项目依赖环境是否有更新?
  • 检测项目其他依赖是否有更新?
  • 检测是否遵循 WodaX 规范?
  • 应用项目补丁,强制更新
  • 应用核心依赖库更新检测,强制更新
  • 应用执行检查代码规范,完成部分自修复
  • 应用格式化代码

安装

$ npm install wodax-update-notifier -D # yarn add wodax-update-notifier -D

用法示例

check_update.js 源码

const util = require('util');
const fs = require('fs');
const path = require('path');
const updateNotifier = require('wodax-update-notifier');

const leftToken = '\033[1;31m';
const rightToken = '\033[0;0m';

/**
 * 异步运行 spawn 子进程
 * @param command
 * @param args
 * @param options
 * @param onExitCallback
 */
function runSpawnProcess(
  command = '',
  args = [],
  options = {},
  onExitCallback = () => {
  }
) {
  try {
    const envData = {
      ...process.env,
      NODE_OPTIONS: '--max_old_space_size=4096',
    };

    const hl = require('child_process').spawn(command, args, {
      env: envData,
      ...options,
    });
    hl.stdout.on('data', (data) => {
      console.log(data.toString());
    });
    hl.stderr.on('data', (data) => {
      console.log(data.toString());
    });
    hl.on('exit', (code, signal) => {
      onExitCallback && onExitCallback(code, signal);
    });
  } catch (e) {
    console.error(e);
    onExitCallback(-1, '');
  }
}

/**
 * 运行同步命令
 * @param command  命令
 * @param enableOutputContent 是否可以输出内容
 * @param enableThrow  是否可以抛出异常
 */
function runOneCommand({
                         command = `pwd`,
                         enableOutputContent = false,
                         enableThrow = false,
                       }) {
  try {
    const output = require('child_process').execSync(`${command}`, {
      env: {
        ...process.env,
        NODE_OPTIONS: '--max_old_space_size=4096',
      },
    });
    if (enableOutputContent) {
      console.log(output.toString());
    }
  } catch (e) {
    if (enableThrow) throw e;
  }
}

/** check Current Project Packages
 *
 * @param pkgInfo
 */
function checkCurrentProjectPackage(pkgInfo) {
  try {
    const {name: packageName, version: packageVersion, wodax} = pkgInfo;
    const {templateClassId, templateKey, templateVersion} = wodax;
    const pkg = {
      name: packageName,
      version: packageVersion,
    };

    // check templateVersion
    updateNotifier({
      pkg,
      templateClassId,
      templateKey,
      templateVersion,
      updateCheckInterval: 0,
      callback: function (err, info) {
        if (err) {
          if (!pkg.name === packageName) {
            console.error(err);
          }
        } else {
          console.log(info);
        }
      },
    });

    // check Dependencies
    [
      'devDependencies',
      'optionalDependencies',
      'dependencies',
      'peerDependency',
      'bundleDependencies',
    ].map((ele) => {
      const dependencies = pkgInfo[ele] || {};
      Object.keys(dependencies).map((one_pkg_name) => {
        const pkg = {
          name: one_pkg_name,
          version: dependencies[one_pkg_name],
          dependenciesType: ele,
        };

        const notifier = updateNotifier({
          pkg,
          templateClassId,
          templateKey,
          updateCheckInterval: 0,
          callback: function (err, info) {
            if (err) {
              if (!pkg.name === packageName) {
                console.error(err);
              }
            } else {
              const packageName = this.packageName;
              // 1. 先检测包是否存在?
              const modulePath = `${path.normalize(
                path.join(__dirname, '../node_modules', packageName)
              )}`;
              if (!fs.existsSync(`${modulePath}`)) {
                try {
                  const installCommand = this.getInstallCommand({
                    latestVersion: pkg.version,
                    isYarnGlobal: false,
                    isGlobal: false,
                  });

                  console.log(`${leftToken}${'-'.repeat(36)}${rightToken}`);
                  console.log(`检测路径为:'${modulePath}'`);
                  console.log(
                    `根据 WodaX 项目架构规范要求,'${packageName}' 依赖包没有找到,正在启动安装程序 ...`
                  );
                  console.log(
                    `如果 '${packageName}' 更新失败,请在终端中手动运行该命令 '${installCommand}'`
                  );
                  console.log(`${leftToken}${'-'.repeat(36)}${rightToken}`);
                  runOneCommand({
                    command: `${installCommand}`,
                    enableOutputContent: false,
                    enableThrow: true,
                  });
                  console.log(`核心依赖 '${packageName}' 已安装完成 ... `);
                  console.log(`${leftToken}${'-'.repeat(36)}${rightToken}`);
                } catch (e) {
                  console.error(e);
                }
              } else {
                // 2. 再检测是否需要更新
                const {type} = info;
                if (type && type !== 'latest') {
                  this.config.set('update', info);
                  this.printInfo(info);
                }
              }
            }
          },
        });
      });
    });
  } catch (e) {
  }
}

// 检测 npm config list
console.log(
  `${leftToken}正在检测 npm config 配置 (nodejs 全局globle 缓存cache 需要特别注意)...  ${rightToken}`
);
runOneCommand({command: `npm config list`, enableOutputContent: true});
console.log(`${leftToken}${'-'.repeat(36)}${rightToken}`);

updateNotifier.selfCheckUpdate((err, info) => {
  if (!err) {
    const {packageName, needUpdate, installCommand} = info;
    if (!needUpdate) {
      // check Dependencies
      const pkgInfo = require('../package.json');
      checkCurrentProjectPackage(pkgInfo);
    } else {
      console.log(
        `根据 WodaX 项目架构规范要求,'${packageName}' 正在强制更新中 ...`
      );
      console.log(
        `如果 '${packageName}'更新失败,请在终端中手动运行该命令 '${installCommand}' ...`
      );

      try {
        runOneCommand({command: `${installCommand}`, enableThrow: true});
        console.log(
          `${leftToken} 核心依赖 '${packageName}' 已更新完成。 正在重新检查 ... ${rightToken}`
        );
        runSpawnProcess(process.execPath, [__filename], {});
      } catch (e) {
        console.error(e);
        process.exit(999);
      }
    }
  }
});

Current Tags

  • 0.17.8                                ...           latest (7 months ago)

137 Versions

  • 0.17.8                                ...           7 months ago
  • 0.17.7                                ...           7 months ago
  • 0.17.6                                ...           7 months ago
  • 0.17.5                                ...           7 months ago
  • 0.17.4                                ...           7 months ago
  • 0.17.3                                ...           7 months ago
  • 0.17.2                                ...           7 months ago
  • 0.17.1                                ...           7 months ago
  • 0.17.0                                ...           7 months ago
  • 0.16.71                                ...           7 months ago
  • 0.16.70                                ...           7 months ago
  • 0.16.69                                ...           7 months ago
  • 0.16.68                                ...           7 months ago
  • 0.16.67                                ...           7 months ago
  • 0.16.66                                ...           7 months ago
  • 0.16.65                                ...           7 months ago
  • 0.16.64                                ...           7 months ago
  • 0.16.63                                ...           7 months ago
  • 0.16.62                                ...           7 months ago
  • 0.16.61                                ...           7 months ago
  • 0.16.60                                ...           7 months ago
  • 0.16.59                                ...           7 months ago
  • 0.16.58                                ...           7 months ago
  • 0.16.57                                ...           7 months ago
  • 0.16.56                                ...           7 months ago
  • 0.16.55                                ...           7 months ago
  • 0.16.54                                ...           7 months ago
  • 0.16.53                                ...           7 months ago
  • 0.16.52                                ...           7 months ago
  • 0.16.51                                ...           7 months ago
  • 0.16.50                                ...           7 months ago
  • 0.16.49                                ...           7 months ago
  • 0.16.48                                ...           7 months ago
  • 0.16.47                                ...           7 months ago
  • 0.16.46                                ...           7 months ago
  • 0.16.45                                ...           8 months ago
  • 0.16.44                                ...           8 months ago
  • 0.16.43                                ...           8 months ago
  • 0.16.42                                ...           8 months ago
  • 0.16.41                                ...           8 months ago
  • 0.16.38                                ...           8 months ago
  • 0.16.36                                ...           8 months ago
  • 0.16.40                                ...           8 months ago
  • 0.16.39                                ...           8 months ago
  • 0.16.37                                ...           8 months ago
  • 0.16.35                                ...           8 months ago
  • 0.16.34                                ...           8 months ago
  • 0.16.33                                ...           8 months ago
  • 0.16.32                                ...           8 months ago
  • 0.16.31                                ...           8 months ago
  • 0.16.30                                ...           8 months ago
  • 0.16.29                                ...           8 months ago
  • 0.16.28                                ...           8 months ago
  • 0.16.27                                ...           8 months ago
  • 0.16.26                                ...           8 months ago
  • 0.16.25                                ...           8 months ago
  • 0.16.24                                ...           8 months ago
  • 0.16.23                                ...           8 months ago
  • 0.16.22                                ...           8 months ago
  • 0.16.21                                ...           8 months ago
  • 0.16.20                                ...           8 months ago
  • 0.16.19                                ...           8 months ago
  • 0.16.18                                ...           8 months ago
  • 0.16.17                                ...           8 months ago
  • 0.16.16                                ...           8 months ago
  • 0.16.15                                ...           8 months ago
  • 0.16.14                                ...           8 months ago
  • 0.16.13                                ...           8 months ago
  • 0.16.12                                ...           8 months ago
  • 0.16.11                                ...           8 months ago
  • 0.16.10                                ...           8 months ago
  • 0.16.9                                ...           8 months ago
  • 0.16.8                                ...           8 months ago
  • 0.16.7                                ...           8 months ago
  • 0.16.6                                ...           8 months ago
  • 0.16.5                                ...           8 months ago
  • 0.16.4                                ...           8 months ago
  • 0.16.3                                ...           8 months ago
  • 0.16.2                                ...           8 months ago
  • 0.16.1                                ...           8 months ago
  • 0.16.0                                ...           8 months ago
  • 0.15.16                                ...           8 months ago
  • 0.15.15                                ...           8 months ago
  • 0.15.14                                ...           8 months ago
  • 0.15.13                                ...           8 months ago
  • 0.15.12                                ...           8 months ago
  • 0.15.11                                ...           8 months ago
  • 0.15.8                                ...           8 months ago
  • 0.15.7                                ...           8 months ago
  • 0.15.6                                ...           8 months ago
  • 0.15.5                                ...           8 months ago
  • 0.15.4                                ...           8 months ago
  • 0.15.3                                ...           8 months ago
  • 0.15.2                                ...           8 months ago
  • 0.15.1                                ...           8 months ago
  • 0.15.0                                ...           8 months ago
  • 0.14.20                                ...           8 months ago
  • 0.14.19                                ...           8 months ago
  • 0.14.18                                ...           8 months ago
  • 0.14.17                                ...           9 months ago
  • 0.14.16                                ...           9 months ago
  • 0.14.15                                ...           9 months ago
  • 0.14.14                                ...           9 months ago
  • 0.14.13                                ...           9 months ago
  • 0.14.12                                ...           10 months ago
  • 0.14.11                                ...           10 months ago
  • 0.14.10                                ...           10 months ago
  • 0.14.9                                ...           10 months ago
  • 0.14.8                                ...           a year ago
  • 0.14.7                                ...           a year ago
  • 0.14.6                                ...           a year ago
  • 0.14.5                                ...           a year ago
  • 0.14.4                                ...           a year ago
  • 0.14.3                                ...           a year ago
  • 0.14.2                                ...           a year ago
  • 0.14.1                                ...           a year ago
  • 0.14.0                                ...           a year ago
  • 0.13.0                                ...           a year ago
  • 0.11.0                                ...           a year ago
  • 0.10.0                                ...           a year ago
  • 0.9.0                                ...           a year ago
  • 0.8.2                                ...           a year ago
  • 0.8.1                                ...           a year ago
  • 0.8.0                                ...           a year ago
  • 0.7.1                                ...           a year ago
  • 0.6.4                                ...           a year ago
  • 0.6.3                                ...           a year ago
  • 0.6.2                                ...           a year ago
  • 0.6.0                                ...           a year ago
  • 0.5.0                                ...           a year ago
  • 0.4.0                                ...           a year ago
  • 0.3.0                                ...           a year ago
  • 0.2.2                                ...           a year ago
  • 0.2.1                                ...           a year ago
  • 0.2.0                                ...           a year ago
  • 0.1.1                                ...           a year ago
  • 0.1.0                                ...           a year ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 1
Last Month 6
Dependencies (15)
Dependents (0)
None

Copyright 2014 - 2017 © taobao.org |