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

np-guard

v1.2.0

Published

npm publish命令拦截器

Downloads

2

Readme

np-guard

npm publish命令拦截器, 提供两种拦截方式:

  1. 提示引导: 在 npm publish 时只做拦截, 并提示用户应该使用其它的命令去发布该包
  2. 重写覆盖: 重写命令覆盖原本的 npm publish 行为

安装

npm i -g np-guard or npm i np-guard -D

package.json 中配置

{
  "script": {
    "prepublishOnly": "npg"
  }
}

这时候运行 npm publish, 请求就会被拦截

npg命令参数

例如 npg --cmd 'npm run release' --color , terminal会输出以下信息:

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! `npm publish` is forbidden for this package. !!
!! Use `npm run release` instead.               !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

options:

  • version (alias: v)
  • help (alias: h)
  • silent (alias: s): 是否输出提示信息, 默认为非静默模式, 即输出
  • msg (alias: m): 自定义要输出的信息, 默认为npg自带的提示信息
  • cmd (alias C): 如果使用npg自带的提示信息, cmd则是一个信息插槽
  • color (alias c): 输出的文字颜色, 默认为white, 可选值可以通过help参数查看
  • bg (alias b): 输出的文字背景色, 默认为red, 可选值可以通过help参数查看
  • skip-npg (alias S): 跳过npg拦截, 用于真实的 npm publish 运行阶段
  • overwrite (alias o): 重写覆盖原本的 npm publish 行为, 如 npg -o 'npm run release'

colorbg 的可选值:

  • `color`` 可选值有: black, red, green, yellow, blue, magenta, cyan, white, gray, grey
  • `bg`` 可选值有: black, red, green, yellow, blue, magenta, cyan, white

环境变量:

  • SKIP_NPG=1skip-npg 作用相同

温馨提示

最佳搭档 —— np

在package.json中配置, 下面这种的目的是: 提示引导

{
  "script": {
    "prepublishOnly": "npg --cmd 'npm run release'",
    "release": "npx cross-env SKIP_NPG=1 np --no-yarn --yolo --branch release"
  }
}

这里必须设置环境变量 SKIP_NPG=1, 因为 np 底层调用的还是 npm publish, 会导致与 prepublishOnly 钩子循环调用造成卡死

下面这种的目的是重写覆盖默认的 npm publish 行为

{
  "script": {
    "prepublishOnly": "npg -o 'npm run release'",
    "release": "np --no-yarn --yolo --any-branch"
  }
}

报错 exit 1

npm publish 后, terminal中会有这样的报错, 这属于无法避免的正常情况

npm ERR! code 1
npm ERR! path /Volumes/dev/np-guard
npm ERR! command failed
npm ERR! command sh -c exit 1

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/zhuangjunlin/.npm/_logs/2022-07-01T17_18_43_293Z-debug-0.log

原因: npm publish拦截 的实现是通过 exit 1 的方式, 这属于 npm publish 执行的报错,并不影响 prepublishOnly 的执行