@byaga/cdk-patterns 0.9.3 → 0.10.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 (46) hide show
  1. package/.fleet/run.json +11 -0
  2. package/LICENSE.md +21 -0
  3. package/README.md +37 -0
  4. package/lib/DeployStack.d.ts +3 -7
  5. package/lib/DeployStack.js +7 -8
  6. package/lib/NodeJsLambda.d.ts +22 -0
  7. package/lib/NodeJsLambda.js +18 -2
  8. package/lib/NodeJsLambdaLayer.d.ts +13 -0
  9. package/lib/NodeJsLambdaLayer.js +13 -2
  10. package/lib/StaticWebSite.d.ts +20 -4
  11. package/lib/StaticWebSite.js +29 -15
  12. package/lib/methods/BuildOptions.d.ts +3 -0
  13. package/lib/methods/BuildOptions.js +2 -0
  14. package/lib/methods/build-ecmascript.d.ts +9 -0
  15. package/lib/methods/build-ecmascript.js +20 -0
  16. package/lib/methods/build-node-source.d.ts +10 -13
  17. package/lib/methods/build-node-source.js +30 -96
  18. package/lib/methods/build-typescript.d.ts +8 -0
  19. package/lib/methods/build-typescript.js +33 -0
  20. package/lib/methods/copy-files.d.ts +8 -0
  21. package/lib/methods/{empty-directory.js → copy-files.js} +21 -14
  22. package/lib/methods/duration.d.ts +8 -6
  23. package/lib/methods/duration.js +17 -11
  24. package/lib/methods/generate-hash.d.ts +7 -15
  25. package/lib/methods/generate-hash.js +25 -53
  26. package/lib/methods/get-files.d.ts +9 -0
  27. package/lib/methods/get-files.js +38 -0
  28. package/lib/methods/get-source-directory.d.ts +16 -2
  29. package/lib/methods/get-source-directory.js +31 -30
  30. package/lib/methods/hash-file.d.ts +18 -0
  31. package/lib/methods/hash-file.js +45 -0
  32. package/lib/methods/install-node-modules.d.ts +12 -0
  33. package/lib/methods/install-node-modules.js +30 -0
  34. package/package.json +25 -11
  35. package/jest.config.js +0 -16
  36. package/lib/IDeployStack.d.ts +0 -20
  37. package/lib/IDeployStack.js +0 -48
  38. package/lib/methods/apply-honeycomb-to-lambda.spec.d.ts +0 -1
  39. package/lib/methods/apply-honeycomb-to-lambda.spec.js +0 -61
  40. package/lib/methods/empty-directory.d.ts +0 -2
  41. package/lib/methods/walk-directory.d.ts +0 -14
  42. package/lib/methods/walk-directory.js +0 -48
  43. package/test-results/html/index.html +0 -1
  44. package/test-results/html/jest-html-reporters-attach/index/index.js +0 -3
  45. package/test-results/html/jest-html-reporters-attach/index/result.js +0 -1
  46. package/test-results/junit/junit.xml +0 -13
@@ -0,0 +1,11 @@
1
+ {
2
+ "configurations": [
3
+ {
4
+ "type": "npm",
5
+ "name": "cdk-patterns build",
6
+ "command": "run",
7
+ "scripts": "build",
8
+ },
9
+
10
+ ]
11
+ }
package/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 VeryFineHat
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # @byaga/cdk-patterns
2
+
3
+ This package provides a set of reusable patterns for the AWS Cloud Development Kit (CDK).
4
+
5
+ NOTE: This package is highly opinionated and is intended to be used by the author. Use at your own risk/joy.
6
+
7
+ ## Installation
8
+
9
+ To install the package, use the following command:
10
+
11
+ ```bash
12
+ npm install @byaga/cdk-patterns
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ Import the package in your TypeScript or JavaScript file:
18
+
19
+ ```typescript
20
+ import { Pattern } from '@byaga/cdk-patterns';
21
+ ```
22
+
23
+ Then, you can use the patterns provided by the package in your CDK stacks.
24
+
25
+ ## Patterns
26
+
27
+ The package includes the following patterns:
28
+
29
+ - [NodeJsLambda](./src/NodeJsLambda.md)
30
+
31
+ ## Contributing
32
+
33
+ Contributions are welcome! Please read our contributing guidelines for details.
34
+
35
+ ## License
36
+
37
+ This project is licensed under the MIT License - see the LICENSE file for details.
@@ -2,11 +2,7 @@ import { Stack } from 'aws-cdk-lib';
2
2
  import IStackArguments from './IStackArguments';
3
3
  import { IConstruct } from "constructs";
4
4
  export declare class DeployStack extends Stack {
5
- registry: {
6
- [t: string]: {
7
- [n: string]: any;
8
- };
9
- };
5
+ registry: Record<string, Record<string, unknown>>;
10
6
  stage: string;
11
7
  name: string;
12
8
  project: string;
@@ -17,7 +13,7 @@ export declare class DeployStack extends Stack {
17
13
  static genStackResourceName(stackName: string, resource: string, stage?: string): string;
18
14
  static genStackResourceId(stackName: string, resource: string, stage?: string): string;
19
15
  constructor(scope: IConstruct, props: IStackArguments);
20
- get(type: string, name: string): any;
21
- set(type: string, name: string, instance: any): any;
16
+ get(type: string, name: string): {} | null;
17
+ set(type: string, name: string, instance: unknown): unknown;
22
18
  }
23
19
  export default DeployStack;
@@ -35,14 +35,13 @@ class DeployStack extends aws_cdk_lib_1.Stack {
35
35
  ...props,
36
36
  stackName: props.stackName + '-' + stage
37
37
  });
38
- const stack = this;
39
- stack.name = props.stackName || '';
40
- stack.stage = stage;
41
- stack.project = props.project;
42
- stack.tags.setTag('stage', stage);
43
- stack.tags.setTag('stack', this.genName('ui-stack'));
44
- stack.tags.setTag('project', props.project);
45
- stack.tags.setTag('owner', props.owner);
38
+ this.name = props.stackName || '';
39
+ this.stage = stage;
40
+ this.project = props.project;
41
+ this.tags.setTag('stage', stage);
42
+ this.tags.setTag('stack', this.genName('ui-stack'));
43
+ this.tags.setTag('project', props.project);
44
+ this.tags.setTag('owner', props.owner);
46
45
  }
47
46
  get(type, name) {
48
47
  const items = this.registry[type];
@@ -2,12 +2,34 @@ import { FunctionOptions } from "aws-cdk-lib/aws-lambda";
2
2
  import { DeployStack } from "./DeployStack";
3
3
  import { FunctionIntegration } from "./FunctionIntegration";
4
4
  import { Duration } from "aws-cdk-lib";
5
+ /**
6
+ * Interface for the properties of a Node.js function.
7
+ */
5
8
  interface NodeFunctionProps {
9
+ /**
10
+ * The properties of the function.
11
+ */
6
12
  funcProps?: FunctionOptions;
13
+ /**
14
+ * The timeout duration for the function.
15
+ */
7
16
  timeout?: Duration;
17
+ /**
18
+ * The memory size for the function.
19
+ */
8
20
  memory?: number;
9
21
  }
22
+ /**
23
+ * Class representing a Node.js Lambda function.
24
+ * @extends FunctionIntegration
25
+ */
10
26
  export declare class NodeJsLambda extends FunctionIntegration {
27
+ /**
28
+ * Creates a new Node.js Lambda function.
29
+ * @param {DeployStack} stack - The deployment stack.
30
+ * @param {string} id - The ID of the function.
31
+ * @param {NodeFunctionProps} options - The properties of the function.
32
+ */
11
33
  constructor(stack: DeployStack, id: string, options?: NodeFunctionProps);
12
34
  }
13
35
  export {};
@@ -8,19 +8,35 @@ const aws_lambda_1 = require("aws-cdk-lib/aws-lambda");
8
8
  const FunctionIntegration_1 = require("./FunctionIntegration");
9
9
  const build_node_source_1 = require("./methods/build-node-source");
10
10
  const duration_1 = __importDefault(require("./methods/duration"));
11
+ /**
12
+ * Class representing a Node.js Lambda function.
13
+ * @extends FunctionIntegration
14
+ */
11
15
  class NodeJsLambda extends FunctionIntegration_1.FunctionIntegration {
16
+ /**
17
+ * Creates a new Node.js Lambda function.
18
+ * @param {DeployStack} stack - The deployment stack.
19
+ * @param {string} id - The ID of the function.
20
+ * @param {NodeFunctionProps} options - The properties of the function.
21
+ */
12
22
  constructor(stack, id, options) {
13
23
  console.log('Defining Node Lambda', id);
24
+ // Measure the duration of the build process
14
25
  const done = (0, duration_1.default)();
15
- const { buildDir } = (0, build_node_source_1.buildNodeSource)('lambda', id);
26
+ // Build the Node.js source code
27
+ const buildDir = (0, build_node_source_1.buildNodeSource)('lambda', id);
16
28
  console.log('Total Build Duration (ms)', done());
29
+ // Call the parent constructor with the function properties
17
30
  super(stack, id, {
18
31
  ...options,
19
32
  funcProps: {
20
33
  ...options?.funcProps,
34
+ // The source code of the function
21
35
  code: aws_lambda_1.Code.fromAsset(buildDir),
36
+ // The handler of the function
22
37
  handler: `${id}.handler`,
23
- runtime: aws_lambda_1.Runtime.NODEJS_18_X
38
+ // The runtime of the function
39
+ runtime: aws_lambda_1.Runtime.NODEJS_20_X
24
40
  }
25
41
  });
26
42
  }
@@ -2,11 +2,24 @@ import { LayerVersion, Runtime } from "aws-cdk-lib/aws-lambda";
2
2
  import { DeployStack } from "./DeployStack";
3
3
  import { LayerVersionOptions } from "aws-cdk-lib/aws-lambda/lib/layers";
4
4
  import { Architecture } from "aws-cdk-lib/aws-lambda/lib/architecture";
5
+ /**
6
+ * Interface for the properties of the NodeJsLambdaLayer class.
7
+ */
5
8
  interface NodeJsLambdaLayerProps extends LayerVersionOptions {
6
9
  readonly compatibleRuntimes?: Runtime[];
7
10
  readonly compatibleArchitectures?: Architecture[];
8
11
  }
12
+ /**
13
+ * Class representing a Node.js AWS Lambda layer.
14
+ * Extends the LayerVersion class from the AWS CDK library.
15
+ */
9
16
  export declare class NodeJsLambdaLayer extends LayerVersion {
17
+ /**
18
+ * Constructs a new NodeJsLambdaLayer instance.
19
+ * @param {DeployStack} stack - The deployment stack.
20
+ * @param {string} id - The ID of the layer.
21
+ * @param {NodeJsLambdaLayerProps} props - The properties of the layer.
22
+ */
10
23
  constructor(stack: DeployStack, id: string, props?: NodeJsLambdaLayerProps);
11
24
  }
12
25
  export {};
@@ -7,16 +7,27 @@ exports.NodeJsLambdaLayer = void 0;
7
7
  const aws_lambda_1 = require("aws-cdk-lib/aws-lambda");
8
8
  const build_node_source_1 = require("./methods/build-node-source");
9
9
  const duration_1 = __importDefault(require("./methods/duration"));
10
+ /**
11
+ * Class representing a Node.js AWS Lambda layer.
12
+ * Extends the LayerVersion class from the AWS CDK library.
13
+ */
10
14
  class NodeJsLambdaLayer extends aws_lambda_1.LayerVersion {
15
+ /**
16
+ * Constructs a new NodeJsLambdaLayer instance.
17
+ * @param {DeployStack} stack - The deployment stack.
18
+ * @param {string} id - The ID of the layer.
19
+ * @param {NodeJsLambdaLayerProps} props - The properties of the layer.
20
+ */
11
21
  constructor(stack, id, props = {}) {
12
22
  console.log("Building Lambda Layer", id);
13
23
  const done = (0, duration_1.default)();
14
- const { buildDir } = (0, build_node_source_1.buildNodeSource)('lambda-layer', id, { dir: 'nodejs' });
24
+ const buildDir = (0, build_node_source_1.buildNodeSource)('lambda-layer', id, { subdirectory: 'nodejs' });
15
25
  console.log('Build Duration (ms)', done());
16
26
  super(stack, stack.genId(id), {
27
+ compatibleRuntimes: [aws_lambda_1.Runtime.NODEJS_20_X],
17
28
  ...props,
18
29
  layerVersionName: stack.genName(id),
19
- code: aws_lambda_1.Code.fromAsset(buildDir)
30
+ code: aws_lambda_1.Code.fromAsset(buildDir),
20
31
  });
21
32
  stack.set('lambda-layer', id, this);
22
33
  }
@@ -2,16 +2,32 @@
2
2
  import { DeployStack } from "./DeployStack";
3
3
  import { SourceConfiguration } from "aws-cdk-lib/aws-cloudfront";
4
4
  import IDomainConfig from "./IDomainConfig";
5
- import { IIgnoreOptions } from "./methods/walk-directory";
6
- interface StaticWebSiteConfig {
5
+ /**
6
+ * Configuration interface for the StaticWebSite class.
7
+ */
8
+ export interface StaticWebSiteConfig {
7
9
  srcDir?: string;
8
10
  domain: IDomainConfig;
9
11
  env: NodeJS.ProcessEnv;
10
- ignore?: IIgnoreOptions;
11
12
  proxy?: SourceConfiguration[];
12
13
  }
14
+ /**
15
+ * Logic which will setup a static web site in AWS with an S3 Bucket, CloudFront Distribution, and Route53 A-Name Record.
16
+ */
13
17
  export declare class StaticWebSite {
18
+ /**
19
+ * Constructs a new StaticWebSite instance.
20
+ * @param {DeployStack} stack - The deployment stack.
21
+ * @param {string} id - The ID of the website.
22
+ * @param {StaticWebSiteConfig} props - The configuration properties of the website.
23
+ */
14
24
  constructor(stack: DeployStack, id: string, props: StaticWebSiteConfig);
25
+ /**
26
+ * Defines a proxy for the static website.
27
+ * @param {DeployStack} stack - The deployment stack.
28
+ * @param {string} domainName - The domain name.
29
+ * @param {string} pathPattern - The path pattern.
30
+ * @returns {SourceConfiguration} The source configuration for the proxy.
31
+ */
15
32
  static defineProxy(stack: DeployStack, domainName: string, pathPattern: string): SourceConfiguration;
16
33
  }
17
- export {};
@@ -16,26 +16,26 @@ const aws_route53_1 = require("aws-cdk-lib/aws-route53");
16
16
  const aws_route53_targets_1 = require("aws-cdk-lib/aws-route53-targets");
17
17
  const duration_1 = __importDefault(require("./methods/duration"));
18
18
  const build_node_source_1 = require("./methods/build-node-source");
19
- const walk_directory_1 = require("./methods/walk-directory");
20
19
  const aws_cdk_lib_1 = require("aws-cdk-lib");
20
+ /**
21
+ * Logic which will setup a static web site in AWS with an S3 Bucket, CloudFront Distribution, and Route53 A-Name Record.
22
+ */
21
23
  class StaticWebSite {
24
+ /**
25
+ * Constructs a new StaticWebSite instance.
26
+ * @param {DeployStack} stack - The deployment stack.
27
+ * @param {string} id - The ID of the website.
28
+ * @param {StaticWebSiteConfig} props - The configuration properties of the website.
29
+ */
22
30
  constructor(stack, id, props) {
23
31
  console.log('Deploying Static Web Site', id);
24
32
  const done = (0, duration_1.default)();
25
- const childrenExcluded = props.ignore?.childrenExcluded || walk_directory_1.no;
26
- const { buildDir, moduleChanged } = (0, build_node_source_1.buildNodeSource)('web', id, {
27
- ignore: {
28
- ...props.ignore,
29
- childrenExcluded: stat => stat.name === 'out' || childrenExcluded(stat)
30
- }
33
+ const buildDir = (0, build_node_source_1.buildNodeSource)('web', id);
34
+ console.log('Building UI Source', id);
35
+ (0, child_process_1.execSync)('npm run export', {
36
+ cwd: buildDir,
37
+ env: props.env
31
38
  });
32
- if (moduleChanged) {
33
- console.log('Building UI Source', id);
34
- (0, child_process_1.execSync)('npm run export', {
35
- cwd: buildDir,
36
- env: props.env
37
- });
38
- }
39
39
  console.log('Total Build Duration (ms)', done());
40
40
  const exportDir = buildDir + '/out';
41
41
  console.log('Creating HTTPS Certificate', id + '-certificate');
@@ -46,7 +46,14 @@ class StaticWebSite {
46
46
  websiteIndexDocument: "index.html",
47
47
  websiteErrorDocument: "error.html",
48
48
  removalPolicy: core_1.RemovalPolicy.DESTROY,
49
- autoDeleteObjects: true
49
+ autoDeleteObjects: true,
50
+ publicReadAccess: false,
51
+ blockPublicAccess: {
52
+ blockPublicAcls: true,
53
+ blockPublicPolicy: true,
54
+ ignorePublicAcls: true,
55
+ restrictPublicBuckets: true
56
+ }
50
57
  });
51
58
  new Output_1.Output(stack, `${id}-content-bucket`, s3BucketSource.bucketName);
52
59
  const cloudFrontPolicy = new aws_iam_1.PolicyStatement({
@@ -86,6 +93,13 @@ class StaticWebSite {
86
93
  target: aws_route53_1.RecordTarget.fromAlias(new aws_route53_targets_1.CloudFrontTarget(distro))
87
94
  });
88
95
  }
96
+ /**
97
+ * Defines a proxy for the static website.
98
+ * @param {DeployStack} stack - The deployment stack.
99
+ * @param {string} domainName - The domain name.
100
+ * @param {string} pathPattern - The path pattern.
101
+ * @returns {SourceConfiguration} The source configuration for the proxy.
102
+ */
89
103
  static defineProxy(stack, domainName, pathPattern) {
90
104
  return {
91
105
  customOriginSource: {
@@ -0,0 +1,3 @@
1
+ export interface BuildOptions {
2
+ subdirectory?: string;
3
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Builds ECMAScript files.
3
+ * Copies the files from the source directory to the build directory and installs the necessary node modules.
4
+ * @function buildEcmascript
5
+ * @param {string[]} files - The files to build.
6
+ * @param {string} srcDir - The source directory.
7
+ * @param {string} buildDir - The build directory.
8
+ */
9
+ export declare function buildEcmascript(files: string[], srcDir: string, buildDir: string): void;
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.buildEcmascript = void 0;
4
+ const copy_files_1 = require("./copy-files");
5
+ const install_node_modules_1 = require("./install-node-modules");
6
+ /**
7
+ * Builds ECMAScript files.
8
+ * Copies the files from the source directory to the build directory and installs the necessary node modules.
9
+ * @function buildEcmascript
10
+ * @param {string[]} files - The files to build.
11
+ * @param {string} srcDir - The source directory.
12
+ * @param {string} buildDir - The build directory.
13
+ */
14
+ function buildEcmascript(files, srcDir, buildDir) {
15
+ // Copies the files from the source directory to the build directory
16
+ (0, copy_files_1.copyFiles)(files, srcDir, buildDir);
17
+ // Installs the necessary node modules
18
+ (0, install_node_modules_1.installNodeModules)(buildDir, ['dev', 'optional', 'peer']);
19
+ }
20
+ exports.buildEcmascript = buildEcmascript;
@@ -1,13 +1,10 @@
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 {};
1
+ import { BuildOptions } from "./BuildOptions";
2
+ /**
3
+ * Builds the source code for a Node.js project.
4
+ * @function buildNodeSource
5
+ * @param {string} type - The type of the source code.
6
+ * @param {string} id - The ID of the source code.
7
+ * @param {BuildOptions} [options] - Options for building the source code.
8
+ * @returns - The build directory containing the resulting source code
9
+ */
10
+ export declare function buildNodeSource(type: string, id: string, options?: BuildOptions): string;
@@ -1,105 +1,39 @@
1
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
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
26
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
4
  };
28
5
  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");
6
+ exports.buildNodeSource = void 0;
7
+ const fs_extra_1 = require("fs-extra");
35
8
  const get_source_directory_1 = require("./get-source-directory");
9
+ const get_files_1 = require("./get-files");
10
+ const generate_hash_1 = require("./generate-hash");
36
11
  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 };
12
+ const build_ecmascript_1 = require("./build-ecmascript");
13
+ const build_typescript_1 = require("./build-typescript");
14
+ const hash_file_1 = require("./hash-file");
15
+ /**
16
+ * Builds the source code for a Node.js project.
17
+ * @function buildNodeSource
18
+ * @param {string} type - The type of the source code.
19
+ * @param {string} id - The ID of the source code.
20
+ * @param {BuildOptions} [options] - Options for building the source code.
21
+ * @returns - The build directory containing the resulting source code
22
+ */
23
+ function buildNodeSource(type, id, options) {
24
+ const srcDir = (0, get_source_directory_1.getSourceDirectory)(type, id, options?.subdirectory);
25
+ const buildDir = (0, get_source_directory_1.getBuildDirectory)(type, id, options?.subdirectory);
26
+ const files = (0, get_files_1.getFiles)(srcDir);
27
+ const hash = (0, generate_hash_1.generateHash)(files);
28
+ if ((0, hash_file_1.getStoredHash)(type, id) === hash)
29
+ return buildDir;
30
+ const rmComplete = (0, duration_1.default)();
31
+ if ((0, fs_extra_1.existsSync)(buildDir))
32
+ (0, fs_extra_1.emptyDirSync)(buildDir);
33
+ console.log('Cleanup Duration (ms)', rmComplete());
34
+ const isTypeScript = !!files.find(file => file.endsWith('tsconfig.json'));
35
+ isTypeScript ? (0, build_typescript_1.buildTypescript)(srcDir, buildDir) : (0, build_ecmascript_1.buildEcmascript)(files, srcDir, buildDir);
36
+ (0, hash_file_1.storeHash)(type, id, hash);
37
+ return buildDir;
93
38
  }
94
39
  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
+ /**
2
+ * Builds TypeScript source code.
3
+ * Ensures the build directory exists, installs necessary node modules, and compiles the TypeScript code.
4
+ * @function buildTypescript
5
+ * @param {string} srcDir - The source directory containing the TypeScript code.
6
+ * @param {string} buildDir - The output directory for the compiled JavaScript code.
7
+ */
8
+ export declare function buildTypescript(srcDir: string, buildDir: string): void;
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.buildTypescript = void 0;
7
+ const child_process_1 = require("child_process");
8
+ const duration_1 = __importDefault(require("./duration"));
9
+ const fs_extra_1 = require("fs-extra");
10
+ const install_node_modules_1 = require("./install-node-modules");
11
+ /**
12
+ * Builds TypeScript source code.
13
+ * Ensures the build directory exists, installs necessary node modules, and compiles the TypeScript code.
14
+ * @function buildTypescript
15
+ * @param {string} srcDir - The source directory containing the TypeScript code.
16
+ * @param {string} buildDir - The output directory for the compiled JavaScript code.
17
+ */
18
+ function buildTypescript(srcDir, buildDir) {
19
+ // Ensure the build directory exists
20
+ (0, fs_extra_1.ensureDirSync)(buildDir);
21
+ // Install necessary node modules
22
+ (0, install_node_modules_1.installNodeModules)(srcDir);
23
+ // Measure the duration of the build process
24
+ const done = (0, duration_1.default)();
25
+ // Compile the TypeScript code
26
+ (0, child_process_1.execSync)(`npm run build -- --outDir ${buildDir}`, {
27
+ cwd: srcDir,
28
+ stdio: 'inherit'
29
+ });
30
+ // Log the duration of the build process
31
+ console.log('NPM Install Duration (ms)', done());
32
+ }
33
+ exports.buildTypescript = buildTypescript;
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Copies files from the source directory to the build directory.
3
+ * @function copyFiles
4
+ * @param {string[]} files - The files to copy.
5
+ * @param {string} srcDir - The source directory.
6
+ * @param {string} buildDir - The build directory.
7
+ */
8
+ export declare function copyFiles(files: string[], srcDir: string, buildDir: string): void;