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/find-root

v2.0.16

Published

尋找 Yarn workspace 根目錄的工具,提供路徑驗證、錯誤處理和斷言函數 (Find Yarn workspace root directory with path validation, error handling, and assertion functions)

Readme

@yarn-tool/find-root

尋找 Yarn workspace 根目錄的工具,提供路徑驗證、錯誤處理和斷言函數
Find Yarn workspace root directory with path validation, error handling, and assertion functions

簡介 / Introduction

此模組提供尋找 Yarn workspace 根目錄的進階功能,包含路徑驗證、錯誤處理和斷言函數。它能夠自動偵測當前目錄是否位於 workspace 環境中,並返回完整的路徑資訊。

This module provides advanced functionality for finding Yarn workspace root directories, including path validation, error handling, and assertion functions. It can automatically detect whether the current directory is inside a workspace environment and return complete path information.

功能特色 / Features

  • 🔍 根目錄偵測 (Root Detection) - 自動尋找 workspace 根目錄和套件根目錄
  • 路徑驗證 (Path Validation) - 提供多種斷言函數驗證 workspace 狀態
  • ⚠️ 錯誤處理 (Error Handling) - 支援自訂錯誤拋出行為
  • 🔄 延遲初始化 (Lazy Initialization) - 提供方便的 findRootLazy 函數
  • 📋 模式匹配 (Pattern Matching) - 列出符合套件路徑的 workspace 模式

安裝 / Install

yarn add @yarn-tool/find-root

yarn-tool add @yarn-tool/find-root

yt add @yarn-tool/find-root

pnpm add @yarn-tool/find-root

npm install @yarn-tool/find-root

使用方式 / Usage

基本用法 / Basic Usage

import { findRoot, findRootLazy } from '@yarn-tool/find-root';

// 使用 findRootLazy(選項皆為可選)
// Using findRootLazy (all options are optional)
const rootData = findRootLazy();
console.log(rootData?.root); // 專案根目錄 / Project root
console.log(rootData?.ws);   // Workspace 根目錄 / Workspace root
console.log(rootData?.pkg);  // 套件根目錄 / Package root

// 使用 findRoot(需提供完整選項)
// Using findRoot (requires complete options)
const rootData2 = findRoot({
  cwd: process.cwd(),
  throwError: true,
});

返回值說明 / Return Value

interface IFindRootReturnType {
  /** 當前工作目錄 / Current working directory */
  cwd: string;
  /** 當前套件的根目錄 / Current package root directory */
  pkg: string;
  /** Workspace 根目錄 / Workspace root directory */
  ws: string;
  /** 是否存在 workspace 配置 / Whether workspace configuration exists */
  hasWorkspace: boolean;
  /** 當前目錄是否為 workspace 根目錄 / Whether current directory is workspace root */
  isWorkspace: boolean;
  /** 專案根目錄 / Project root */
  root: string;
  /** 當前目錄是否為專案根目錄 / Whether current directory is project root */
  isRoot: boolean;
}

斷言函數 / Assertion Functions

import { 
  assertHasWorkspaces, 
  assertNotWorkspacesRoot,
  assertHasAndNotWorkspacesRoot 
} from '@yarn-tool/find-root';

// 斷言當前目錄位於 workspace 內
// Assert that current directory is inside a workspace
assertHasWorkspaces(rootData);

// 斷言當前目錄不是 workspace 根目錄
// Assert that current directory is not workspace root
assertNotWorkspacesRoot(rootData);

// 組合斷言:位於 workspace 內但不是根目錄
// Combined assertion: inside workspace but not root
assertHasAndNotWorkspacesRoot(rootData);

列出 Workspace 模式 / List Workspace Patterns

import { listMatchedPatternByPath } from '@yarn-tool/find-root';

// 列出符合套件路徑的 workspace 模式
// List workspace patterns that match the package path
const patterns = listMatchedPatternByPath(
  '/workspace/root',           // Workspace 根目錄 / Workspace root
  '/workspace/root/packages/a' // 套件目錄 / Package directory
);
console.log(patterns); // ['packages/*']

選項說明 / Options

interface IFindRootOptions {
  /** 當前工作目錄 / Current working directory */
  cwd: string;
  /** 是否跳過 workspace 檢查 / Whether to skip workspace check */
  skipCheckWorkspace?: boolean | string;
  /** 找不到時是否拋出錯誤 / Whether to throw error when not found */
  throwError?: boolean;
  /** 是否要求必須有 workspace 配置 / Whether workspace configuration is required */
  shouldHasWorkspaces?: boolean;
  /** 是否要求當前目錄不能是 workspace 根目錄 / Whether current directory should not be workspace root */
  shouldNotWorkspacesRoot?: boolean;
}

API

findRoot(options, _throwError?)

尋找 Yarn workspace 根目錄的核心函數。
Core function for finding Yarn workspace root directory.

findRootLazy(options?, _throwError?)

延遲初始化版本,選項參數皆為可選。
Lazy initialization version with all options being optional.

newFakeRootData(rootData, input)

建立虛擬的根目錄資料物件。
Create a fake root data object.

assertHasWorkspaces(rootData)

斷言當前目錄位於 workspace 內。
Assert that current directory is inside a workspace.

assertNotWorkspacesRoot(rootData)

斷言當前目錄不是 workspace 根目錄。
Assert that current directory is not workspace root.

assertHasAndNotWorkspacesRoot(rootData)

組合斷言:位於 workspace 內但不是根目錄。
Combined assertion: inside workspace but not root.

listMatchedPatternByPath(ws, pkg)

列出符合指定路徑的 workspace 模式。
List workspace patterns that match the specified path.

相關模組 / Related Modules

授權 / License

ISC