

Amazon CodeCatalyst 不再向新客戶開放。現有客戶可以繼續正常使用該服務。如需詳細資訊，請參閱[如何從 CodeCatalyst 遷移](migration.md)。

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 將儲存庫和原始程式碼元件新增至藍圖
<a name="comp-repo-source-bp"></a>

Amazon CodeCatalyst 會使用儲存庫來存放程式碼。儲存庫會採用名稱做為輸入。大多數元件存放在儲存庫中，例如原始程式碼檔案、工作流程和其他元件，例如受管開發環境 (MDE)。來源儲存庫元件也會匯出用於管理檔案和靜態資產的元件。儲存庫有名稱限制。如需詳細資訊，請參閱[在 CodeCatalyst 中使用來源儲存庫儲存程式碼並進行協同合作儲存程式碼並與來源儲存庫協作](source.md)。

```
const repository = new SourceRepository(this, {
  title: 'my-new-repository-title',
});
```

**匯入 Amazon CodeCatalyst 藍圖儲存庫和原始程式碼元件**

在您的 `blueprint.ts`檔案中，新增下列項目：

```
import {...} from '@caws-blueprint-component/caws-source-repositories'
```

**Topics**
+ [新增檔案](#repo-add-file-bp)
+ [新增一般檔案](#repo-add-generic-file-bp)
+ [複製檔案](#repo-copy-file-bp)
+ [鎖定多個檔案](#target-multiple-files-bp)
+ [建立新的儲存庫並新增檔案](#repo-code-examples-bp)

## 新增檔案
<a name="repo-add-file-bp"></a>

您可以使用 `SourceFile` 建構將文字檔案寫入儲存庫。操作是最常見的使用案例之一，並採用儲存庫、檔案路徑和文字內容。如果檔案路徑不存在於儲存庫中，元件會建立所有必要的資料夾。

```
new SourceFile(repository, `path/to/my/file/in/repo/file.txt`, 'my file contents');
```

**注意**  
如果您將兩個檔案寫入相同儲存庫中的相同位置，則最新的實作會覆寫先前的檔案。您可以使用 功能來分層產生的程式碼，而且在延伸自訂藍圖可能已產生的程式碼時特別有用。

## 新增一般檔案
<a name="repo-add-generic-file-bp"></a>

您可以將任意位元寫入儲存庫。您可以從緩衝區讀取並使用 `File` 建構。

```
new File(repository, `path/to/my/file/in/repo/file.img`, new Buffer(...));

new File(repository, `path/to/my/file/in/repo/new-img.img`, new StaticAsset('path/to/image.png').content());
```

## 複製檔案
<a name="repo-copy-file-bp"></a>

您可以複製並貼上入門程式碼，然後在該基礎上產生更多程式碼，以開始使用產生的程式碼。將程式碼放在 `static-assets` 目錄中，然後使用 `StaticAsset` 建構將該程式碼設為目標。在此情況下，路徑一律從 `static-assets` 目錄的根目錄開始。

```
const starterCode = new StaticAsset('path/to/file/file.txt')
const starterCodeText = new StaticAsset('path/to/file/file.txt').toString()
const starterCodeRawContent = new StaticAsset('path/to/image/hello.png').content()

const starterCodePath = new StaticAsset('path/to/image/hello.png').path()
// starterCodePath is equal to 'path/to/image/hello.png'
```

的子類別`StaticAsset`為 `SubstitutionAsset`。子類別的功能完全相同，但您可以改為對檔案執行八字形替換。其有助於執行copy-and-replace樣式產生。

靜態資產替換使用八字形範本引擎來轉譯植入產生來源儲存庫的靜態檔案。轉譯期間會套用八字形範本規則，這表示所有值預設為 HTML 編碼。若要轉譯未逸出的 HTML，請使用三重八字語法 `{{{name}}}`。如需詳細資訊，請參閱[八字形範本規則](https://github.com/janl/mustache.js?tab=readme-ov-file#variables)。

**注意**  
對無法解譯文字的檔案執行替代 可能會產生錯誤。

```
const starterCodeText = new SubstitionAsset('path/to/file/file.txt').subsitite({
  'my_variable': 'subbed value1',
  'another_variable': 'subbed value2'
})
```

## 鎖定多個檔案
<a name="target-multiple-files-bp"></a>

靜態資產支援透過 上的靜態函數`StaticAsset`及其名為 的子類別來鎖定 glob`findAll(...)`，這會傳回預先載入其路徑、內容等的靜態資產清單。您可以將清單與`File`建構連結，以複製內容並貼到 `static-assets`目錄中。

```
new File(repository, `path/to/my/file/in/repo/file.img`, new Buffer(...));

new File(repository, `path/to/my/file/in/repo/new-img.img`, new StaticAsset('path/to/image.png').content());
```

## 建立新的儲存庫並新增檔案
<a name="repo-code-examples-bp"></a>

您可以使用儲存庫元件，在產生的專案中建立新的儲存庫。然後，您可以將檔案或工作流程新增至建立的儲存庫。

```
import { SourceRepository } from '@amazon-codecatalyst/codecatalyst-source-repositories';
...
const repository = new SourceRepository(this, { title: 'myRepo' });
```

下列範例示範如何將檔案和工作流程新增至現有的儲存庫：

```
import { SourceFile } from '@amazon-codecatalyst/codecatalyst-source-repositories';
import { Workflow } from '@amazon-codecatalyst/codecatalyst-workflows';
...
new SourceFile(repository, 'README.md', 'This is the content of my readme');
new Workflow(this, repository, {/**...workflowDefinition...**/});
```

結合這兩項程式碼，會產生名為 的單一儲存庫，`myRepo`其中包含來源檔案`README.md`和根目錄的 CodeCatalyst 工作流程。