npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

lerna-demo-fangcao

v1.0.0

Published

- `mkdir lerna-demo` 在d:/ 下创建一个空的文件夹,命名为lerna-demo - `cd lerna-demo && lerna init` 初始化 通过cmd进入相关目录,进行初始化 - 生成目录如下:

Downloads

3

Readme

lerna

  • mkdir lerna-demo 在d:/ 下创建一个空的文件夹,命名为lerna-demo

  • cd lerna-demo && lerna init 初始化 通过cmd进入相关目录,进行初始化

  • 生成目录如下:

    • packages(目录)
    • lerna.json(配置文件)
    • package.json(工程描述文件)
  • 添加一个测试package

    默认情况下,package是放在packages目录下的。

    // 进入packages目录
    cd d:/lerna-demo/packages
    // 创建一个packge目录
    mkdir module-1
    // 进入module-1 package目录
    cd module-1
    // 初始化一个package
    npm init -y
  • cd lerna-demo && lerna bootstrap安装各packages依赖 这一步操作,官网上是这样描述的。

    • lerna bootstrap: 安装依赖
    • lerna publish:发布和更新package
  • 发布 在发布的时候,就需要git 工具的配合了。 所以在发布之前,请确认此时该lerna工程是否已经连接到git的远程仓库。你可以执行下面的命令进行查看。

    git remote -v
    // print log
    origin  [email protected]:xmkp-math-h5/lerna-demo.git (fetch)
    origin  [email protected]:xmkp-math-h5/lerna-demo.git (push)

本篇文章的代码托管在Github上。因此会显示此远程链接信息。 如果你还没有与远程仓库链接,请首先在github创建一个空的仓库,然后根据相关提示信息,进行链接。

# 在当前项目中发布包
lerna publish

# lerna publish 永远不会发布标记为 private 的包(package.json中的”private“: true)

  • 增加模块包到 packages 中指定项目 下面是将 ui-web 模块增加到 example-web 项目中 lerna add antd-mobile --scope=module-2

  • 在最外层运行里面制定的项目 lerna exec --scope module-2 -- yarn test

  • 如果命令中不增加 --scope example-web直接使用下面的命令,这会在 packages 下所有包执行命令rm -rf ./node_modules lerna exec -- rm -rf ./node_modules

  • lerna list 显示所有的安装的包

  • lerna list --json 可以通过json的方式查看 lerna 安装了哪些包,json 中还包括包的路径,有时候可以用于查找包是否生效。

  • lerna clean 从所有包中删除 node_modules 目录

    注意下 lerna clean 不会删除项目最外层的根 node_modules

  • vv

lerna常用的命令用法

  • 基于 lerna 的多包项目建设与npm & unpkg 的发布(一)

  • 增加两个 packages

    • lerna create @mo-demo/cli
    • lerna create @mo-demo/cli-shared-utils
  • 分别给相应的package增加依赖模块

    • lerna add chalk // 为所有 package 增加 chalk 模块
    • lerna add semver --scope @mo-demo/cli-shared-utils // 为 @mo-demo/cli-shared-utils 增加 semver 模块
    • lerna add @mo-demo/cli-shared-utils --scope @mo-demo/cli // 增加内部模块之间的依赖
  • 发布

    • lerna publish
  • lerna bootstrap --hoist

    为某个 package 安装的包被放到了这个 package 目录下的 node_modules 目录下。这样对于多个 package 都依赖的包,会被多个 package 安装多次,并且每个 package 下都维护 node_modules ,也不清爽。于是我们使用 --hoist 来把每个 package 下的依赖包都提升到工程根目录,来降低安装以及管理的成本

    为了省去每次都输入 --hoist 参数的麻烦,可以在 lerna.json 配置:

    {
      "packages": [
        "packages/*"
      ],
      "command": {
        "bootstrap": {
          "hoist": true
        }
      },
      "version": "0.0.1-alpha.0"
    }
  • lerna clean

    lerna clean                           
    info cli using local version of lerna
    lerna notice cli v4.0.0
    lerna info Removing the following directories:
    lerna info clean packages/cli-shared-utils/node_modules
    lerna info clean packages/cli/node_modules
    lerna info clean packages/hello-world/node_modules
    lerna info clean packages/module-1/node_modules
    lerna info clean packages/module-2/node_modules
    ? Proceed? (ynH)
  • 优雅的提交

    • commitizen && cz-lerna-changelog

    commitizen 是用来格式化 git commit message 的工具,它提供了一种问询式的方式去获取所需的提交信息. cz-lerna-changelog 是专门为 Lerna 项目量身定制的提交规范,在问询的过程,会有类似影响哪些 package 的选择。如下:

1

  • vim .git/COMMIT_EDITMSG 可以查看到 commitizen && cz-lerna-changelog 指定的项目

2

上面我们使用了 commitizen 来规范提交,但这个要靠开发自觉使用 npm run c 。万一忘记了,或者直接使用 git commit 提交怎么办?答案就是在提交时对提交信息进行校验,如果不符合要求就不让提交,并提示。校验的工作由 commitlint 来完成,校验的时机则由 husky 来指定。husky 继承了 Git 下所有的钩子,在触发钩子的时候,husky 可以阻止不合法的 commit,push 等等。

  • commitlint && husky
// 安装 commitlint 以及要遵守的规范
npm i -D @commitlint/cli @commitlint/config-conventional

// 安装 husky
npm i -D husky
// 在工程根目录为 commitlint 增加配置文件 commitlint.config.js 为commitlint 指定相应的规范
module.exports = { extends: ['@commitlint/config-conventional'] }
// 在 package.json 中增加如下配置
"husky": {
  "hooks": {
    "commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
  }
}

"commit-msg"是git提交时校验提交信息的钩子,当触发时便会使用 commitlit 来校验。安装配置完成后,想通过 git commit 或者其它第三方工具提交时,只要提交信息不符合规范就无法提交。从而约束开发者使用 npm run c 来提交;

  • standardjs && lint-staged

    除了规范提交信息,代码本身肯定也少了靠规范来统一风格

    • standardjs就是完整的一套 JavaScript 代码规范,自带 linter & 代码自动修正。它无需配置,自动格式化代码并修正,提前发现风格以及程序问题。
    • lint-staged staged 是 Git 里的概念,表示暂存区,lint-staged 表示只检查并矫正暂存区中的文件。一来提高校验效率,二来可以为老的项目带去巨大的方便。
    // 安装
    npm i -D standard lint-staged
    // package.json
    {
      "name": "root",
      "private": true,
      "scripts": {
        "c": "git-cz"
      },
      "config": {
        "commitizen": {
          "path": "./node_modules/cz-lerna-changelog"
        }
      },
      "husky": {
        "hooks": {
          "pre-commit": "lint-staged",
          "commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
        }
      },
      "lint-staged": {
        "*.js": [
          "standard --fix",
          "git add"
        ]
      },
      "devDependencies": {
        "@commitlint/cli": "^8.1.0",
        "@commitlint/config-conventional": "^8.1.0",
        "commitizen": "^3.1.1",
        "cz-lerna-changelog": "^2.0.2",
        "husky": "^3.0.0",
        "lerna": "^3.15.0",
        "lint-staged": "^9.2.0",
        "standard": "^13.0.2"
      }
    }

    安装完成后,在 package.json 增加 lint-staged 配置,如上所示表示对暂存区中的 js 文件执行 standard --fix 校验并自动修复。那什么时候去校验呢,就又用到了上面安装的 husky ,husky的配置中增加pre-commit的钩子用来执行 lint-staged 的校验操作,如上所示。

    此时提交 js 文件时,便会自动修正并校验错误。即保证了代码风格统一,又能提高代码质量。

  • 自动生成日志

    有了之前的规范提交,自动生成日志便水到渠成了。再详细看下 lerna publish 时做了哪些事情:

    • lerna version
      • 找出从上一个版本发布以来有过变更的 package
      • 3
      • 提示开发者确定要发布的版本号
      • 将所有更新过的的 package 中的package.json的version字段更新
      • 将依赖更新过的 package 的 包中的依赖版本号更新
      • 更新 lerna.json 中的 version 字段
      • 提交上述修改,并打一个 tag
      • 推送到 git 仓库