Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code Native Integrations #173

Merged
merged 1 commit into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
github-cli 2.21.1
nodejs 18.12.1
nodejs 18.17.1
yarn 1.22.19
24 changes: 14 additions & 10 deletions packages/generator-spectral/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@prismatic-io/generator-spectral",
"version": "2.3.2",
"version": "3.0.0",
"description": "Yeoman generator for scaffolding out Prismatic components",
"keywords": [
"prismatic",
Expand All @@ -27,6 +27,8 @@
"test": "jest",
"yo": "yo",
"yo-component": "yarn run build && mkdir -p tmp && cd tmp && yo ../generators/component",
"yo-integration": "yarn run build && mkdir -p tmp && cd tmp && yo ../generators/integration",
"yo-flow": "yarn run build && mkdir -p tmp && cd tmp && yo ../generators/flow",
"yo-formats": "yarn run build && mkdir -p tmp && cd tmp && yo ../generators/formats",
"yo-action": "cd tmp/src && yo ../../generators/action",
"yo-trigger": "cd tmp/src && yo ../../generators/trigger",
Expand All @@ -37,31 +39,33 @@
"generators"
],
"dependencies": {
"@prismatic-io/spectral": "7.9.0",
"yeoman-generator": "5.6.1",
"lodash": "4.17.21",
"@apidevtools/swagger-parser": "10.1.0",
"@prismatic-io/spectral": "8.0.0",
"lodash": "4.17.21",
"number-to-words": "1.2.4",
"openapi-types": "11.0.1",
"striptags": "3.2.0",
"ts-morph": "20.0.0",
"number-to-words": "1.2.4"
"uuid": "9.0.1",
"yeoman-generator": "5.6.1"
},
"devDependencies": {
"@types/number-to-words": "1.2.1",
"@types/jest": "27.5.1",
"@types/lodash": "4.14.182",
"@types/node": "16.11.36",
"@types/jest": "27.5.1",
"@types/number-to-words": "1.2.1",
"@types/uuid": "9.0.7",
"@types/yeoman-generator": "5.2.10",
"copyfiles": "2.4.1",
"eslint": "8.16.0",
"eslint-plugin-jest": "26.2.2",
"jest": "28.1.0",
"jest-config": "28.1.0",
"mem-fs": "2.2.1",
"prettier": "2.6.2",
"ts-jest": "28.0.3",
"ts-node": "10.8.0",
"prettier": "2.6.2",
"typescript": "4.7.2",
"yo": "4.3.1",
"mem-fs": "2.2.1"
"yo": "4.3.1"
}
}
61 changes: 61 additions & 0 deletions packages/generator-spectral/src/generators/flow/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import Generator from "yeoman-generator";
import { camelCase, merge } from "lodash";
import { v4 as uuid4 } from "uuid";

class FlowGenerator extends Generator {
answers!: {
name: string;
description: string;
};

constructor(
args: string | string[],
options: Generator.GeneratorOptions,
features: Generator.GeneratorFeatures | undefined
) {
super(args, options, features);

this.option("name", {
type: String,
description: "Name of Flow",
alias: "n",
});
this.option("description", {
type: String,
description: "Description of Flow",
alias: "d",
});
}

async prompting() {
this.answers = await this.prompt([
{
type: "input",
name: "name",
message: "Name of Flow",
when: () => !Boolean(this.options.name),
},
{
type: "input",
name: "description",
message: "Description of Flow",
when: () => !Boolean(this.options.description),
},
]);

merge(this.answers, this.options);
}

async writing() {
const { name, description } = this.answers;
const key = camelCase(name);
const context = {
flow: { name, key, description, stableKey: uuid4() },
};

const destination = this.options.destinationPath || `${key}.ts`;
this.renderTemplate("flow.ts", destination, context);
}
}

module.exports = FlowGenerator;
37 changes: 37 additions & 0 deletions packages/generator-spectral/src/generators/flow/templates/flow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { flow } from "@prismatic-io/spectral";
import { createClient } from "./client";
import type { ConfigVars } from "./configVars";

export const <%= flow.key %> = flow<ConfigVars>({
name: "<%= flow.name %>",
stableKey: "<%= flow.stableKey %>",
description: "<%= flow.description %>",
onTrigger: async (context, payload, params) => {
const { logger } = context;

logger.info(`Trigger context: ${JSON.stringify(context)}`);
logger.info(`Trigger payload: ${JSON.stringify(payload)}`);
logger.info(`Trigger params: ${JSON.stringify(params)}`);

return Promise.resolve({
payload,
});
},
onExecution: async (context, params) => {
const { logger, configVars } = context;

if (!configVars?.connection1) {
throw new Error("Missing connection1 configuration");
}

logger.info(`Action context: ${JSON.stringify(context)}`);
logger.info(`Action params: ${JSON.stringify(params)}`);

const client = createClient(configVars.connection1);
return {
data: await client.call("<%= flow.name %>"),
};
},
});

export default [<%= flow.key %>];
150 changes: 150 additions & 0 deletions packages/generator-spectral/src/generators/integration/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
import path from "path";
import Generator from "yeoman-generator";
import { camelCase, merge } from "lodash";
import { v4 as uuid4 } from "uuid";

const connectionTypes = {
basic: "Basic Connection",
oauth: "OAuth 2.0",
};
type ConnectionType = keyof typeof connectionTypes;

class IntegrationGenerator extends Generator {
answers!: {
name: string;
description: string;
connectionType: ConnectionType;
};

constructor(
args: string | string[],
options: Generator.GeneratorOptions,
features: Generator.GeneratorFeatures | undefined
) {
super(args, options, features);

this.option("name", {
type: String,
description: "Name of Integration",
alias: "n",
});
this.option("description", {
type: String,
description: "Description of Integration",
alias: "d",
});
this.option("connectionType", {
type: String,
description: "Type of Connection",
alias: "t",
});
}

configVarLabel = "Connection 1";
flowName = "Flow 1";

initializing() {
this.composeWith(require.resolve("../flow"), {
destinationPath: path.join("src", "flows.ts"),
name: this.flowName,
description: "This is the first Flow",
});
}

async prompting() {
this.answers = await this.prompt([
{
type: "input",
name: "name",
message: "Name of Integration",
when: () => !Boolean(this.options.name),
},
{
type: "input",
name: "description",
message: "Description of Integration",
when: () => !Boolean(this.options.description),
},
{
type: "list",
name: "connectionType",
message: "Type of Connection",
choices: Object.entries(connectionTypes).map(([key, label]) => ({
name: label,
value: key,
})),
when: () =>
!Boolean(this.options.connectionType) ||
!Object.keys(connectionTypes).includes(this.options.connectionType),
},
]);

merge(this.answers, this.options);
}

async writing() {
const { name, description, connectionType } = this.answers;
const context = {
integration: { name, description, key: camelCase(name) },
configVar: {
key: camelCase(this.configVarLabel),
stableKey: uuid4(),
label: this.configVarLabel,
connectionType,
},
flow: { name: this.flowName },
};

const templateFiles = [
["assets", "icon.png"],
["src", "index.ts"],
["src", "index.test.ts"],
["src", "client.ts"],
"jest.config.js",
"package.json",
"tsconfig.json",
"webpack.config.js",
];
const templates = templateFiles.map<Generator.TemplateRenderOptions<this>>(
(f) => ({ source: f, destination: f })
);
this.renderTemplates(templates, context);

this.renderTemplate(
`${connectionType}.ts`,
["src", "configVars.ts"],
context
);

this.packageJson.merge({
scripts: {
build: "webpack",
import: "npm run build && prism integrations:import",
test: "jest",
lint: "eslint --ext .ts .",
},
eslintConfig: {
root: true,
extends: ["@prismatic-io/eslint-config-spectral"],
},
});

await this.addDependencies("@prismatic-io/spectral");
await this.addDevDependencies([
"eslint",
"@prismatic-io/eslint-config-spectral",
]);
await this.addDevDependencies({
"@types/jest": "26.0.24",
"copy-webpack-plugin": "11.0.0",
jest: "27.0.6",
"ts-jest": "27.0.3",
"ts-loader": "9.2.3",
typescript: "4.3.5",
webpack: "5.76.3",
"webpack-cli": "5.0.1",
});
}
}

module.exports = IntegrationGenerator;
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { connectionConfigVar } from "@prismatic-io/spectral";

export const <%= configVar.key %> = connectionConfigVar({
key: "<%= configVar.key %>",
stableKey: "<%= configVar.stableKey %>",
label: "<%= configVar.label %>",
inputs: {
username: {
label: "Username",
placeholder: "Username",
type: "string",
required: true,
comments: "Username for my Connection",
},
password: {
label: "Password",
placeholder: "Password",
type: "password",
required: true,
comments: "Password for my Connection",
},
},
});

export const configVars = { <%= configVar.key %> };
export type ConfigVars = typeof configVars;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
};
Loading
Loading