Rush Stack商店部落格活動
跳至主要內容

設定新的儲存庫

本教學逐步說明如何將數個專案整合到新的 Rush 單一儲存庫中。(如果您想查看基於這些步驟的完整範例,請查看 GitHub 上的 rush-example 儲存庫。)

在此範例中,假設我們有 3 個專案資料夾,如下所示

  • my-app:Web 應用程式
  • my-controls:應用程式使用的控制項程式庫
  • my-toolchain:用於編譯其他專案的 NodeJS 建置工具

最初,這些專案都位於自己的資料夾中。它們使用繁瑣的程序建置,如下所示

~$ cd my-toolchain
~/my-toolchain$ npm run build
~/my-toolchain$ npm link
~/my-toolchain$ cd ../my-controls
~/my-controls$ npm link my-toolchain
~/my-controls$ npm run build
~/my-controls$ npm link
~/my-controls$ cd ../my-app
~/my-app$ npm link my-toolchain
~/my-app$ npm link my-controls
~/my-app$ npm run build

讓我們使用 Rush 來處理這些專案!

步驟 1:檢查您的 Rush 版本

在開始之前,請確定您已全域安裝最新的 Rush 版本

~$ npm install -g @microsoft/rush

注意:如果此指令因您的使用者帳戶沒有存取 NPM 全域資料夾的權限而失敗,您可能需要修正您的 NPM 設定

步驟 2:使用「rush init」初始化您的儲存庫

假設您已建立一個空的 GitHub 儲存庫,我們將把這些專案複製到其中。將您的儲存庫複製到某個位置,然後執行 rush init 以產生 Rush 的設定檔

~$ git clone https://github.com/my-team/my-repo
~$ cd my-repo
~/my-repo$ rush init

它會產生這些檔案 (如需詳細資訊,請參閱設定檔參考)

檔案作用
rush.jsonRush 的主要設定檔
.gitattributes(如果您未使用 Git,請刪除此檔案。)
告知 Git 不要對 shrinkwrap 檔案執行合併作業,因為這樣做不安全。
.gitignore(如果您未使用 Git,請刪除此檔案。)
告知 Git 不要追蹤 Rush 建立的暫存檔案。
.github/workflows/ci.yml(如果您未使用 GitHub Actions,請刪除此檔案。)
設定 GitHub Actions 服務以使用 Rush 執行 PR 建置。
common/config/rush/.npmrcRush 使用此檔案來設定套件登錄,無論套件管理員是 PNPM、NPM 還是 Yarn。
common/config/rush/.npmrc-publish當發佈 NPM 套件時,Rush 會使用此檔案而非 .npmrc
common/config/rush/.pnpmfile.cjs(如果您選擇使用 NPM 或 Yarn 而不是 PNPM,請刪除此檔案。)
用於解決其 package.json 檔案中有錯誤的依賴問題。
common/config/rush/artifactory.json(如果您未使用 Artifactory,請刪除此檔案。)
用於定義自訂 rush setup 體驗以設定 Artifactory 認證。
common/config/rush/build-cache.json用於設定 Rush 的雲端建置快取。
common/config/rush/command-line.json您可以使用此選項來定義將成為 Rush 命令列一部分的自訂命令/參數。
common/config/rush/common-versions.json用於指定影響 Rush 儲存庫中所有專案的 NPM 依賴版本選取。
common/config/rush/experiments.json用於啟用 Rush 的實驗性功能。
common/config/rush/pnpm-config.json用於設定 PNPM 套件管理員在 rush installrush update 期間的行為。
common/config/rush/rush-plugins.json用於啟用 Rush 的外掛程式。
common/config/rush/version-policies.json用於定義進階發佈設定。
git-hooks/commit-msg.sample用於定義將由 rush install 啟用的 Git 鉤點範本。

注意:如果您的分支中已存在這些檔案,rush init 會發出警告,而且不會覆寫現有檔案。

接下來,將產生的檔案新增至 Git,並將其提交至您的分支

~/my-repo$ git add .
~/my-repo$ git commit -m "Initialize Rush repo"

步驟 3:自訂您的設定

範本檔案有許多文件和加上註解的範例程式碼片段。我們建議您查看它們,以熟悉基本選項和功能。

您可以隨時變更選項,但您應該事先考慮 rush.json 中的一些設定

  • 選擇套件管理員:範本預設為使用 PNPM,但您也可以使用 NPM 或 Yarn。如需指南,請參閱NPM vs PNPM vs Yarn

  • 檢查您的 Rush 版本:確定您的 rushVersion 設定是最新的版本,該版本顯示在 NPM 登錄中。

  • 檢查其他版本欄位:同時檢查您是否為任何其他適用的欄位使用最新的穩定版本,例如 pnpmVersionnpmVersionyarnVersionnodeSupportedVersionRange

  • 決定是否使用「類別資料夾」模型:請參閱 rush.json 中關於 projectFolderMinDepthprojectFolderMaxDepth 的註解,並規劃專案資料夾在單一儲存庫中的組織方式

  • 設定您的登錄存取權:初始的 .npmrc 檔案已設定為使用公用 NPM 登錄。如果您將使用私人登錄,您應該更新 common/config/rush/.npmrc 檔案。