@gradientedge/cdk-utils 8.144.0 → 8.145.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. package/dist/src/lib/cloudflare/common/construct.d.ts +5 -0
  2. package/dist/src/lib/cloudflare/common/construct.js +11 -0
  3. package/dist/src/lib/cloudflare/common/stack.d.ts +1 -0
  4. package/dist/src/lib/cloudflare/common/stack.js +1 -3
  5. package/dist/src/lib/cloudflare/common/types.d.ts +2 -0
  6. package/dist/src/lib/cloudflare/construct/index.d.ts +1 -0
  7. package/dist/src/lib/cloudflare/construct/index.js +17 -0
  8. package/dist/src/lib/cloudflare/construct/pages-static-site/index.d.ts +2 -0
  9. package/dist/src/lib/cloudflare/construct/pages-static-site/index.js +18 -0
  10. package/dist/src/lib/cloudflare/construct/pages-static-site/main.d.ts +55 -0
  11. package/dist/src/lib/cloudflare/construct/pages-static-site/main.js +90 -0
  12. package/dist/src/lib/cloudflare/construct/pages-static-site/types.d.ts +11 -0
  13. package/dist/src/lib/cloudflare/construct/pages-static-site/types.js +2 -0
  14. package/dist/src/lib/cloudflare/index.d.ts +1 -0
  15. package/dist/src/lib/cloudflare/index.js +1 -0
  16. package/dist/src/lib/cloudflare/services/page/main.d.ts +4 -2
  17. package/dist/src/lib/cloudflare/services/page/main.js +14 -1
  18. package/dist/src/lib/cloudflare/services/page/types.d.ts +6 -0
  19. package/dist/src/lib/cloudflare/services/zone/main.d.ts +1 -1
  20. package/dist/src/lib/cloudflare/services/zone/main.js +2 -4
  21. package/dist/src/lib/common/types.d.ts +1 -1
  22. package/package.json +13 -12
  23. package/src/lib/cloudflare/common/construct.ts +14 -1
  24. package/src/lib/cloudflare/common/stack.ts +1 -3
  25. package/src/lib/cloudflare/common/types.ts +2 -0
  26. package/src/lib/cloudflare/construct/index.ts +1 -0
  27. package/src/lib/cloudflare/construct/pages-static-site/index.ts +2 -0
  28. package/src/lib/cloudflare/construct/pages-static-site/main.ts +106 -0
  29. package/src/lib/cloudflare/construct/pages-static-site/types.ts +12 -0
  30. package/src/lib/cloudflare/index.ts +1 -0
  31. package/src/lib/cloudflare/services/page/main.ts +17 -2
  32. package/src/lib/cloudflare/services/page/types.ts +6 -0
  33. package/src/lib/cloudflare/services/zone/main.ts +3 -5
  34. package/src/lib/common/types.ts +1 -1
@@ -20,6 +20,11 @@ export declare class CommonCloudflareConstruct extends TerraformStack {
20
20
  * @summary Determine the fully qualified domain name based on domainName & subDomain
21
21
  */
22
22
  protected determineFullyQualifiedDomain(): void;
23
+ /**
24
+ * @summary Determine the account id based on the cdktf.json context
25
+ */
26
+ protected determineAccountId(): void;
27
+ protected determineApiToken(): void;
23
28
  /**
24
29
  * @summary Utility method to determine if the initialisation is in development (dev) stage
25
30
  * This is determined by the stage property injected via cdk context
@@ -31,6 +31,8 @@ class CommonCloudflareConstruct extends cdktf_1.TerraformStack {
31
31
  this.workerManager = new services_1.CloudflareWorkerManager();
32
32
  this.zoneManager = new services_1.CloudflareZoneManager();
33
33
  this.determineFullyQualifiedDomain();
34
+ this.determineAccountId();
35
+ this.determineApiToken();
34
36
  new provider_1.CloudflareProvider(this, `${this.id}-provider`, this.props);
35
37
  }
36
38
  /**
@@ -41,6 +43,15 @@ class CommonCloudflareConstruct extends cdktf_1.TerraformStack {
41
43
  ? `${this.props.subDomain}.${this.props.domainName}`
42
44
  : this.props.domainName;
43
45
  }
46
+ /**
47
+ * @summary Determine the account id based on the cdktf.json context
48
+ */
49
+ determineAccountId() {
50
+ this.props.accountId = new cdktf_1.TerraformVariable(this, `accountId`, {}).stringValue;
51
+ }
52
+ determineApiToken() {
53
+ this.props.apiToken = new cdktf_1.TerraformVariable(this, `apiToken`, {}).stringValue;
54
+ }
44
55
  /**
45
56
  * @summary Utility method to determine if the initialisation is in development (dev) stage
46
57
  * This is determined by the stage property injected via cdk context
@@ -25,6 +25,7 @@ export declare class CommonCloudflareStack extends TerraformStack {
25
25
  */
26
26
  protected determineConstructProps(props: CommonCloudflareStackProps): {
27
27
  accountId: any;
28
+ apiToken: any;
28
29
  domainName: any;
29
30
  extraContexts: any;
30
31
  features: any;
@@ -5,7 +5,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.CommonCloudflareStack = void 0;
7
7
  const fs_1 = __importDefault(require("fs"));
8
- const construct_1 = require("./construct");
9
8
  const app_root_path_1 = __importDefault(require("app-root-path"));
10
9
  const cdktf_1 = require("cdktf");
11
10
  const lodash_1 = __importDefault(require("lodash"));
@@ -32,8 +31,6 @@ class CommonCloudflareStack extends cdktf_1.TerraformStack {
32
31
  /* determine extra cdk stage contexts */
33
32
  this.determineStageContexts();
34
33
  this.props = this.determineConstructProps(props);
35
- /* initialise the construct */
36
- this.construct = new construct_1.CommonCloudflareConstruct(this, 'cdk-utils', this.props);
37
34
  }
38
35
  /**
39
36
  * @summary Method to determine the core CDK construct properties injected via context cdktf.json
@@ -43,6 +40,7 @@ class CommonCloudflareStack extends cdktf_1.TerraformStack {
43
40
  determineConstructProps(props) {
44
41
  return {
45
42
  accountId: this.node.tryGetContext('accountId'),
43
+ apiToken: this.node.tryGetContext('apiToken'),
46
44
  domainName: this.node.tryGetContext('domainName'),
47
45
  extraContexts: this.node.tryGetContext('extraContexts'),
48
46
  features: this.node.tryGetContext('features'),
@@ -4,4 +4,6 @@ import { BaseProps } from '../../common';
4
4
  */
5
5
  export interface CommonCloudflareStackProps extends BaseProps, CloudflareProviderConfig {
6
6
  accountId: string;
7
+ apiToken: string;
8
+ useExistingZone?: boolean;
7
9
  }
@@ -0,0 +1 @@
1
+ export * from './pages-static-site';
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./pages-static-site"), exports);
@@ -0,0 +1,2 @@
1
+ export * from './main';
2
+ export * from './types';
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./main"), exports);
18
+ __exportStar(require("./types"), exports);
@@ -0,0 +1,55 @@
1
+ import { DataCloudflareZone } from '@cdktf/provider-cloudflare/lib/data-cloudflare-zone';
2
+ import { PagesDomain } from '@cdktf/provider-cloudflare/lib/pages-domain';
3
+ import { PagesProject } from '@cdktf/provider-cloudflare/lib/pages-project';
4
+ import { Record } from '@cdktf/provider-cloudflare/lib/record';
5
+ import { Zone } from '@cdktf/provider-cloudflare/lib/zone';
6
+ import { Construct } from 'constructs';
7
+ import { CommonCloudflareConstruct } from '../../common';
8
+ import { CloudflarePagesStaticSiteProps } from './types';
9
+ /**
10
+ * @classdesc Provides a construct to create and deploy a cloudflare pages static site
11
+ * @example
12
+ * import { CloudflarePagesStaticSite, CloudflareStaticSiteProps } '@gradientedge/cdk-utils'
13
+ * import { Construct } from 'constructs'
14
+ *
15
+ * class CustomConstruct extends CloudflarePagesStaticSite {
16
+ * constructor(parent: Construct, id: string, props: CloudflareStaticSiteProps) {
17
+ * super(parent, id, props)
18
+ * this.props = props
19
+ * this.id = id
20
+ * this.initResources()
21
+ * }
22
+ * }
23
+ */
24
+ export declare class CloudflarePagesStaticSite extends CommonCloudflareConstruct {
25
+ props: CloudflarePagesStaticSiteProps;
26
+ sitePagesCnameRecord: Record;
27
+ sitePagesDomain: PagesDomain;
28
+ sitePagesProject: PagesProject;
29
+ siteZone: DataCloudflareZone | Zone;
30
+ constructor(parent: Construct, id: string, props: CloudflarePagesStaticSiteProps);
31
+ /**
32
+ * @summary Initialise and provision resources
33
+ */
34
+ protected initResources(): void;
35
+ /**
36
+ * @summary Resolve the zone to use for the static site
37
+ */
38
+ protected resolveZone(): void;
39
+ /**
40
+ * @summary Create the pages project
41
+ */
42
+ protected createProject(): void;
43
+ /**
44
+ * @summary Create the pages domain
45
+ */
46
+ protected createDomain(): void;
47
+ /**
48
+ * @summary Create the pages cname record
49
+ */
50
+ protected createRecord(): void;
51
+ /**
52
+ * @summary Deploy the pages project
53
+ */
54
+ protected deploySite(): void;
55
+ }
@@ -0,0 +1,90 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CloudflarePagesStaticSite = void 0;
4
+ const common_1 = require("../../common");
5
+ /**
6
+ * @classdesc Provides a construct to create and deploy a cloudflare pages static site
7
+ * @example
8
+ * import { CloudflarePagesStaticSite, CloudflareStaticSiteProps } '@gradientedge/cdk-utils'
9
+ * import { Construct } from 'constructs'
10
+ *
11
+ * class CustomConstruct extends CloudflarePagesStaticSite {
12
+ * constructor(parent: Construct, id: string, props: CloudflareStaticSiteProps) {
13
+ * super(parent, id, props)
14
+ * this.props = props
15
+ * this.id = id
16
+ * this.initResources()
17
+ * }
18
+ * }
19
+ */
20
+ class CloudflarePagesStaticSite extends common_1.CommonCloudflareConstruct {
21
+ /* static site resources */
22
+ sitePagesCnameRecord;
23
+ sitePagesDomain;
24
+ sitePagesProject;
25
+ siteZone;
26
+ constructor(parent, id, props) {
27
+ super(parent, id, props);
28
+ this.props = props;
29
+ this.id = id;
30
+ }
31
+ /**
32
+ * @summary Initialise and provision resources
33
+ */
34
+ initResources() {
35
+ this.resolveZone();
36
+ this.createProject();
37
+ this.createDomain();
38
+ this.createRecord();
39
+ this.deploySite();
40
+ }
41
+ /**
42
+ * @summary Resolve the zone to use for the static site
43
+ */
44
+ resolveZone() {
45
+ if (this.props.useExistingZone) {
46
+ this.siteZone = this.zoneManager.resolveZone(`${this.id}-zone`, this);
47
+ }
48
+ else {
49
+ this.siteZone = this.zoneManager.createZone(`${this.id}-zone`, this, this.props.siteZone);
50
+ }
51
+ }
52
+ /**
53
+ * @summary Create the pages project
54
+ */
55
+ createProject() {
56
+ this.sitePagesProject = this.pageManager.createPagesProject(`${this.id}-site-project`, this, this.props.sitePagesProject);
57
+ }
58
+ /**
59
+ * @summary Create the pages domain
60
+ */
61
+ createDomain() {
62
+ this.sitePagesDomain = this.pageManager.createPagesDomain(`${this.id}-site-domain`, this, {
63
+ accountId: this.props.accountId,
64
+ domain: `${this.props.siteSubDomain}.${this.props.domainName}`,
65
+ projectName: this.sitePagesProject.name,
66
+ });
67
+ }
68
+ /**
69
+ * @summary Create the pages cname record
70
+ */
71
+ createRecord() {
72
+ this.sitePagesCnameRecord = this.recordManager.createRecord(`${this.id}-site-record`, this, {
73
+ ...this.props.siteCnameRecord,
74
+ name: this.props.siteSubDomain,
75
+ value: `${this.sitePagesProject.name}.pages.dev`,
76
+ });
77
+ }
78
+ /**
79
+ * @summary Deploy the pages project
80
+ */
81
+ deploySite() {
82
+ this.pageManager.deployPagesProject(`${this.id}-deploy`, this, {
83
+ branch: this.props.siteBranch ?? 'main',
84
+ directory: this.props.siteAssetDir,
85
+ message: this.props.siteDeployMessage,
86
+ projectName: this.sitePagesProject.name,
87
+ });
88
+ }
89
+ }
90
+ exports.CloudflarePagesStaticSite = CloudflarePagesStaticSite;
@@ -0,0 +1,11 @@
1
+ import { CommonCloudflareStackProps } from '../../common';
2
+ import { PagesProjectProps, RecordProps, ZoneProps } from '../../services';
3
+ export interface CloudflarePagesStaticSiteProps extends CommonCloudflareStackProps {
4
+ siteAssetDir: string;
5
+ siteBranch?: string;
6
+ siteCnameRecord: RecordProps;
7
+ siteDeployMessage: string;
8
+ sitePagesProject: PagesProjectProps;
9
+ siteSubDomain: string;
10
+ siteZone: ZoneProps;
11
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,4 +1,5 @@
1
1
  export * from './common';
2
+ export * from './construct';
2
3
  export * from './services';
3
4
  export * from './types';
4
5
  export * from './utils';
@@ -15,6 +15,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./common"), exports);
18
+ __exportStar(require("./construct"), exports);
18
19
  __exportStar(require("./services"), exports);
19
20
  __exportStar(require("./types"), exports);
20
21
  __exportStar(require("./utils"), exports);
@@ -1,8 +1,9 @@
1
+ import { PageRule } from '@cdktf/provider-cloudflare/lib/page-rule';
1
2
  import { PagesDomain } from '@cdktf/provider-cloudflare/lib/pages-domain';
2
3
  import { PagesProject } from '@cdktf/provider-cloudflare/lib/pages-project';
4
+ import { LocalExec } from 'cdktf-local-exec';
3
5
  import { CommonCloudflareConstruct } from '../../common';
4
- import { PageRuleProps, PagesDomainProps, PagesProjectProps } from './types';
5
- import { PageRule } from '@cdktf/provider-cloudflare/lib/page-rule';
6
+ import { PageRuleProps, PagesDomainProps, PagesProjectDeployProps, PagesProjectProps } from './types';
6
7
  /**
7
8
  * @classdesc Provides operations on Cloudflare Pages
8
9
  * - A new instance of this class is injected into {@link CommonCloudflareConstruct} constructor.
@@ -45,4 +46,5 @@ export declare class CloudflarePageManager {
45
46
  * @see [CDKTF Page Rule Module]{@link https://github.com/cdktf/cdktf-provider-cloudflare/blob/main/docs/pageRule.typescript.md}
46
47
  */
47
48
  createPageRule(id: string, scope: CommonCloudflareConstruct, props: PageRuleProps): PageRule;
49
+ deployPagesProject(id: string, scope: CommonCloudflareConstruct, props: PagesProjectDeployProps): LocalExec;
48
50
  }
@@ -1,10 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.CloudflarePageManager = void 0;
4
+ const page_rule_1 = require("@cdktf/provider-cloudflare/lib/page-rule");
4
5
  const pages_domain_1 = require("@cdktf/provider-cloudflare/lib/pages-domain");
5
6
  const pages_project_1 = require("@cdktf/provider-cloudflare/lib/pages-project");
7
+ const cdktf_local_exec_1 = require("cdktf-local-exec");
6
8
  const utils_1 = require("../../utils");
7
- const page_rule_1 = require("@cdktf/provider-cloudflare/lib/page-rule");
8
9
  /**
9
10
  * @classdesc Provides operations on Cloudflare Pages
10
11
  * - A new instance of this class is injected into {@link CommonCloudflareConstruct} constructor.
@@ -82,5 +83,17 @@ class CloudflarePageManager {
82
83
  (0, utils_1.createCloudflareTfOutput)(`${id}-pageRuleId`, scope, pageRule.id);
83
84
  return pageRule;
84
85
  }
86
+ deployPagesProject(id, scope, props) {
87
+ if (!props)
88
+ throw `Props undefined for ${id}`;
89
+ const localExecProvider = new cdktf_local_exec_1.Provider(scope, `${id}`);
90
+ const branch = scope.isProductionStage() ? 'main' : props.branch;
91
+ const message = process.env.BUILD_NUMBER ?? props.message;
92
+ const deployment = new cdktf_local_exec_1.LocalExec(scope, `${id}-deploy-${new Date().toISOString()}`, {
93
+ command: `CLOUDFLARE_ACCOUNT_ID=${scope.props.accountId} CLOUDFLARE_API_TOKEN=${scope.props.apiToken} npx wrangler pages deploy ${props.directory} --project-name=${props.projectName} --branch=${props.branch} --commit-message=${message}`,
94
+ cwd: '',
95
+ });
96
+ return deployment;
97
+ }
85
98
  }
86
99
  exports.CloudflarePageManager = CloudflarePageManager;
@@ -7,3 +7,9 @@ export interface PagesDomainProps extends PagesDomainConfig {
7
7
  }
8
8
  export interface PageRuleProps extends PageRuleConfig {
9
9
  }
10
+ export interface PagesProjectDeployProps {
11
+ branch: string;
12
+ directory: string;
13
+ message: string;
14
+ projectName: string;
15
+ }
@@ -34,7 +34,7 @@ export declare class CloudflareZoneManager {
34
34
  * @see [CDKTF Zone Module]{@link https://github.com/cdktf/cdktf-provider-cloudflare/blob/main/docs/zone.typescript.md}
35
35
  */
36
36
  createZone(id: string, scope: CommonCloudflareConstruct, props: ZoneProps): Zone;
37
- resolveZone(id: string, scope: CommonCloudflareConstruct, options: ZoneOptions): DataCloudflareZone;
37
+ resolveZone(id: string, scope: CommonCloudflareConstruct, options?: ZoneOptions): DataCloudflareZone;
38
38
  /**
39
39
  * @summary Method to create a new zone cache reserve
40
40
  * @param id scoped id of the resource
@@ -49,12 +49,10 @@ class CloudflareZoneManager {
49
49
  return zone;
50
50
  }
51
51
  resolveZone(id, scope, options) {
52
- if (!options)
53
- throw `Options undefined for ${id}`;
54
52
  const zone = new data_cloudflare_zone_1.DataCloudflareZone(scope, `${id}-data-zone`, {
55
53
  accountId: scope.props.accountId,
56
- name: options.name ?? scope.props.domainName,
57
- zoneId: options.id,
54
+ name: options?.name ?? scope.props.domainName,
55
+ zoneId: options?.id,
58
56
  });
59
57
  return zone;
60
58
  }
@@ -2,7 +2,7 @@ export interface BaseProps {
2
2
  domainName: string;
3
3
  extraContexts?: string[];
4
4
  name: string;
5
- skipStageForARecords: boolean;
5
+ skipStageForARecords?: boolean;
6
6
  stage: string;
7
7
  stageContextPath?: string;
8
8
  subDomain?: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gradientedge/cdk-utils",
3
- "version": "8.144.0",
3
+ "version": "8.145.0",
4
4
  "description": "Utilities for AWS CDK provisioning",
5
5
  "main": "dist/index.js",
6
6
  "engines": {
@@ -47,17 +47,18 @@
47
47
  }
48
48
  },
49
49
  "dependencies": {
50
- "@aws-sdk/client-secrets-manager": "^3.454.0",
51
- "@aws-sdk/credential-providers": "^3.454.0",
50
+ "@aws-sdk/client-secrets-manager": "^3.458.0",
51
+ "@aws-sdk/credential-providers": "^3.458.0",
52
52
  "@aws-sdk/types": "^3.451.0",
53
- "@cdktf/provider-azurerm": "^11.0.5",
53
+ "@cdktf/provider-azurerm": "^11.0.6",
54
54
  "@cdktf/provider-cloudflare": "^10.0.3",
55
- "@types/lodash": "^4.14.201",
56
- "@types/node": "^20.9.2",
55
+ "@types/lodash": "^4.14.202",
56
+ "@types/node": "^20.10.0",
57
57
  "@types/uuid": "^9.0.7",
58
58
  "app-root-path": "^3.1.0",
59
- "aws-cdk-lib": "^2.110.0",
59
+ "aws-cdk-lib": "^2.110.1",
60
60
  "cdktf": "^0.19.1",
61
+ "cdktf-local-exec": "^0.4.8",
61
62
  "constructs": "^10.3.0",
62
63
  "lodash": "^4.17.21",
63
64
  "moment": "^2.29.4",
@@ -70,10 +71,10 @@
70
71
  "@babel/core": "^7.23.3",
71
72
  "@babel/eslint-parser": "^7.23.3",
72
73
  "@babel/plugin-proposal-class-properties": "^7.18.6",
73
- "@types/jest": "^29.5.8",
74
- "@typescript-eslint/eslint-plugin": "^6.11.0",
75
- "@typescript-eslint/parser": "^6.11.0",
76
- "aws-cdk": "^2.110.0",
74
+ "@types/jest": "^29.5.10",
75
+ "@typescript-eslint/eslint-plugin": "^6.12.0",
76
+ "@typescript-eslint/parser": "^6.12.0",
77
+ "aws-cdk": "^2.110.1",
77
78
  "better-docs": "^2.7.2",
78
79
  "codecov": "^3.8.3",
79
80
  "commitizen": "^4.3.0",
@@ -99,7 +100,7 @@
99
100
  "taffydb": "^2.7.3",
100
101
  "ts-jest": "^29.1.1",
101
102
  "ts-node": "^10.9.1",
102
- "typescript": "5.2.2"
103
+ "typescript": "5.3.2"
103
104
  },
104
105
  "optionalDependencies": {
105
106
  "prop-types": "^15.8.1",
@@ -1,5 +1,5 @@
1
1
  import { CloudflareProvider } from '@cdktf/provider-cloudflare/lib/provider'
2
- import { TerraformStack } from 'cdktf'
2
+ import { TerraformStack, TerraformVariable } from 'cdktf'
3
3
  import { Construct } from 'constructs'
4
4
  import { isDevStage, isPrdStage, isTestStage, isUatStage } from '../../common'
5
5
  import {
@@ -45,6 +45,8 @@ export class CommonCloudflareConstruct extends TerraformStack {
45
45
  this.zoneManager = new CloudflareZoneManager()
46
46
 
47
47
  this.determineFullyQualifiedDomain()
48
+ this.determineAccountId()
49
+ this.determineApiToken()
48
50
  new CloudflareProvider(this, `${this.id}-provider`, this.props)
49
51
  }
50
52
 
@@ -57,6 +59,17 @@ export class CommonCloudflareConstruct extends TerraformStack {
57
59
  : this.props.domainName
58
60
  }
59
61
 
62
+ /**
63
+ * @summary Determine the account id based on the cdktf.json context
64
+ */
65
+ protected determineAccountId(): void {
66
+ this.props.accountId = new TerraformVariable(this, `accountId`, {}).stringValue
67
+ }
68
+
69
+ protected determineApiToken(): void {
70
+ this.props.apiToken = new TerraformVariable(this, `apiToken`, {}).stringValue
71
+ }
72
+
60
73
  /**
61
74
  * @summary Utility method to determine if the initialisation is in development (dev) stage
62
75
  * This is determined by the stage property injected via cdk context
@@ -34,9 +34,6 @@ export class CommonCloudflareStack extends TerraformStack {
34
34
  this.determineStageContexts()
35
35
 
36
36
  this.props = this.determineConstructProps(props)
37
-
38
- /* initialise the construct */
39
- this.construct = new CommonCloudflareConstruct(this, 'cdk-utils', this.props)
40
37
  }
41
38
 
42
39
  /**
@@ -47,6 +44,7 @@ export class CommonCloudflareStack extends TerraformStack {
47
44
  protected determineConstructProps(props: CommonCloudflareStackProps) {
48
45
  return {
49
46
  accountId: this.node.tryGetContext('accountId'),
47
+ apiToken: this.node.tryGetContext('apiToken'),
50
48
  domainName: this.node.tryGetContext('domainName'),
51
49
  extraContexts: this.node.tryGetContext('extraContexts'),
52
50
  features: this.node.tryGetContext('features'),
@@ -5,4 +5,6 @@ import { BaseProps } from '../../common'
5
5
  */
6
6
  export interface CommonCloudflareStackProps extends BaseProps, CloudflareProviderConfig {
7
7
  accountId: string
8
+ apiToken: string
9
+ useExistingZone?: boolean
8
10
  }
@@ -0,0 +1 @@
1
+ export * from './pages-static-site'
@@ -0,0 +1,2 @@
1
+ export * from './main'
2
+ export * from './types'
@@ -0,0 +1,106 @@
1
+ import { DataCloudflareZone } from '@cdktf/provider-cloudflare/lib/data-cloudflare-zone'
2
+ import { PagesDomain } from '@cdktf/provider-cloudflare/lib/pages-domain'
3
+ import { PagesProject } from '@cdktf/provider-cloudflare/lib/pages-project'
4
+ import { Record } from '@cdktf/provider-cloudflare/lib/record'
5
+ import { Zone } from '@cdktf/provider-cloudflare/lib/zone'
6
+ import { Construct } from 'constructs'
7
+ import { CommonCloudflareConstruct } from '../../common'
8
+ import { CloudflarePagesStaticSiteProps } from './types'
9
+
10
+ /**
11
+ * @classdesc Provides a construct to create and deploy a cloudflare pages static site
12
+ * @example
13
+ * import { CloudflarePagesStaticSite, CloudflareStaticSiteProps } '@gradientedge/cdk-utils'
14
+ * import { Construct } from 'constructs'
15
+ *
16
+ * class CustomConstruct extends CloudflarePagesStaticSite {
17
+ * constructor(parent: Construct, id: string, props: CloudflareStaticSiteProps) {
18
+ * super(parent, id, props)
19
+ * this.props = props
20
+ * this.id = id
21
+ * this.initResources()
22
+ * }
23
+ * }
24
+ */
25
+ export class CloudflarePagesStaticSite extends CommonCloudflareConstruct {
26
+ declare props: CloudflarePagesStaticSiteProps
27
+
28
+ /* static site resources */
29
+ sitePagesCnameRecord: Record
30
+ sitePagesDomain: PagesDomain
31
+ sitePagesProject: PagesProject
32
+ siteZone: DataCloudflareZone | Zone
33
+
34
+ constructor(parent: Construct, id: string, props: CloudflarePagesStaticSiteProps) {
35
+ super(parent, id, props)
36
+ this.props = props
37
+ this.id = id
38
+ }
39
+
40
+ /**
41
+ * @summary Initialise and provision resources
42
+ */
43
+ protected initResources() {
44
+ this.resolveZone()
45
+ this.createProject()
46
+ this.createDomain()
47
+ this.createRecord()
48
+ this.deploySite()
49
+ }
50
+
51
+ /**
52
+ * @summary Resolve the zone to use for the static site
53
+ */
54
+ protected resolveZone() {
55
+ if (this.props.useExistingZone) {
56
+ this.siteZone = this.zoneManager.resolveZone(`${this.id}-zone`, this)
57
+ } else {
58
+ this.siteZone = this.zoneManager.createZone(`${this.id}-zone`, this, this.props.siteZone)
59
+ }
60
+ }
61
+
62
+ /**
63
+ * @summary Create the pages project
64
+ */
65
+ protected createProject() {
66
+ this.sitePagesProject = this.pageManager.createPagesProject(
67
+ `${this.id}-site-project`,
68
+ this,
69
+ this.props.sitePagesProject
70
+ )
71
+ }
72
+
73
+ /**
74
+ * @summary Create the pages domain
75
+ */
76
+ protected createDomain() {
77
+ this.sitePagesDomain = this.pageManager.createPagesDomain(`${this.id}-site-domain`, this, {
78
+ accountId: this.props.accountId,
79
+ domain: `${this.props.siteSubDomain}.${this.props.domainName}`,
80
+ projectName: this.sitePagesProject.name,
81
+ })
82
+ }
83
+
84
+ /**
85
+ * @summary Create the pages cname record
86
+ */
87
+ protected createRecord() {
88
+ this.sitePagesCnameRecord = this.recordManager.createRecord(`${this.id}-site-record`, this, {
89
+ ...this.props.siteCnameRecord,
90
+ name: this.props.siteSubDomain,
91
+ value: `${this.sitePagesProject.name}.pages.dev`,
92
+ })
93
+ }
94
+
95
+ /**
96
+ * @summary Deploy the pages project
97
+ */
98
+ protected deploySite() {
99
+ this.pageManager.deployPagesProject(`${this.id}-deploy`, this, {
100
+ branch: this.props.siteBranch ?? 'main',
101
+ directory: this.props.siteAssetDir,
102
+ message: this.props.siteDeployMessage,
103
+ projectName: this.sitePagesProject.name,
104
+ })
105
+ }
106
+ }
@@ -0,0 +1,12 @@
1
+ import { CommonCloudflareStackProps } from '../../common'
2
+ import { PagesProjectProps, RecordProps, ZoneProps } from '../../services'
3
+
4
+ export interface CloudflarePagesStaticSiteProps extends CommonCloudflareStackProps {
5
+ siteAssetDir: string
6
+ siteBranch?: string
7
+ siteCnameRecord: RecordProps
8
+ siteDeployMessage: string
9
+ sitePagesProject: PagesProjectProps
10
+ siteSubDomain: string
11
+ siteZone: ZoneProps
12
+ }
@@ -1,4 +1,5 @@
1
1
  export * from './common'
2
+ export * from './construct'
2
3
  export * from './services'
3
4
  export * from './types'
4
5
  export * from './utils'
@@ -1,9 +1,10 @@
1
+ import { PageRule } from '@cdktf/provider-cloudflare/lib/page-rule'
1
2
  import { PagesDomain } from '@cdktf/provider-cloudflare/lib/pages-domain'
2
3
  import { PagesProject } from '@cdktf/provider-cloudflare/lib/pages-project'
4
+ import { LocalExec, Provider } from 'cdktf-local-exec'
3
5
  import { CommonCloudflareConstruct } from '../../common'
4
6
  import { createCloudflareTfOutput } from '../../utils'
5
- import { PageRuleProps, PagesDomainProps, PagesProjectProps } from './types'
6
- import { PageRule } from '@cdktf/provider-cloudflare/lib/page-rule'
7
+ import { PageRuleProps, PagesDomainProps, PagesProjectDeployProps, PagesProjectProps } from './types'
7
8
 
8
9
  /**
9
10
  * @classdesc Provides operations on Cloudflare Pages
@@ -91,4 +92,18 @@ export class CloudflarePageManager {
91
92
 
92
93
  return pageRule
93
94
  }
95
+
96
+ public deployPagesProject(id: string, scope: CommonCloudflareConstruct, props: PagesProjectDeployProps) {
97
+ if (!props) throw `Props undefined for ${id}`
98
+
99
+ const localExecProvider = new Provider(scope, `${id}`)
100
+ const branch = scope.isProductionStage() ? 'main' : props.branch
101
+ const message = process.env.BUILD_NUMBER ?? props.message
102
+ const deployment = new LocalExec(scope, `${id}-deploy-${new Date().toISOString()}`, {
103
+ command: `CLOUDFLARE_ACCOUNT_ID=${scope.props.accountId} CLOUDFLARE_API_TOKEN=${scope.props.apiToken} npx wrangler pages deploy ${props.directory} --project-name=${props.projectName} --branch=${props.branch} --commit-message=${message}`,
104
+ cwd: '',
105
+ })
106
+
107
+ return deployment
108
+ }
94
109
  }
@@ -5,3 +5,9 @@ import { PagesProjectConfig } from '@cdktf/provider-cloudflare/lib/pages-project
5
5
  export interface PagesProjectProps extends PagesProjectConfig {}
6
6
  export interface PagesDomainProps extends PagesDomainConfig {}
7
7
  export interface PageRuleProps extends PageRuleConfig {}
8
+ export interface PagesProjectDeployProps {
9
+ branch: string
10
+ directory: string
11
+ message: string
12
+ projectName: string
13
+ }
@@ -60,13 +60,11 @@ export class CloudflareZoneManager {
60
60
  return zone
61
61
  }
62
62
 
63
- public resolveZone(id: string, scope: CommonCloudflareConstruct, options: ZoneOptions) {
64
- if (!options) throw `Options undefined for ${id}`
65
-
63
+ public resolveZone(id: string, scope: CommonCloudflareConstruct, options?: ZoneOptions) {
66
64
  const zone = new DataCloudflareZone(scope, `${id}-data-zone`, {
67
65
  accountId: scope.props.accountId,
68
- name: options.name ?? scope.props.domainName,
69
- zoneId: options.id,
66
+ name: options?.name ?? scope.props.domainName,
67
+ zoneId: options?.id,
70
68
  })
71
69
 
72
70
  return zone
@@ -2,7 +2,7 @@ export interface BaseProps {
2
2
  domainName: string
3
3
  extraContexts?: string[]
4
4
  name: string
5
- skipStageForARecords: boolean
5
+ skipStageForARecords?: boolean
6
6
  stage: string
7
7
  stageContextPath?: string
8
8
  subDomain?: string