$ npm install find-yarn-workspace-root2
用於尋找 Yarn workspace 根目錄 (Workspace Root) 的演算法,擷取自 yarnpkg.com,並加強為 find-yarn-workspace-root 的增強版本。
當前版本:1.2.55
workspaces 的 package.json 所在目錄null,不會丟出例外readPackageJSON、extractWorkspaces、isMatchWorkspaces 等輔助函式yarn add find-yarn-workspace-root2
或使用 npm:
npm install find-yarn-workspace-root2
const findWorkspaceRoot = require('find-yarn-workspace-root2');
const workspaceRoot = findWorkspaceRoot(__dirname); // Absolute path or null
在 TypeScript 中也可直接匯入具名函式:
import { findWorkspaceRoot } from 'find-yarn-workspace-root2/core';
findWorkspaceRoot('/path/to/project');
若起始目錄本身即為 workspace 根目錄,會回傳該目錄的絕對路徑;若起始目錄不屬於任何 workspace,則回傳 null。
主入口函式,從 initial 開始向上尋找 workspace 根目錄。
| 參數 | 型別 | 說明 |
|---|---|---|
initial |
string? |
起始搜尋目錄,未提供時使用 process.cwd() |
| 回傳值 | 說明 |
|---|---|
string |
workspace 根目錄的絕對路徑 |
null |
找不到 package.json,或路徑不符合 workspaces 模式 |
find-yarn-workspace-root2/core 匯入)| 函式 | 說明 |
|---|---|
checkWorkspaces(current, initial) |
檢查 current 是否為有效的 workspace 根目錄,回傳 { done, found, relativePath } |
isMatchWorkspaces(relativePath, workspaces) |
判斷相對路徑是否符合 workspaces 的 glob 模式,回傳 boolean |
extractWorkspaces(manifest) |
自 package.json 內容中取出 workspaces 的 packages 陣列,無配置時回傳 null |
readPackageJSON(dir) |
讀取並解析指定目錄下的 package.json,檔案不存在時回傳 null |
這些輔助函式同時被附加為 findWorkspaceRoot 的靜態屬性,例如 findWorkspaceRoot.extractWorkspaces(manifest)。
本套件本身沒有需設定的選項,行為完全取決於目標專案 package.json 的 workspaces 設定。
Yarn 支援兩種格式,本套件皆可辨識:
{
"workspaces": ["packages/*"]
}
{
"workspaces": {
"packages": ["packages/*"]
}
}
判斷流程:
package.jsonpackage.json 的 workspaces 設定workspaces 設定,繼續往父目錄查找;已到達檔案系統根目錄則回傳 nullworkspaces 設定,計算起始目錄相對該目錄的路徑並進行 glob 匹配null更多相關工具的比較與遷移方式,請參閱 docs/find-path-tools.md。
Contributions are welcome! Just clone this repository and install the dependencies:
git clone https://github.com/square/find-yarn-workspace-root.git
cd find-yarn-workspace-root
yarn
Note that you'll need node and yarn installed. Next, verify the tests all pass:
yarn test
Then create a branch for your bugfix/feature, make changes and update the tests, and submit a pull request. Please do not change the version in package.json when submitting a pull request. We determine the next version automatically based on the commits since the last release.
NOTE: Commit messages follow the Angular commit message guidelines.
Releases are handled by Travis CI once a build succeeds on master. To trigger a release, bump the version using standard-version:
$(yarn bin)/standard-version
That should update the CHANGELOG, bump the version in package.json, and create a git tag. Then, just git push && git push --tags.
Copyright 2017 Square, Inc.
© 2010 - cnpmjs.org x YWFE | Home | YWFE