@byaga/cdk-patterns 0.6.1 → 0.8.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 (62) hide show
  1. package/.fleet/run.json +11 -0
  2. package/lib/ApiCertificate.d.ts +2 -2
  3. package/lib/ApiCertificate.js +4 -5
  4. package/lib/CognitoApiGatewayAuthorizer.d.ts +19 -0
  5. package/lib/CognitoApiGatewayAuthorizer.js +24 -0
  6. package/lib/DeployStack.d.ts +20 -0
  7. package/lib/DeployStack.js +48 -0
  8. package/lib/DynamoDbTable.d.ts +12 -0
  9. package/lib/DynamoDbTable.js +34 -0
  10. package/lib/FunctionIntegration.d.ts +21 -0
  11. package/lib/FunctionIntegration.js +61 -0
  12. package/lib/ICorsConfig.d.ts +5 -0
  13. package/lib/ICorsConfig.js +2 -0
  14. package/lib/NodeJsLambda.d.ts +13 -0
  15. package/lib/NodeJsLambda.js +28 -0
  16. package/lib/Output.d.ts +5 -0
  17. package/lib/Output.js +13 -0
  18. package/lib/RestApi.d.ts +28 -0
  19. package/lib/RestApi.js +142 -0
  20. package/lib/Role.d.ts +2 -2
  21. package/lib/Role.js +2 -5
  22. package/lib/SsmParameter.d.ts +18 -0
  23. package/lib/SsmParameter.js +40 -0
  24. package/lib/StaticWebSite.d.ts +12 -0
  25. package/lib/StaticWebSite.js +107 -0
  26. package/lib/index.d.ts +7 -1
  27. package/lib/index.js +15 -3
  28. package/lib/methods/apply-honeycomb-to-lambda.d.ts +3 -0
  29. package/lib/methods/apply-honeycomb-to-lambda.js +22 -0
  30. package/lib/methods/build-node-source.d.ts +13 -0
  31. package/lib/methods/build-node-source.js +105 -0
  32. package/lib/methods/duration.d.ts +8 -0
  33. package/lib/methods/duration.js +22 -0
  34. package/lib/methods/empty-directory.d.ts +2 -0
  35. package/lib/methods/empty-directory.js +47 -0
  36. package/lib/methods/generate-hash.d.ts +15 -0
  37. package/lib/methods/generate-hash.js +59 -0
  38. package/lib/methods/get-source-directory.d.ts +4 -0
  39. package/lib/methods/get-source-directory.js +37 -0
  40. package/lib/methods/walk-directory.d.ts +14 -0
  41. package/lib/methods/walk-directory.js +48 -0
  42. package/package.json +4 -2
  43. package/src/ApiCertificate.ts +6 -6
  44. package/src/CognitoApiGatewayAuthorizer.ts +25 -0
  45. package/src/{IDeployStack.ts → DeployStack.ts} +4 -4
  46. package/src/DynamoDbTable.ts +40 -0
  47. package/src/FunctionIntegration.ts +83 -0
  48. package/src/ICorsConfig.ts +5 -0
  49. package/src/NodeJsLambda.ts +31 -0
  50. package/src/Output.ts +11 -0
  51. package/src/RestApi.ts +178 -0
  52. package/src/Role.ts +5 -7
  53. package/src/SsmParameter.ts +47 -0
  54. package/src/StaticWebSite.ts +99 -0
  55. package/src/index.ts +7 -1
  56. package/src/methods/apply-honeycomb-to-lambda.ts +22 -0
  57. package/src/methods/build-node-source.ts +97 -0
  58. package/src/methods/duration.ts +24 -0
  59. package/src/methods/empty-directory.ts +19 -0
  60. package/src/methods/generate-hash.ts +49 -0
  61. package/src/methods/get-source-directory.ts +11 -0
  62. package/src/methods/walk-directory.ts +30 -0
@@ -0,0 +1,12 @@
1
+ /// <reference types="node" />
2
+ import { DeployStack } from "./DeployStack";
3
+ import IDomainConfig from "./IDomainConfig";
4
+ interface StaticWebSiteConfig {
5
+ srcDir?: string;
6
+ domain: IDomainConfig;
7
+ env: NodeJS.ProcessEnv;
8
+ }
9
+ export declare class StaticWebSite {
10
+ constructor(stack: DeployStack, id: string, props: StaticWebSiteConfig);
11
+ }
12
+ export {};
@@ -0,0 +1,107 @@
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.StaticWebSite = void 0;
30
+ const core_1 = require("aws-cdk-lib/core");
31
+ const Output_1 = require("./Output");
32
+ const child_process_1 = require("child_process");
33
+ const fs = __importStar(require("fs-extra"));
34
+ const path = __importStar(require("path"));
35
+ const aws_s3_1 = require("aws-cdk-lib/aws-s3");
36
+ const aws_s3_deployment_1 = require("aws-cdk-lib/aws-s3-deployment");
37
+ const ApiCertificate_1 = __importDefault(require("./ApiCertificate"));
38
+ const aws_cloudfront_1 = require("aws-cdk-lib/aws-cloudfront");
39
+ const aws_iam_1 = require("aws-cdk-lib/aws-iam");
40
+ const aws_route53_1 = require("aws-cdk-lib/aws-route53");
41
+ const aws_route53_targets_1 = require("aws-cdk-lib/aws-route53-targets");
42
+ class StaticWebSite {
43
+ constructor(stack, id, props) {
44
+ console.log('Deploying Static Web Site', id);
45
+ const srcDir = path.resolve("../src/", props.srcDir || '');
46
+ const buildDir = path.resolve("../dist/", props.srcDir || '');
47
+ console.log('Installing Prod Dependencies', id);
48
+ (0, child_process_1.execSync)('npm i --production --quiet', {
49
+ cwd: srcDir
50
+ });
51
+ console.log('Building UI Source', id);
52
+ (0, child_process_1.execSync)('npm run export', {
53
+ cwd: srcDir,
54
+ //env: props.env
55
+ });
56
+ fs.copySync(path.resolve(srcDir, "./out"), buildDir);
57
+ console.log('Creating HTTPS Certificate', id + '-certificate');
58
+ const certificate = new ApiCertificate_1.default(stack, id + '-certificate', props.domain);
59
+ console.log('Deploying Site Content Bucket', stack.genId(id, "content-bucket"));
60
+ const s3BucketSource = new aws_s3_1.Bucket(stack, stack.genId(id, "content-bucket"), {
61
+ bucketName: certificate.domain,
62
+ websiteIndexDocument: "index.html",
63
+ websiteErrorDocument: "error.html",
64
+ publicReadAccess: true,
65
+ removalPolicy: core_1.RemovalPolicy.DESTROY,
66
+ autoDeleteObjects: true
67
+ });
68
+ new Output_1.Output(stack, `${id}-content-bucket`, s3BucketSource.bucketName);
69
+ const cloudFrontPolicy = new aws_iam_1.PolicyStatement({
70
+ actions: ['s3:GetBucket*', 's3:GetObject*', 's3:List*'],
71
+ resources: [s3BucketSource.bucketArn, s3BucketSource.bucketArn + '/*'],
72
+ });
73
+ new aws_s3_deployment_1.BucketDeployment(stack, stack.genId(id, 'bucket-deployment'), {
74
+ sources: [aws_s3_deployment_1.Source.asset(buildDir)],
75
+ destinationBucket: s3BucketSource
76
+ });
77
+ const originAccessIdentity = new aws_cloudfront_1.OriginAccessIdentity(stack, "WebsiteOAI");
78
+ cloudFrontPolicy.addArnPrincipal(originAccessIdentity.cloudFrontOriginAccessIdentityS3CanonicalUserId);
79
+ const distro = new aws_cloudfront_1.CloudFrontWebDistribution(stack, stack.genId(id, 'cloudfront-web-distribution'), {
80
+ enabled: true,
81
+ //priceClass:
82
+ originConfigs: [{
83
+ s3OriginSource: {
84
+ s3BucketSource,
85
+ originAccessIdentity
86
+ },
87
+ behaviors: [{
88
+ allowedMethods: aws_cloudfront_1.CloudFrontAllowedMethods.GET_HEAD_OPTIONS,
89
+ isDefaultBehavior: true
90
+ }]
91
+ }],
92
+ viewerProtocolPolicy: aws_cloudfront_1.ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
93
+ viewerCertificate: aws_cloudfront_1.ViewerCertificate.fromAcmCertificate(certificate, {
94
+ aliases: [certificate.domain],
95
+ sslMethod: aws_cloudfront_1.SSLMethod.SNI
96
+ })
97
+ });
98
+ new Output_1.Output(stack, `${id}-base-url`, "https://" + certificate.domain);
99
+ console.log('Setting Route53 A-Name Record', props.domain.domainName);
100
+ new aws_route53_1.ARecord(stack, stack.genId('alias'), {
101
+ zone: certificate.hostedZone,
102
+ recordName: certificate.domain,
103
+ target: aws_route53_1.RecordTarget.fromAlias(new aws_route53_targets_1.CloudFrontTarget(distro))
104
+ });
105
+ }
106
+ }
107
+ exports.StaticWebSite = StaticWebSite;
package/lib/index.d.ts CHANGED
@@ -1,6 +1,12 @@
1
1
  export { ApiCertificate } from "./ApiCertificate";
2
- export { IDeployStack } from "./IDeployStack";
2
+ export { DeployStack } from "./DeployStack";
3
3
  export { IDomainConfig } from "./IDomainConfig";
4
4
  export { IHostedZoneConfig } from "./IHostedZoneConfig";
5
5
  export { IStackArguments } from "./IStackArguments";
6
6
  export { Role } from "./Role";
7
+ export { StaticWebSite } from "./StaticWebSite";
8
+ export { Output } from "./Output";
9
+ export { RestApi } from "./RestApi";
10
+ export { NodeJsLambda } from "./NodeJsLambda";
11
+ export { DynamoDbTable } from "./DynamoDbTable";
12
+ export { FunctionIntegration } from "./FunctionIntegration";
package/lib/index.js CHANGED
@@ -1,9 +1,21 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Role = exports.IDeployStack = exports.ApiCertificate = void 0;
3
+ exports.FunctionIntegration = exports.DynamoDbTable = exports.NodeJsLambda = exports.RestApi = exports.Output = exports.StaticWebSite = exports.Role = exports.DeployStack = exports.ApiCertificate = void 0;
4
4
  var ApiCertificate_1 = require("./ApiCertificate");
5
5
  Object.defineProperty(exports, "ApiCertificate", { enumerable: true, get: function () { return ApiCertificate_1.ApiCertificate; } });
6
- var IDeployStack_1 = require("./IDeployStack");
7
- Object.defineProperty(exports, "IDeployStack", { enumerable: true, get: function () { return IDeployStack_1.IDeployStack; } });
6
+ var DeployStack_1 = require("./DeployStack");
7
+ Object.defineProperty(exports, "DeployStack", { enumerable: true, get: function () { return DeployStack_1.DeployStack; } });
8
8
  var Role_1 = require("./Role");
9
9
  Object.defineProperty(exports, "Role", { enumerable: true, get: function () { return Role_1.Role; } });
10
+ var StaticWebSite_1 = require("./StaticWebSite");
11
+ Object.defineProperty(exports, "StaticWebSite", { enumerable: true, get: function () { return StaticWebSite_1.StaticWebSite; } });
12
+ var Output_1 = require("./Output");
13
+ Object.defineProperty(exports, "Output", { enumerable: true, get: function () { return Output_1.Output; } });
14
+ var RestApi_1 = require("./RestApi");
15
+ Object.defineProperty(exports, "RestApi", { enumerable: true, get: function () { return RestApi_1.RestApi; } });
16
+ var NodeJsLambda_1 = require("./NodeJsLambda");
17
+ Object.defineProperty(exports, "NodeJsLambda", { enumerable: true, get: function () { return NodeJsLambda_1.NodeJsLambda; } });
18
+ var DynamoDbTable_1 = require("./DynamoDbTable");
19
+ Object.defineProperty(exports, "DynamoDbTable", { enumerable: true, get: function () { return DynamoDbTable_1.DynamoDbTable; } });
20
+ var FunctionIntegration_1 = require("./FunctionIntegration");
21
+ Object.defineProperty(exports, "FunctionIntegration", { enumerable: true, get: function () { return FunctionIntegration_1.FunctionIntegration; } });
@@ -0,0 +1,3 @@
1
+ import { FunctionProps } from "aws-cdk-lib/aws-lambda";
2
+ import { DeployStack } from "../DeployStack";
3
+ export declare function applyHoneycombToLambda(stack: DeployStack, props: FunctionProps): FunctionProps;
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.applyHoneycombToLambda = void 0;
4
+ const aws_lambda_1 = require("aws-cdk-lib/aws-lambda");
5
+ const SsmParameter_1 = require("../SsmParameter");
6
+ let honeyCombLayer;
7
+ function applyHoneycombToLambda(stack, props) {
8
+ if (!honeyCombLayer)
9
+ honeyCombLayer = aws_lambda_1.LayerVersion.fromLayerVersionArn(stack, stack.genId('hc-layer'), `arn:aws:lambda:${stack.region}:702835727665:layer:honeycomb-lambda-extension-x86_64-v11-1-1:1`);
10
+ const layers = props.layers ? [...props.layers] : [];
11
+ const environment = props.environment || {};
12
+ layers.push(honeyCombLayer);
13
+ environment.LIBHONEY_API_KEY = new SsmParameter_1.SsmParameter(stack, `/honeycomb/api-key`).stringValue;
14
+ environment.LIBHONEY_DATASET = new SsmParameter_1.SsmParameter(stack, `/honeycomb/dataset`).stringValue;
15
+ environment.LOGS_API_DISABLE_PLATFORM_MSGS = "true";
16
+ return {
17
+ ...props,
18
+ layers,
19
+ environment
20
+ };
21
+ }
22
+ exports.applyHoneycombToLambda = applyHoneycombToLambda;
@@ -0,0 +1,13 @@
1
+ import { IIgnoreOptions } from "./walk-directory";
2
+ export declare const defaultIgnore: IIgnoreOptions;
3
+ interface IBuildOptions {
4
+ dir?: string;
5
+ ignore?: IIgnoreOptions;
6
+ }
7
+ export declare function buildNodeSource(type: string, id: string, options?: IBuildOptions): {
8
+ buildDir: string;
9
+ moduleChanged: boolean;
10
+ packageChanged: boolean;
11
+ sourceChanged: boolean;
12
+ };
13
+ export {};
@@ -0,0 +1,105 @@
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.buildNodeSource = exports.defaultIgnore = void 0;
30
+ const path = __importStar(require("path"));
31
+ const fse = __importStar(require("fs-extra"));
32
+ const fs = __importStar(require("fs"));
33
+ const child_process_1 = require("child_process");
34
+ const generate_hash_1 = require("./generate-hash");
35
+ const get_source_directory_1 = require("./get-source-directory");
36
+ const duration_1 = __importDefault(require("./duration"));
37
+ const empty_directory_1 = __importDefault(require("./empty-directory"));
38
+ const distributionRoot = path.resolve(process.cwd(), "../dist");
39
+ const hashRoot = path.join(distributionRoot, "hash");
40
+ fse.ensureDirSync(hashRoot);
41
+ exports.defaultIgnore = {
42
+ included: (p) => {
43
+ return !p.name.startsWith(".") &&
44
+ !p.name.endsWith(".md") &&
45
+ !p.name.includes(".test.") &&
46
+ p.name !== 'package-lock.json';
47
+ },
48
+ childrenIncluded: (p) => {
49
+ return p.name === "node_modules";
50
+ }
51
+ };
52
+ function buildNodeSource(type, id, options = {}) {
53
+ const { dir = '' } = options;
54
+ const ignore = {
55
+ ...exports.defaultIgnore, ...options.ignore
56
+ };
57
+ const srcDir = (0, get_source_directory_1.getSourceDirectory)(type, id);
58
+ const buildDir = (0, get_source_directory_1.getBuildDirectory)(type, id);
59
+ const hashFilePath = path.join(hashRoot, `${type}-${id}-hash.txt`);
60
+ const folderExists = fs.existsSync(buildDir);
61
+ const hashFileExists = folderExists && fse.existsSync(hashFilePath);
62
+ const prevHash = (hashFileExists ? fse.readJsonSync(hashFilePath) : {});
63
+ const hash = (0, generate_hash_1.generateHash)(srcDir, { ignore });
64
+ const nextHash = JSON.parse((0, generate_hash_1.generateHash)(srcDir, { ignore }));
65
+ const moduleChanged = JSON.stringify(prevHash, null, '\t') !== hash;
66
+ let packageChanged = false, sourceChanged = false;
67
+ if (moduleChanged) {
68
+ const prevPackageHash = findPackageHash(prevHash);
69
+ const packageHash = findPackageHash(nextHash);
70
+ packageChanged = prevPackageHash !== packageHash;
71
+ sourceChanged = sourceWasUpdated(prevHash, nextHash);
72
+ if (sourceChanged) {
73
+ const rmComplete = (0, duration_1.default)();
74
+ if (folderExists)
75
+ (0, empty_directory_1.default)(buildDir, { childrenExcluded: folder => folder.name === 'node_modules' });
76
+ console.log('Cleanup Duration (ms)', rmComplete());
77
+ const copyComplete = (0, duration_1.default)();
78
+ fse.copySync(srcDir, buildDir, {
79
+ filter: src => !~src.indexOf("node_modules") && !src.endsWith('.test.js')
80
+ });
81
+ console.log('Copy Duration (ms)', copyComplete());
82
+ }
83
+ if (packageChanged) {
84
+ const installComplete = (0, duration_1.default)();
85
+ (0, child_process_1.execSync)("npm i --omit=dev --omit=optional --omit=peer --quite", {
86
+ cwd: dir ? path.resolve(buildDir, dir) : buildDir
87
+ });
88
+ console.log('NPM Install Duration (ms)', installComplete());
89
+ }
90
+ fs.writeFileSync(hashFilePath, hash);
91
+ }
92
+ return { buildDir, packageChanged, sourceChanged, moduleChanged };
93
+ }
94
+ exports.buildNodeSource = buildNodeSource;
95
+ function findPackageHash(hash) {
96
+ return hash?.children?.find(file => file.name === 'package.json')?.hash;
97
+ }
98
+ function sourceWasUpdated(prevHash, nextHash) {
99
+ const fileCountChanged = prevHash.children?.length != nextHash.children?.length;
100
+ const updatedItem = prevHash.children?.find(prevFile => {
101
+ const nextFile = nextHash.children?.find(f => f.name = prevFile.name);
102
+ return prevFile.name !== 'package.json' && (!nextFile || nextFile.hash !== prevFile.hash);
103
+ });
104
+ return fileCountChanged || !!updatedItem;
105
+ }
@@ -0,0 +1,8 @@
1
+ declare const duration: (() => {
2
+ (): number;
3
+ time: [number, number];
4
+ }) | (() => {
5
+ (): number;
6
+ time: number;
7
+ });
8
+ export default duration;
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const hrDuration = () => {
4
+ const startTime = process.hrtime();
5
+ const onEnd = function duration() {
6
+ const hrTime = process.hrtime(startTime);
7
+ return hrTime[0] * 1000 + hrTime[1] / 1000000;
8
+ };
9
+ onEnd.time = startTime;
10
+ return onEnd;
11
+ };
12
+ const msDuration = () => {
13
+ const startTime = Date.now();
14
+ const onEnd = function duration() {
15
+ return Date.now() - startTime;
16
+ };
17
+ onEnd.time = startTime;
18
+ return onEnd;
19
+ };
20
+ // @ts-ignore
21
+ const duration = process.hrtime ? hrDuration : msDuration;
22
+ exports.default = duration;
@@ -0,0 +1,2 @@
1
+ import { IIgnoreOptions } from "./walk-directory";
2
+ export default function emptyDirSync(parentFolder: string, options: IIgnoreOptions): void;
@@ -0,0 +1,47 @@
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ const fse = __importStar(require("fs-extra"));
30
+ const duration_1 = __importDefault(require("./duration"));
31
+ const walk_directory_1 = __importDefault(require("./walk-directory"));
32
+ function emptyDirSync(parentFolder, options) {
33
+ const rmComplete = (0, duration_1.default)();
34
+ const files = (0, walk_directory_1.default)(parentFolder, options);
35
+ files
36
+ .forEach((file) => {
37
+ try {
38
+ fse.removeSync(file.fullpath());
39
+ console.log('Deleted file:', file.fullpath());
40
+ }
41
+ catch (err) {
42
+ console.error('Error deleting file:', err);
43
+ }
44
+ });
45
+ console.log('Empty Directory Complete (ms)', parentFolder, rmComplete());
46
+ }
47
+ exports.default = emptyDirSync;
@@ -0,0 +1,15 @@
1
+ import { IIgnoreOptions } from './walk-directory';
2
+ interface HashOptions {
3
+ ignore: IIgnoreOptions;
4
+ }
5
+ export interface HashResult {
6
+ hash?: string;
7
+ children?: HashItem[];
8
+ }
9
+ export interface HashItem {
10
+ hash: string;
11
+ path: string;
12
+ name: string;
13
+ }
14
+ export declare function generateHash(folder: string, opt: HashOptions): string;
15
+ export {};
@@ -0,0 +1,59 @@
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.generateHash = void 0;
30
+ const crypto = __importStar(require("crypto"));
31
+ const path = __importStar(require("path"));
32
+ const fs = __importStar(require("fs"));
33
+ const walk_directory_1 = __importDefault(require("./walk-directory"));
34
+ const ALGO = 'sha1';
35
+ const ENCODING = 'base64';
36
+ function generateHash(folder, opt) {
37
+ const files = (0, walk_directory_1.default)(folder, opt.ignore);
38
+ const hash = crypto.createHash(ALGO);
39
+ const children = files
40
+ .sort((a, b) => a.fullpath().localeCompare(b.fullpath()))
41
+ .map((child) => {
42
+ const relPath = path.relative(process.cwd(), child.fullpath());
43
+ const fileHash = crypto.createHash(ALGO);
44
+ fileHash.update(relPath);
45
+ fileHash.update(fs.readFileSync(child.fullpath()));
46
+ const file = {
47
+ name: child.name,
48
+ path: relPath,
49
+ hash: fileHash.digest(ENCODING)
50
+ };
51
+ hash.update(file.hash);
52
+ return file;
53
+ });
54
+ return JSON.stringify({
55
+ hash: hash.digest(ENCODING),
56
+ children
57
+ }, null, '\t');
58
+ }
59
+ exports.generateHash = generateHash;
@@ -0,0 +1,4 @@
1
+ export declare const sourceRoot: string;
2
+ export declare const distributionRoot: string;
3
+ export declare function getSourceDirectory(type: string, id: string): string;
4
+ export declare function getBuildDirectory(type: string, id: string): string;
@@ -0,0 +1,37 @@
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.getBuildDirectory = exports.getSourceDirectory = exports.distributionRoot = exports.sourceRoot = void 0;
27
+ const path = __importStar(require("path"));
28
+ exports.sourceRoot = path.resolve(process.cwd(), "../src");
29
+ exports.distributionRoot = path.resolve(process.cwd(), "../dist");
30
+ function getSourceDirectory(type, id) {
31
+ return path.join(exports.sourceRoot, type, id);
32
+ }
33
+ exports.getSourceDirectory = getSourceDirectory;
34
+ function getBuildDirectory(type, id) {
35
+ return path.join(exports.distributionRoot, type, id);
36
+ }
37
+ exports.getBuildDirectory = getBuildDirectory;
@@ -0,0 +1,14 @@
1
+ /// <reference types="node" />
2
+ import * as fs from 'fs';
3
+ export interface IIgnoreOptions {
4
+ excluded?: (stat: fs.Dirent) => boolean;
5
+ included?: (stat: fs.Dirent) => boolean;
6
+ childrenExcluded?: (stat: fs.Dirent) => boolean;
7
+ childrenIncluded?: (stat: fs.Dirent) => boolean;
8
+ }
9
+ export declare const yes: () => boolean;
10
+ export declare const no: () => boolean;
11
+ export default function walkDirectory(directory: string, options: IIgnoreOptions, filepaths?: DirentExtended[]): DirentExtended[];
12
+ export interface DirentExtended extends fs.Dirent {
13
+ fullpath: () => string;
14
+ }
@@ -0,0 +1,48 @@
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.no = exports.yes = void 0;
27
+ const path = __importStar(require("path"));
28
+ const fs = __importStar(require("fs"));
29
+ const yes = () => true;
30
+ exports.yes = yes;
31
+ const no = () => false;
32
+ exports.no = no;
33
+ function walkDirectory(directory, options, filepaths = []) {
34
+ const { excluded = exports.no, included = exports.yes, childrenExcluded = exports.no, childrenIncluded = exports.yes } = options;
35
+ const files = fs.readdirSync(directory, { withFileTypes: true });
36
+ for (let stats of files) {
37
+ const filepath = path.join(directory, stats.name);
38
+ stats.fullpath = () => filepath;
39
+ if (stats.isDirectory() && !childrenExcluded(stats) && childrenIncluded(stats)) {
40
+ walkDirectory(filepath, options, filepaths);
41
+ }
42
+ else if (stats.isFile() && !excluded(stats) && included(stats)) {
43
+ filepaths.push(stats);
44
+ }
45
+ }
46
+ return filepaths;
47
+ }
48
+ exports.default = walkDirectory;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@byaga/cdk-patterns",
3
- "version": "0.6.1",
3
+ "version": "0.8.0",
4
4
  "description": "Collection of common patterns used when making AWS CloudFormation templates using CDK",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -15,9 +15,11 @@
15
15
  "author": "VeryFineHat",
16
16
  "license": "MIT",
17
17
  "dependencies": {
18
+ "@types/fs-extra": "^11.0.2",
18
19
  "aws-cdk": "^2.92.0",
19
20
  "aws-cdk-lib": "^2.92.0",
20
- "constructs": "^10.2.69"
21
+ "constructs": "^10.2.69",
22
+ "fs-extra": "^11.1.1"
21
23
  },
22
24
  "devDependencies": {
23
25
  "@types/node": "^20.5.0",
@@ -1,31 +1,31 @@
1
1
  import {Certificate, CertificateValidation} from "aws-cdk-lib/aws-certificatemanager";
2
2
  import {HostedZone, IHostedZone} from "aws-cdk-lib/aws-route53";
3
- import {CfnOutput} from "aws-cdk-lib";
4
- import IDeployStack from "./IDeployStack";
3
+ import {Output} from "./Output";
4
+ import DeployStack from "./DeployStack";
5
5
  import IDomainConfig from "./IDomainConfig";
6
6
 
7
7
  export class ApiCertificate extends Certificate {
8
8
  hostedZone: IHostedZone
9
9
  domain: string
10
10
 
11
- constructor(stack: IDeployStack, id: string, domain: IDomainConfig) {
11
+ constructor(stack: DeployStack, id: string, domain: IDomainConfig) {
12
12
  const certDomain = [domain.domainName]
13
13
  if (domain.subdomain) certDomain.splice(0, 0, domain.subdomain)
14
14
  const domainName = certDomain.join('.')
15
15
 
16
+ console.log('Getting Hosted Zone', domain.hostedZone.name)
16
17
  const hostedZone: IHostedZone = HostedZone.fromHostedZoneAttributes(stack, stack.genId(id, 'hosted-zone'), {
17
18
  hostedZoneId: domain.hostedZone.id,
18
19
  zoneName: domain.hostedZone.name
19
20
  })
21
+ console.log('Defining Certificate For', domainName)
20
22
  super(stack, stack.genId(id), {
21
23
  domainName,
22
24
  validation: CertificateValidation.fromDns(hostedZone)
23
25
  });
24
26
 
25
27
  this.domain = domainName
26
- new CfnOutput(this, stack.genId(id, "certificate-output"), {
27
- value: this.certificateArn ,
28
- exportName: stack.genName(id)})
28
+ new Output(stack, id, this.certificateArn)
29
29
  this.hostedZone = hostedZone
30
30
  }
31
31
  }