-
Notifications
You must be signed in to change notification settings - Fork 618
/
Copy pathRushAmazonS3BuildCachePlugin.ts
99 lines (86 loc) · 3.13 KB
/
RushAmazonS3BuildCachePlugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import type { IRushPlugin, RushSession, RushConfiguration } from '@rushstack/rush-sdk';
import type {
IAmazonS3BuildCacheProviderOptionsAdvanced,
IAmazonS3BuildCacheProviderOptionsSimple
} from './AmazonS3BuildCacheProvider';
const PLUGIN_NAME: string = 'AmazonS3BuildCachePlugin';
/**
* @public
*/
export interface IAmazonS3ConfigurationJson {
/**
* (Required unless s3Endpoint is specified) The name of the bucket to use for build cache (e.g. "my-bucket").
*/
s3Bucket?: string;
/**
* (Required unless s3Bucket is specified) The Amazon S3 endpoint of the bucket to use for build cache (e.g. "my-bucket.s3.us-east-2.amazonaws.com" or "http://localhost:9000").
*/
s3Endpoint?: string;
/**
* The Amazon S3 region of the bucket to use for build cache (e.g. "us-east-1").
*/
s3Region: string;
/**
* An optional prefix ("folder") for cache items.
*/
s3Prefix?: string;
/**
* If set to true, allow writing to the cache. Defaults to false.
*/
isCacheWriteAllowed?: boolean;
}
/**
* @public
*/
export class RushAmazonS3BuildCachePlugin implements IRushPlugin {
public pluginName: string = PLUGIN_NAME;
public apply(rushSession: RushSession, rushConfig: RushConfiguration): void {
rushSession.hooks.initialize.tap(PLUGIN_NAME, () => {
rushSession.registerCloudBuildCacheProviderFactory('amazon-s3', async (buildCacheConfig) => {
type IBuildCache = typeof buildCacheConfig & {
amazonS3Configuration: IAmazonS3ConfigurationJson;
};
const { amazonS3Configuration } = buildCacheConfig as IBuildCache;
let options:
| IAmazonS3BuildCacheProviderOptionsAdvanced
| IAmazonS3BuildCacheProviderOptionsSimple
| undefined;
const { s3Endpoint, s3Bucket, s3Region } = amazonS3Configuration;
const s3Prefix: undefined | string = amazonS3Configuration.s3Prefix || undefined;
const isCacheWriteAllowed: boolean = !!amazonS3Configuration.isCacheWriteAllowed;
if (s3Prefix && s3Prefix[0] === '/') {
throw new Error('s3Prefix should not have a leading /');
}
// mutually exclusive
if (s3Bucket && s3Endpoint) {
throw new Error('Only one of "s3Bucket" or "s3Endpoint" must be provided.');
}
if (s3Endpoint) {
options = {
// IAmazonS3BuildCacheProviderOptionsAdvanced
s3Region,
s3Endpoint,
s3Prefix,
isCacheWriteAllowed
};
}
if (s3Bucket) {
options = {
// IAmazonS3BuildCacheProviderOptionsSimple
s3Region,
s3Bucket,
s3Prefix,
isCacheWriteAllowed
};
}
if (!options) {
throw new Error('You must provide either an s3Endpoint or s3Bucket');
}
const { AmazonS3BuildCacheProvider } = await import('./AmazonS3BuildCacheProvider');
return new AmazonS3BuildCacheProvider(options, rushSession);
});
});
}
}