forked from shellscape/dot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathamplify.ts
More file actions
52 lines (43 loc) · 1.42 KB
/
Copy pathamplify.ts
File metadata and controls
52 lines (43 loc) · 1.42 KB
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
import * as Amplify from '@aws-cdk/aws-amplify-alpha';
import { RemovalPolicy } from 'aws-cdk-lib';
import { Asset } from 'aws-cdk-lib/aws-s3-assets';
import { DotStack } from '../constructs/Stack';
interface AddAmplifyAppOptions {
distPath: string;
domainName?: string;
environmentVariables?: { [key: string]: string };
name: string;
pwaRedirect?: boolean;
scope: DotStack;
subdomain?: string;
}
interface AddAmplifyAppResult {
app: Amplify.App;
}
export const addAmplifyApp = (options: AddAmplifyAppOptions): AddAmplifyAppResult => {
const { distPath, domainName, environmentVariables = {}, name, pwaRedirect, scope } = options;
const subdomain = options.subdomain ?? scope.envName;
const baseName = DotStack.baseName(name, '-app');
const appName = scope.resourceName(baseName);
const asset = new Asset(scope, `${appName}-asset`, { path: distPath });
const app = new Amplify.App(scope, appName, {
appName
});
const branch = app.addBranch(scope.envName, { asset, environmentVariables });
app.applyRemovalPolicy(RemovalPolicy.DESTROY);
// Note: needed for PWA routing
if (pwaRedirect) {
app.addCustomRule({
source: '/<*>',
status: Amplify.RedirectStatus.NOT_FOUND_REWRITE,
target: '/index.html'
});
}
if (domainName) {
const domain = app.addDomain(domainName, {
enableAutoSubdomain: false
});
domain.mapSubDomain(branch, subdomain);
}
return { app };
};