-
-
Notifications
You must be signed in to change notification settings - Fork 961
/
get-sites.ts
75 lines (64 loc) · 2.09 KB
/
get-sites.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
import type {GcpRegion} from '../pricing/gcp-regions';
import {REMOTION_BUCKET_PREFIX} from '../shared/constants';
import {getCloudStorageClient} from './helpers/get-cloud-storage-client';
export type Site = {
bucketName: string;
id: string;
serveUrl: string;
bucketRegion: GcpRegion;
};
export type BucketWithLocation = {
name: string;
creationDate: number;
region: GcpRegion;
};
export type GetSitesOutput = {
sites: Site[];
buckets: BucketWithLocation[];
};
/**
* @description Gets all the deployed sites for a certain GCP project, within the specified region.
* @see [Documentation](https://remotion.dev/docs/cloudrun/getsites)
* @param {GcpRegion} params.region The GCP region that you want to query for.
* @returns {Promise<GetSitesOutput>} A Promise containing an object with `sites` and `bucket` keys. Consult documentation for details.
*/
export const getSites = async (
region: GcpRegion | 'all regions',
): Promise<GetSitesOutput> => {
const cloudStorageClient = getCloudStorageClient();
const buckets: BucketWithLocation[] = [];
const sites: Site[] = [];
const [fetchedBuckets] = await cloudStorageClient.getBuckets();
for (const bucket of fetchedBuckets) {
if (
bucket.name?.startsWith(REMOTION_BUCKET_PREFIX) &&
(region === 'all regions' ||
bucket.metadata.location === region.toUpperCase())
) {
const bucketObject = {
name: bucket.name,
creationDate: new Date(bucket.metadata?.timeCreated).getTime(),
region: bucket.metadata.location.toLowerCase(),
};
buckets.push(bucketObject);
}
}
for (const bucket of buckets) {
const [, , apiResponse] = await cloudStorageClient
.bucket(bucket.name)
.getFiles({autoPaginate: false, delimiter: '/', prefix: 'sites/'});
if (!apiResponse.prefixes) {
continue; // no sites folder within bucket
}
for (const prefix of apiResponse.prefixes) {
const sitePath = prefix.split('/');
sites.push({
bucketName: bucket.name,
id: sitePath[1],
serveUrl: `https://storage.googleapis.com/${bucket.name}/${prefix}index.html`,
bucketRegion: bucket.region,
});
}
}
return {sites, buckets};
};