WIP create-integration CLI command

This commit is contained in:
Eric Allam
2023-07-06 10:38:12 +01:00
parent bee77051e7
commit 090037602c
6 changed files with 145 additions and 6 deletions
+3 -2
View File
@@ -1,6 +1,5 @@
import { WebClient } from "@slack/web-api";
import type { IntegrationClient, TriggerIntegration } from "@trigger.dev/sdk";
import { clientFactory } from "./client";
import { joinConversation, postMessage } from "./tasks";
const tasks = {
@@ -21,7 +20,9 @@ export class Slack
this.client = {
tasks,
usesLocalAuth: false,
clientFactory,
clientFactory: (auth) => {
return new WebClient(auth.accessToken);
},
};
}
+4 -4
View File
@@ -4,11 +4,11 @@ import type {
MessageAttachment,
MessageMetadata,
WebAPIPlatformError,
WebClient,
} from "@slack/web-api";
import { clientFactory } from "./client";
import type { AuthenticatedTask } from "@trigger.dev/sdk";
type SlackClientType = ReturnType<typeof clientFactory>;
type SlackClientType = InstanceType<typeof WebClient>;
export type ChatPostMessageArguments = {
channel: string;
@@ -40,7 +40,7 @@ function isPlatformError(error: unknown): error is WebAPIPlatformError {
}
export const postMessage: AuthenticatedTask<
ReturnType<typeof clientFactory>,
SlackClientType,
ChatPostMessageArguments,
Awaited<ReturnType<SlackClientType["chat"]["postMessage"]>>
> = {
@@ -102,7 +102,7 @@ type ConversationsJoinResponse = Awaited<
>;
export const joinConversation: AuthenticatedTask<
ReturnType<typeof clientFactory>,
SlackClientType,
{ channel: string },
ConversationsJoinResponse
> = {
+1
View File
@@ -65,6 +65,7 @@
"localtunnel": "^2.0.2",
"ngrok": "5.0.0-beta.2",
"node-fetch": "^3.3.0",
"openai": "^3.3.0",
"ora": "^6.1.2",
"path-to-regexp": "^6.2.1",
"simple-git": "^3.19.0",
+21
View File
@@ -57,6 +57,27 @@ program
await devCommand(path, options);
});
// program
// .command("create-integration")
// .description("Create a new integration package for Trigger.dev")
// .argument(
// "[path]",
// "The path where you would like the package to be created",
// "."
// )
// .option(
// "-n, --package-name <package name>",
// "The name of the package to create (e.g. @trigger.dev/slack)"
// )
// .option(
// "-s, --sdk-package <integration package>",
// "The name of the SDK package to use (e.g. @slack/web-api)"
// )
// .version(getVersion(), "-v, --version", "Display the version number")
// .action(async (path, options) => {
// await createIntegrationCommand(path, options);
// });
export const promptTriggerUrl = async (): Promise<string> => {
const { instanceType } = await inquirer.prompt<{
instanceType: "cloud" | "self-hosted";
@@ -0,0 +1,114 @@
import { z } from "zod";
import { logger } from "../utils/logger.js";
import { resolvePath } from "../utils/parseNameAndPath.js";
import { COMMAND_NAME } from "../consts.js";
import inquirer from "inquirer";
// import { OpenAIApi } from "openai";
const CLIOptionsSchema = z.object({
packageName: z.string().optional(),
sdkPackage: z.string().optional(),
});
type CLIOptions = z.infer<typeof CLIOptionsSchema>;
type ResolvedCLIOptions = Required<CLIOptions>;
export async function createIntegrationCommand(path: string, cliOptions: any) {
const result = CLIOptionsSchema.safeParse(cliOptions);
if (!result.success) {
logger.error(result.error.message);
process.exit(1);
}
const options = result.data;
const resolvedPath = resolvePath(path);
const resolvedOptions = await resolveOptionsWithPrompts(
options,
resolvedPath
);
console.log(resolvedOptions);
}
const resolveOptionsWithPrompts = async (
options: CLIOptions,
_path: string
): Promise<ResolvedCLIOptions> => {
const resolvedOptions: CLIOptions = { ...options };
try {
if (!options.packageName) {
resolvedOptions.packageName = await promptPackageName();
}
if (!options.sdkPackage) {
resolvedOptions.sdkPackage = await promptSdkPackage();
}
} catch (err) {
// If the user is not calling the command from an interactive terminal, inquirer will throw an error with isTTYError = true
// If this happens, we catch the error, tell the user what has happened, and then continue to run the program with a default trigger project
// Otherwise we have to do some fancy namespace extension logic on the Error type which feels overkill for one line
// eslint-disable-next-line @typescript-eslint/no-explicit-any
if (err instanceof Error && (err as any).isTTYError) {
logger.warn(
`'${COMMAND_NAME} create-integration' needs an interactive terminal to provide options`
);
const { shouldContinue } = await inquirer.prompt<{
shouldContinue: boolean;
}>({
name: "shouldContinue",
type: "confirm",
message: `Continue initializing your trigger.dev project?`,
default: true,
});
if (!shouldContinue) {
logger.info("Exiting...");
process.exit(0);
}
} else {
throw err;
}
}
return resolvedOptions as ResolvedCLIOptions;
};
export const promptPackageName = async (): Promise<string> => {
const { packageName } = await inquirer.prompt<{ packageName: string }>({
type: "input",
name: "packageName",
message: "What is the name of your integration package?",
validate: (input) => {
if (!input) {
return "Please enter a package name";
}
return true;
},
});
return packageName;
};
export const promptSdkPackage = async (): Promise<string> => {
const { sdkPackage } = await inquirer.prompt<{ sdkPackage: string }>({
type: "input",
name: "sdkPackage",
message: "What is the name of the SDK package you would like to use?",
validate: (input) => {
if (!input) {
return "Please enter an SDK package name";
}
return true;
},
});
return sdkPackage;
};
+2
View File
@@ -678,6 +678,7 @@ importers:
localtunnel: ^2.0.2
ngrok: 5.0.0-beta.2
node-fetch: ^3.3.0
openai: ^3.3.0
ora: ^6.1.2
path-to-regexp: ^6.2.1
rimraf: ^3.0.2
@@ -701,6 +702,7 @@ importers:
localtunnel: 2.0.2
ngrok: 5.0.0-beta.2
node-fetch: 3.3.0
openai: 3.3.0
ora: 6.1.2
path-to-regexp: 6.2.1
simple-git: 3.19.0