@trayio/cdk-build 0.9.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.
@@ -0,0 +1,10 @@
1
+ import * as TE from 'fp-ts/TaskEither';
2
+ export declare class ConnectorBuilder {
3
+ private operationSchemaGenerator;
4
+ buildConnector(cdkProjectPath: string): TE.TaskEither<Error, undefined>;
5
+ private generateSchemas;
6
+ private generateConnectorsJson;
7
+ private generatePackage;
8
+ private getOperations;
9
+ }
10
+ //# sourceMappingURL=ConnectorBuilder.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ConnectorBuilder.d.ts","sourceRoot":"","sources":["../../src/connector/ConnectorBuilder.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,kBAAkB,CAAC;AASvC,qBAAa,gBAAgB;IAC5B,OAAO,CAAC,wBAAwB,CAA2C;IAE3E,cAAc,CAAC,cAAc,EAAE,MAAM,GAAG,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,SAAS,CAAC;IAQvE,OAAO,CAAC,eAAe;IAiBvB,OAAO,CAAC,sBAAsB;IA8D9B,OAAO,CAAC,eAAe;IA0CvB,OAAO,CAAC,aAAa;CAUrB"}
@@ -0,0 +1,103 @@
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.ConnectorBuilder = void 0;
30
+ const TE = __importStar(require("fp-ts/TaskEither"));
31
+ const O = __importStar(require("fp-ts/Option"));
32
+ const function_1 = require("fp-ts/function");
33
+ const Array_1 = require("fp-ts/Array");
34
+ const pathLib = __importStar(require("path"));
35
+ const fse = __importStar(require("fs-extra"));
36
+ const adm_zip_1 = __importDefault(require("adm-zip"));
37
+ const ConnectorOperationSchemaGenerator_1 = require("./operation/ConnectorOperationSchemaGenerator");
38
+ class ConnectorBuilder {
39
+ constructor() {
40
+ this.operationSchemaGenerator = new ConnectorOperationSchemaGenerator_1.ConnectorOperationSchemaGenerator();
41
+ }
42
+ buildConnector(cdkProjectPath) {
43
+ return (0, function_1.pipe)(this.generateSchemas(cdkProjectPath), TE.chain(() => this.generateConnectorsJson(cdkProjectPath)), TE.chain(() => this.generatePackage(cdkProjectPath)));
44
+ }
45
+ generateSchemas(cdkProjectPath) {
46
+ const operations = this.getOperations(cdkProjectPath);
47
+ return (0, function_1.pipe)((0, Array_1.traverse)(TE.ApplicativeSeq)((operation) => {
48
+ const operationPath = pathLib.join(cdkProjectPath, 'src', operation);
49
+ return this.operationSchemaGenerator.generateSchemasForOperation(operationPath, operation);
50
+ })(operations), TE.map(() => undefined));
51
+ }
52
+ // TODO Move this to another module and add better error handling
53
+ generateConnectorsJson(cdkProjectPath) {
54
+ const operations = this.getOperations(cdkProjectPath);
55
+ const operationEntries = operations.map((operation) => {
56
+ console.log(`Generating schemas for operation ${operation}`); // TODO: Remove this and use a logger
57
+ const operationPath = pathLib.join(cdkProjectPath, 'src', operation);
58
+ const operationJsonPath = pathLib.join(operationPath, 'operation.json');
59
+ const inputJsonPath = pathLib.join(operationPath, 'input.json');
60
+ const outputJsonPath = pathLib.join(operationPath, 'output.json');
61
+ const responseJsonPath = pathLib.join(operationPath, 'response.json');
62
+ // TODO: use DynamicType and safecasts
63
+ const operationJson = fse.readJsonSync(operationJsonPath);
64
+ const inputJson = fse.readJsonSync(inputJsonPath);
65
+ const outputJson = fse.readJsonSync(outputJsonPath);
66
+ const responseJsonOpt = (0, function_1.pipe)(O.fromPredicate((path) => fse.existsSync(path))(responseJsonPath), O.map((path) => fse.readJSONSync(path)));
67
+ const completeOperationJson = Object.assign(Object.assign({}, operationJson), { name: operation, input_schema: inputJson, output_schema: outputJson });
68
+ return (0, function_1.pipe)(responseJsonOpt, O.map((responseJson) => (Object.assign(Object.assign({}, completeOperationJson), { reply_schema: responseJson }))), O.getOrElse(() => completeOperationJson));
69
+ });
70
+ const connectorJsonPath = pathLib.join(cdkProjectPath, 'connector.json');
71
+ const connectorJson = fse.readJsonSync(connectorJsonPath);
72
+ const legacyConnectorsJson = [
73
+ Object.assign(Object.assign({}, connectorJson), { messages: operationEntries }),
74
+ ];
75
+ const legacyConnectorsJsonPath = pathLib.join(cdkProjectPath, 'connectors.json');
76
+ fse.writeJSONSync(legacyConnectorsJsonPath, legacyConnectorsJson);
77
+ return TE.right(undefined);
78
+ }
79
+ // TODO: better error handling
80
+ generatePackage(cdkProjectPath) {
81
+ const connectorPackagePath = pathLib.join(cdkProjectPath, 'dist', 'connector.zip');
82
+ const connectorJsonPath = pathLib.join(cdkProjectPath, 'connector.json');
83
+ const connectorJson = fse.readJsonSync(connectorJsonPath);
84
+ const connectorPackage = new adm_zip_1.default();
85
+ connectorPackage.addLocalFolder(pathLib.join(cdkProjectPath, 'src'), pathLib.join(connectorJson.name, 'src'));
86
+ connectorPackage.addLocalFile(pathLib.join(cdkProjectPath, 'package.json'), connectorJson.name);
87
+ connectorPackage.addLocalFile(pathLib.join(cdkProjectPath, 'tsconfig.json'), connectorJson.name);
88
+ connectorPackage.addLocalFile(pathLib.join(cdkProjectPath, 'jest.config.js'), connectorJson.name);
89
+ connectorPackage.addLocalFile(pathLib.join(cdkProjectPath, 'connector.json'), connectorJson.name);
90
+ connectorPackage.addLocalFile(pathLib.join(cdkProjectPath, 'connectors.json'), connectorJson.name);
91
+ connectorPackage.writeZip(connectorPackagePath);
92
+ return TE.right(undefined);
93
+ }
94
+ getOperations(cdkProjectPath) {
95
+ const srcPath = pathLib.join(cdkProjectPath, 'src');
96
+ const srcEntries = fse.readdirSync(srcPath, { withFileTypes: true });
97
+ return srcEntries
98
+ .filter((srcEntry) => srcEntry.isDirectory())
99
+ .filter((srcEntry) => fse.existsSync(pathLib.join(srcPath, srcEntry.name, 'operation.json')))
100
+ .map((srcEntry) => srcEntry.name);
101
+ }
102
+ }
103
+ exports.ConnectorBuilder = ConnectorBuilder;
@@ -0,0 +1,8 @@
1
+ import * as TE from 'fp-ts/TaskEither';
2
+ export declare class ConnectorOperationSchemaGenerator {
3
+ private schemaTransformer;
4
+ constructor();
5
+ generateSchemasForOperation(operationPath: string, operationName: string): TE.TaskEither<Error, undefined>;
6
+ private generateSchema;
7
+ }
8
+ //# sourceMappingURL=ConnectorOperationSchemaGenerator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ConnectorOperationSchemaGenerator.d.ts","sourceRoot":"","sources":["../../../src/connector/operation/ConnectorOperationSchemaGenerator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,kBAAkB,CAAC;AASvC,qBAAa,iCAAiC;IAC7C,OAAO,CAAC,iBAAiB,CAA4B;;IAMrD,2BAA2B,CAC1B,aAAa,EAAE,MAAM,EACrB,aAAa,EAAE,MAAM,GACnB,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,SAAS,CAAC;IAgDlC,OAAO,CAAC,cAAc;CActB"}
@@ -0,0 +1,66 @@
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.ConnectorOperationSchemaGenerator = void 0;
27
+ const TE = __importStar(require("fp-ts/TaskEither"));
28
+ const function_1 = require("fp-ts/function");
29
+ const pathLib = __importStar(require("path"));
30
+ const StringExtensions_1 = require("@trayio/tray-commons/string/StringExtensions");
31
+ const TJSTraySchemaTypeTransformer_1 = require("@trayio/tray-schema/schema/TJSTraySchemaTypeTransformer");
32
+ const fse = __importStar(require("fs-extra"));
33
+ const DynamicType_1 = require("@trayio/tray-commons/dynamictype/DynamicType");
34
+ class ConnectorOperationSchemaGenerator {
35
+ constructor() {
36
+ this.schemaTransformer = new TJSTraySchemaTypeTransformer_1.TJSTraySchemaTypeTransformer();
37
+ }
38
+ generateSchemasForOperation(operationPath, operationName) {
39
+ console.log(`Generating schemas for operation ${operationName}`); // TODO: Remove this and use a logger
40
+ const inputFilePath = pathLib.join(operationPath, 'input.ts');
41
+ const outputFilePath = pathLib.join(operationPath, 'output.ts');
42
+ const responseFilePath = pathLib.join(operationPath, 'response.ts');
43
+ const inputTypeName = `${StringExtensions_1.StringExtensions.pascalCase(operationName)}Input`;
44
+ const outputTypeName = `${StringExtensions_1.StringExtensions.pascalCase(operationName)}Output`;
45
+ const responseTypeName = `${StringExtensions_1.StringExtensions.pascalCase(operationName)}Response`;
46
+ const inputSchemaFilePath = pathLib.join(operationPath, 'input.json');
47
+ const outputSchemaFilePath = pathLib.join(operationPath, 'output.json');
48
+ const responseSchemaFilePath = pathLib.join(operationPath, 'response.json');
49
+ if (!fse.existsSync(inputFilePath)) {
50
+ return TE.left(new Error(`Input file not found in ${inputFilePath}`));
51
+ }
52
+ if (!fse.existsSync(outputFilePath)) {
53
+ return TE.left(new Error(`Output file not found in ${outputFilePath}`));
54
+ }
55
+ return (0, function_1.pipe)(this.generateSchema(inputFilePath, inputTypeName, inputSchemaFilePath), TE.chain(() => this.generateSchema(outputFilePath, outputTypeName, outputSchemaFilePath)), TE.chain(() => {
56
+ if (fse.existsSync(responseFilePath)) {
57
+ return this.generateSchema(responseFilePath, responseTypeName, responseSchemaFilePath);
58
+ }
59
+ return TE.right(undefined);
60
+ }));
61
+ }
62
+ generateSchema(filePath, typeName, schemaFilePath) {
63
+ return (0, function_1.pipe)(TE.fromEither(this.schemaTransformer.generateFromTypes(typeName, filePath)), TE.chain((schema) => TE.fromEither(DynamicType_1.DynamicType.writeToFile(schemaFilePath, schema))));
64
+ }
65
+ }
66
+ exports.ConnectorOperationSchemaGenerator = ConnectorOperationSchemaGenerator;
package/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "@trayio/cdk-build",
3
+ "version": "0.9.0",
4
+ "description": "Build utilities for CDK projects",
5
+ "exports": {
6
+ "./*": "./dist/*.js"
7
+ },
8
+ "author": "Tray.io",
9
+ "license": "MIT",
10
+ "engines": {
11
+ "node": ">=18.x"
12
+ },
13
+ "publishConfig": {
14
+ "access": "public"
15
+ },
16
+ "dependencies": {
17
+ "@trayio/tray-commons": "*",
18
+ "@trayio/tray-schema": "*",
19
+ "adm-zip": "*"
20
+ },
21
+ "typesVersions": {
22
+ "*": {
23
+ "*": [
24
+ "*",
25
+ "dist/*"
26
+ ]
27
+ }
28
+ },
29
+ "files": [
30
+ "/dist"
31
+ ]
32
+ }