Skip to content

Commit

Permalink
Merge pull request #43 from expressots/40-create-scaffold-for-externa…
Browse files Browse the repository at this point in the history
…l-provider-npm-package

feat: add external provider scaffold
  • Loading branch information
rsaz authored Apr 28, 2024
2 parents 7f4f9b1 + 8ed033b commit 6659ff0
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
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 };

0 comments on commit 6659ff0

Please sign in to comment.