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

feat: add external provider scaffold #43

Merged
merged 1 commit into from
Apr 28, 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
3 changes: 2 additions & 1 deletion src/help/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const helpForm = async (): Promise<void> => {
chalk.green("Alias"),
chalk.green("Description"),
],
colWidths: [15, 10, 60],
colWidths: [15, 15, 60],
});

table.push(
Expand All @@ -26,6 +26,7 @@ const helpForm = async (): Promise<void> => {
["dto", "g d", "Generate a dto"],
["entity", "g e", "Generate an entity"],
["provider", "g p", "Generate a provider"],
["provider external", "a provider", "Generate an external provider"],
["module", "g mo", "Generate a module"],
["middleware", "g mi", "Generate a middleware"],
);
Expand Down
6 changes: 5 additions & 1 deletion src/providers/cli.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Argv, CommandModule } from "yargs";
import { externalProvider } from "./external/external.provider";
import { prismaProvider } from "./prisma/prisma.provider";

// eslint-disable-next-line @typescript-eslint/ban-types
Expand All @@ -12,9 +13,10 @@ const generateProviders = (): CommandModule<CommandModuleArgs, any> => {
builder: (yargs: Argv): Argv => {
yargs
.positional("provider", {
choices: ["prisma"] as const,
choices: ["prisma", "provider"] as const,
describe: "The provider to add to the project",
type: "string",
alias: "p",
})
.option("library-version", {
describe: "The library version to install",
Expand All @@ -34,6 +36,8 @@ const generateProviders = (): CommandModule<CommandModuleArgs, any> => {
handler: async ({ provider, libraryVersion, providerVersion }) => {
if (provider === "prisma") {
await prismaProvider(libraryVersion, providerVersion);
} else if (provider === "provider") {
await externalProvider();
}
},
};
Expand Down
68 changes: 68 additions & 0 deletions src/providers/external/external.provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import chalk from "chalk";
import degit from "degit";
import inquirer from "inquirer";
import { centerText } from "../../utils/center-text";
import { printError } from "../../utils/cli-ui";

async function printInfo(providerName: string): Promise<void> {
console.log("\n");
console.log(
"🐎 Provider",
chalk.green(providerName),
"created successfully!",
);
console.log("🤙 Run the following commands to start the provider:\n");

console.log(chalk.bold.gray(`$ cd ${providerName}`));

console.log("\n");
console.log(chalk.bold.green(centerText("Happy coding!")));
console.log(
chalk.bold.gray(
centerText("Please consider donating to support the project.\n"),
),
);
console.log(
chalk.bold.white(
centerText("💖 Sponsor: https://github.com/sponsors/expressots"),
),
);
console.log("\n");
}

interface IExternalProvider {
providerName: string;
}

const externalProvider = async (): Promise<void> => {
return new Promise<void>(async (resolve, reject) => {
const providerInfo = await inquirer.prompt<IExternalProvider>([
{
type: "input",
name: "providerName",
message: "Type the name of your provider:",
default: "expressots-provider",
transformer: (input: string) => {
return chalk.yellow(chalk.bold(input));
},
},
]);

try {
const emitter = degit(`expressots/expressots-provider-template`);
await emitter.clone(providerInfo.providerName);
printInfo(providerInfo.providerName);

resolve();
} catch (err: any) {
console.log("\n");
printError(
"Project already exists or Folder is not empty",
"generate-external-provider",
);
reject(err);
}
});
};

export { externalProvider };