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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@yarn-tool/run-script-lifecycle

v3.0.5

Published

Execute npm/yarn lifecycle scripts with pre/post script support - 執行 npm/yarn 生命週期腳本,支援前置/後置腳本

Readme

@yarn-tool/run-script-lifecycle

NPM version License: ISC

執行 npm/yarn 生命週期腳本,支援前置/後置腳本 Execute npm/yarn lifecycle scripts with pre/post script support

簡介 (Introduction)

@yarn-tool/run-script-lifecycle 是一個用於執行 npm/yarn 生命週期腳本的工具模組。它能自動處理前置腳本 (pre-scripts) 和後置腳本 (post-scripts),讓您可以輕鬆地以程式化方式執行完整的生命週期流程。

This is a utility module for running npm/yarn lifecycle scripts. It automatically handles pre-scripts and post-scripts, allowing you to easily execute complete lifecycle flows programmatically.

功能特色 (Features)

  • 🔄 自動執行前置/後置腳本 - 自動執行 preinstallpostinstall 等相關腳本
    • Automatic pre/post script execution - Automatically runs related scripts like preinstall, postinstall
  • 📦 讀取 package.json - 自動解析套件的 package.json 並執行定義的腳本
    • Read package.json - Automatically parses package's package.json and executes defined scripts
  • 🎯 TypeScript 支援 - 完整的 TypeScript 類型定義
    • TypeScript support - Complete TypeScript type definitions
  • ⚙️ 靈活配置 - 支援自訂環境變數、stdio 配置等選項
    • Flexible configuration - Supports custom environment variables, stdio configuration, and more

安裝 (Installation)

# 使用 yarn (Using yarn)
yarn add @yarn-tool/run-script-lifecycle

# 使用 yarn-tool (Using yarn-tool)
yarn-tool add @yarn-tool/run-script-lifecycle

# 使用 npm (Using npm)
npm install @yarn-tool/run-script-lifecycle

使用方法 (Usage)

基本用法 (Basic Usage)

import runLifecycleScript from '@yarn-tool/run-script-lifecycle';

// 執行 install 生命週期腳本
// Run install lifecycle script
const results = await runLifecycleScript({
  event: 'install',
  path: '/path/to/package'
});

// results 包含 preinstall、install、postinstall 的執行結果(如果存在)
// results contains execution results for preinstall, install, postinstall (if they exist)
console.log(results);

帶選項的用法 (Usage with Options)

import runLifecycleScript from '@yarn-tool/run-script-lifecycle';

const results = await runLifecycleScript({
  event: 'build',
  path: '/path/to/package',
  stdio: 'inherit',           // 繼承父進程的 stdio (Inherit parent process stdio)
  env: {                      // 自訂環境變數 (Custom environment variables)
    NODE_ENV: 'production',
    CUSTOM_VAR: 'value'
  }
});

// 檢查執行結果 (Check execution results)
results.forEach(result => {
  if (result.code === 0) {
    console.log(`${result.event} completed successfully`);
  } else {
    console.error(`${result.event} failed with code ${result.code}`);
  }
});

API 文檔 (API Documentation)

runLifecycleScript(options)

執行生命週期腳本的主函數。

Main function to run lifecycle scripts.

參數 (Parameters)

| 參數 (Parameter) | 類型 (Type) | 必填 (Required) | 描述 (Description) | |-----------------|-------------|----------------|-------------------| | event | string | ✅ | 要執行的生命週期事件名稱 (Name of lifecycle event to run) | | path | string | ✅ | 套件所在的目錄路徑 (Directory path where package is located) | | args | any[] | ❌ | 傳遞給腳本的參數 (Arguments to pass to script) | | env | Record<string, any> | ❌ | 執行時的環境變數 (Environment variables during execution) | | stdio | StdioOptions | ❌ | 標準輸入輸出配置 (Standard I/O configuration) | | stdioString | boolean | ❌ | 是否將 stdio 視為字串 (Whether to treat stdio as strings) | | pkg | IPackageJson | ❌ | 套件的 package.json 物件 (Package's package.json object) |

返回值 (Returns)

Promise<(IResultNotExists | IResult)[]>

返回執行結果陣列,每個結果包含:

Returns an array of execution results, each containing:

| 屬性 (Property) | 類型 (Type) | 描述 (Description) | |----------------|-------------|-------------------| | code | number | 退出代碼 (Exit code) | | signal | null \| any | 執行訊號 (Execution signal) | | stdout | string | 標準輸出 (Standard output) | | stderr | string | 標準錯誤輸出 (Standard error output) | | event | string | 事件名稱 (Event name) | | script | string | 執行的腳本命令 (Executed script command) | | pkgid | string | 套件識別符 (Package identifier) | | path | string | 執行路徑 (Execution path) |

生命週期執行順序 (Lifecycle Execution Order)

當執行 install 事件時,腳本會按照以下順序執行:

When executing the install event, scripts run in the following order:

  1. preinstall - 安裝前腳本 (Pre-install script)
  2. install - 主要安裝腳本 (Main install script)
  3. postinstall - 安裝後腳本 (Post-install script)

相依套件 (Dependencies)

  • @npmcli/run-script - npm 官方的腳本執行庫
  • @yarn-tool/script-lifecycle - 生命週期配置管理
  • @ts-type/package-dts - TypeScript 類型定義
  • read-package-json-fast - 快速讀取 package.json

授權 (License)

ISC

相關套件 (Related Packages)