简介

update-notifier用于检查包的当前版本是否是最新版本,如果不是,则提示更新
image.png

用法

  1. const updateNotifier = require('update-notifier');
  2. const pkg = require('./package.json');
  3. updateNotifier({pkg}).notify();

源码分析

package.json

  1. {
  2. ...
  3. "dependencies": {
  4. "boxen": "^5.0.0",
  5. "chalk": "^4.1.0",
  6. "configstore": "^5.0.1",
  7. "has-yarn": "^2.1.0",
  8. "import-lazy": "^2.1.0",
  9. "is-ci": "^2.0.0",
  10. "is-installed-globally": "^0.4.0",
  11. "is-npm": "^5.0.0",
  12. "is-yarn-global": "^0.3.0",
  13. "latest-version": "^5.1.0",
  14. "pupa": "^2.1.1",
  15. "semver": "^7.3.4",
  16. "semver-diff": "^3.1.1",
  17. "xdg-basedir": "^4.0.0"
  18. },
  19. "devDependencies": {
  20. "ava": "^2.4.0",
  21. "clear-module": "^4.1.1",
  22. "fixture-stdout": "^0.2.1",
  23. "mock-require": "^3.0.3",
  24. "strip-ansi": "^6.0.0",
  25. "xo": "^0.37.1"
  26. }
  27. ...
  28. }

从以上代码中,可以看出,dependencies中有一些依赖包:
boxen
此包主要用于在终端创建盒,例如:

  1. import boxen from 'boxen';
  2. console.log(boxen('unicorn', {padding: 1}));
  3. /*
  4. ┌─────────────┐
  5. │ │
  6. │ unicorn │
  7. │ │
  8. └─────────────┘
  9. */
  10. console.log(boxen('unicorn', {padding: 1, margin: 1, borderStyle: 'double'}));
  11. /*
  12. ╔═════════════╗
  13. ║ ║
  14. ║ unicorn ║
  15. ║ ║
  16. ╚═════════════╝
  17. */
  18. console.log(boxen('unicorns love rainbows', {title: 'magical', titleAlignment: 'center'}));
  19. /*
  20. ┌────── magical ───────┐
  21. │unicorns love rainbows│
  22. └──────────────────────┘
  23. */

chalk
此包可以用来控制终端中输入的字符串的颜色、背景等

  1. import chalk from 'chalk';
  2. const log = console.log;
  3. // Combine styled and normal strings
  4. log(chalk.blue('Hello') + ' World' + chalk.red('!'));
  5. // Compose multiple styles using the chainable API
  6. log(chalk.blue.bgRed.bold('Hello world!'));
  7. // Pass in multiple arguments
  8. log(chalk.blue('Hello', 'World!', 'Foo', 'bar', 'biz', 'baz'));
  9. // Nest styles
  10. log(chalk.red('Hello', chalk.underline.bgBlue('world') + '!'));
  11. // Nest styles of the same type even (color, underline, background)
  12. log(chalk.green(
  13. 'I am a green line ' +
  14. chalk.blue.underline.bold('with a blue substring') +
  15. ' that becomes green again!'
  16. ));
  17. // ES2015 template literal
  18. log(`
  19. CPU: ${chalk.red('90%')}
  20. RAM: ${chalk.green('40%')}
  21. DISK: ${chalk.yellow('70%')}
  22. `);
  23. // Use RGB colors in terminal emulators that support it.
  24. log(chalk.rgb(123, 45, 67).underline('Underlined reddish color'));
  25. log(chalk.hex('#DEADED').bold('Bold gray!'));

image.png
configstore
此包通过既定文件命名规则,在当前系统中创建文件,用于存储用户的配置数据。再通过文件读写,实现配置的修改和保存。

  1. import Configstore from 'configstore';
  2. const packageJson = JSON.parse(fs.readFileSync('./package.json', 'utf8'));
  3. // Create a Configstore instance.
  4. const config = new Configstore(packageJson.name, {foo: 'bar'});
  5. console.log(config.get('foo'));
  6. //=> 'bar'
  7. config.set('awesome', true);
  8. console.log(config.get('awesome'));
  9. //=> true
  10. // Use dot-notation to access nested properties.
  11. config.set('bar.baz', true);
  12. console.log(config.get('bar'));
  13. //=> {baz: true}
  14. config.delete('awesome');
  15. console.log(config.get('awesome'));
  16. //=> undefined

has-yarn
此包用于来检测项目中是否有yarn,主要是通过检测工作目录中是否有yarn.lock的文件

  1. .
  2. ├── foo
  3. └── package.json
  4. └── bar
  5. ├── package.json
  6. └── yarn.lock
  1. import hasYarn from 'has-yarn';
  2. hasYarn('foo');
  3. //=> false
  4. hasYarn('bar');
  5. //=> true

import-lazy
此包主要是懒加载模块

  1. // Pass in `require` or a custom import function
  2. const importLazy = require('import-lazy')(require);
  3. const _ = importLazy('lodash');
  4. // Instead of referring to its exported properties directly…
  5. _.isNumber(2);
  6. // …it's cached on consecutive calls
  7. _.isNumber('unicorn');
  8. // Works out of the box for functions and regular properties
  9. const stuff = importLazy('./math-lib');
  10. console.log(stuff.sum(1, 2)); // => 3
  11. console.log(stuff.PHI); // => 1.618033

is-ci:判断当前环境是否是持续集成的服务
is-installed-globally:检测是否安装了全局的包
is-npm:检测当前代码是否是npm或yarn运行的
is-yarn-global:检测是否全局安装了yarn
latest-version:获取npm包的最新版
pupa
此包是一个简便的宏模版

  1. import pupa from 'pupa';
  2. pupa('The mobile number of {name} is {phone.mobile}', {
  3. name: 'Sindre',
  4. phone: {
  5. mobile: '609 24 363'
  6. }
  7. });
  8. //=> 'The mobile number of Sindre is 609 24 363'
  9. pupa('I like {0} and {1}', ['🦄', '🐮']);
  10. //=> 'I like 🦄 and 🐮'
  11. // Double braces encodes the HTML entities to avoid code injection.
  12. pupa('I like {{0}} and {{1}}', ['<br>🦄</br>', '<i>🐮</i>']);
  13. //=> 'I like &lt;br&gt;🦄&lt;/br&gt; and &lt;i&gt;🐮&lt;/i&gt;'

semver:Node的语义化版本
semver-diff:获取两个semver版本的diff类型
xdg-basedir
此包主要应用于Linux平台,基于xdg获取文件路径。

  1. import {xdgData, xdgConfig, xdgDataDirectories} from 'xdg-basedir';
  2. console.log(xdgData);
  3. //=> '/home/sindresorhus/.local/share'
  4. console.log(xdgConfig);
  5. //=> '/home/sindresorhus/.config'
  6. console.log(xdgDataDirectories);
  7. //=> ['/home/sindresorhus/.local/share', '/usr/local/share/', '/usr/share/']

devDependencies
ava和xo主要用来进行进行
clear-module:清除模块缓存
fixture-stdout:
mock-require:简单、直观地模拟Node模块
strip-ansi:从字符串中去掉ANSI转译码

  1. import stripAnsi from 'strip-ansi';
  2. stripAnsi('\u001B[4mUnicorn\u001B[0m');
  3. //=> 'Unicorn'
  4. stripAnsi('\u001B]8;;https://github.com\u0007Click\u001B]8;;\u0007');
  5. //=> 'Click'

index.js

  1. 'use strict';
  2. const {spawn} = require('child_process');
  3. const path = require('path');
  4. const {format} = require('util');
  5. const importLazy = require('import-lazy')(require);
  6. const configstore = importLazy('configstore');
  7. const chalk = importLazy('chalk');
  8. const semver = importLazy('semver');
  9. const semverDiff = importLazy('semver-diff');
  10. const latestVersion = importLazy('latest-version');
  11. const isNpm = importLazy('is-npm');
  12. const isInstalledGlobally = importLazy('is-installed-globally');
  13. const isYarnGlobal = importLazy('is-yarn-global');
  14. const hasYarn = importLazy('has-yarn');
  15. const boxen = importLazy('boxen');
  16. const xdgBasedir = importLazy('xdg-basedir');
  17. const isCi = importLazy('is-ci');
  18. const pupa = importLazy('pupa');
  19. const ONE_DAY = 1000 * 60 * 60 * 24;

定义的变量,具体用法可以参照上面package.json的包的解释

代码部分就直接贴一个运行的流程图了
image.png