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 🙏

© 2025 – Pkg Stats / Ryan Hefner

npm-script-manager

v1.2.0

Published

enhance your npm script in your package.json

Readme


npm-script-manager:增强你的 package.json scripts

NPM Version License

npm-script-manager 是一个强大的工具,旨在增强和扩展您在 package.json 中使用 npm script 的方式。它通过将脚本从 package.json 中分离出来,并提供一系列高级功能,让您的脚本管理变得更加轻松、灵活和强大。

为什么选择 npm-script-manager?

在日常的前端或 Node.js 开发中,我们经常需要在 package.json 中定义各种脚本。随着项目复杂度的增加,package.json 中的 scripts 字段会变得越来越臃肿,难以维护。npm-script-manager 正是为了解决这些痛点而生:

  • 告别臃肿的 package.json: 将脚本逻辑迁移到专门的文件中,让您的 package.json 保持清爽。
  • 更简洁的命令: 使用 nr 命令替代 npm run,输入更少,效率更高。
  • 动态脚本: 您的脚本不再是静态的字符串,而是可以动态生成的 JavaScript 代码,充满了可能性。
  • 添加注释和分组: 在脚本文件中,您可以像在代码中一样添加注释,还可以通过分组来组织您的脚本,使其更具可读性。
  • 强大的执行控制: 支持同步、异步以及混合模式执行多个脚本,并可以限制并发数量。
  • 环境感知: 根据不同的开发环境(例如,开发、测试、生产),自动调整脚本的参数。

安装

npm-script-manager 安装为项目的开发依赖:

npm install npm-script-manager --save-dev

快速上手

  1. package.json 中指定脚本文件:

    在您的 package.json 文件中,添加一个 script 字段,指向您存放脚本的外部文件。

    // package.json
    {
      "name": "your-project",
      "version": "1.0.0",
      "script": "./scripts/index.js"
    }
  2. 创建您的脚本文件:

    创建一个 scripts/index.js 文件(或您在 package.json 中指定的任何路径),并导出一个对象,其中包含了您的脚本。

    // scripts/index.js
    module.exports = {
      // 一个简单的脚本
      hello: 'echo "你好, npm-script-manager!"',
    
      // 一个动态脚本
      time: `echo "当前时间是: ${new Date().toString()}"`,
    };
  3. 运行您的脚本:

    现在,您可以使用 nr 命令来运行您的脚本:

    nr hello
    # 输出: "你好, npm-script-manager!"
    
    nr time
    # 输出: "当前时间是: Mon Jul 22 2024 14:30:00 GMT+0800 (中国标准时间)"

高级用法

串行执行(数组)

如果一个脚本的值是一个数组,数组中的命令将会被串行执行(一个接一个)。

// scripts/index.js
module.exports = {
  build: [
    'echo "正在清理旧的构建文件..."',
    "rm -rf dist",
    'echo "正在构建项目..."',
    "tsc",
    'echo "构建完成!"',
  ],
};

运行 nr build 将会按顺序执行上述所有命令。

并行执行(对象)

如果一个脚本的值是一个对象,您可以通过 async: true 来实现并行执行。

// scripts/index.js
module.exports = {
  "run-async": {
    async: true, // 设置为 true 以启用并行执行
    scripts: [
      'echo "任务1 (并行)"',
      'sleep 2 && echo "任务2 (并行,延迟2秒)"',
      'echo "任务3 (并行)"',
    ],
  },
};

运行 nr run-async 将会同时执行 scripts 数组中的所有命令。

限制并发数量

您可以使用 --limit 参数来限制同时运行的脚本数量。这在处理大量并行任务时非常有用,可以防止系统资源被耗尽。

nr echos --limit 5
// scripts/index.js
const echos = [];
for (let i = 0; i < 100; i++) {
  echos.push(`echo "任务 ${i}"`);
}

module.exports = {
  echos: {
    async: true,
    scripts: echos,
  },
};

嵌套脚本

npm-script-manager 支持任意层级的脚本嵌套,让您可以组合和编排复杂的任务流。

// scripts/index.js
module.exports = {
  deploy: {
    async: false, // 默认是串行执行
    scripts: [
      "nr build", // 首先执行构建
      {
        async: true, // 然后并行执行部署到不同的环境
        scripts: ['echo "部署到测试环境..."', 'echo "部署到预发布环境..."'],
      },
      'echo "部署完成!"',
    ],
  },
  // ... 其他脚本
};

函数脚本

脚本的值可以是一个函数。npm-script-manager 会首先执行该函数,如果函数返回一个字符串,那么这个字符串将被作为命令来执行。

// scripts/index.js
module.exports = {
  greet: () => {
    const hour = new Date().getHours();
    if (hour < 12) {
      return 'echo "上午好!"';
    } else if (hour < 18) {
      return 'echo "下午好!"';
    } else {
      return 'echo "晚上好!"';
    }
  },
};

Node.js >= 22 用户

如果您的 Node.js 版本大于或等于 22,您可以使用 nr2 命令,它提供了更好的性能和体验。

贡献

欢迎您为 npm-script-manager 做出贡献!如果您有任何问题或建议,请随时提交 Issue 或 Pull Request。

许可证

本项目基于 MIT 许可证。