Skip to content

Commit

Permalink
Merge pull request #54 from prisma/feat/ppg
Browse files Browse the repository at this point in the history
add new start project
  • Loading branch information
jharrell authored Oct 22, 2024
2 parents 6dbb9ad + 27ff75d commit a01556d
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 23 deletions.
Binary file removed .DS_Store
Binary file not shown.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
node_modules
dist
.vscode
.vscode

.DS_Store
46 changes: 31 additions & 15 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { Command } from "@molt/command";
import chalk from "chalk";
import ora from "ora";

const PRISMA_STARTER_TEMPLATE = "databases/prisma-postgres";

export default class Cli {
instructions: string[] = [];
projects: string[] = [];
Expand All @@ -16,7 +18,7 @@ export default class Cli {
async initialize() {
// Grab projects
const result = await getProjects();

this.projects = result?.[0] ?? []
this.projectsWithSubfolders = result?.[1] ?? []

Expand Down Expand Up @@ -62,6 +64,23 @@ export default class Cli {
}
}

async getProductFolder(): Promise<void> {
if (this.args.folder.length) return
const starter = await prompts.selectStarterOrExample()
if (starter === "starter") {
this.args.folder = PRISMA_STARTER_TEMPLATE.split("/")[0]
this.args.template = PRISMA_STARTER_TEMPLATE
return
} else {
const projects = await prompts.selectORMorPDP()
if (projects !== "orm") {
this.args.folder = projects
} else {
this.args.folder = await prompts.getRootDir()
}
}
}

async collect() {
// Load the list of available templates
const spinner = ora();
Expand All @@ -70,18 +89,14 @@ export default class Cli {
spinner.succeed(`Loaded ${this.projects.length} templates`);

// Collect user input
if (!this.args.folder.length) {
const projects = await prompts.selectORMorPDP()
if( projects !== "orm" ) {
this.args.folder = projects;
} else {
this.args.folder = await prompts.getRootDir();
}
this.projects = this.projects.filter((project) =>
project.startsWith(this.args.folder),
);
}
await this.getProductFolder()

// filter projects based on folder
this.projects = this.projects.filter((project) =>
project.startsWith(this.args.folder),
);

// select template from list of projects
if (!this.args.template.length) {
this.args.template = await prompts.getTemplate(this.projects);
}
Expand All @@ -95,9 +110,10 @@ export default class Cli {
}

if (!this.args.name.length) {
this.args.name = await prompts.getProjectName(
this.args.template?.replace("/", "_"),
);
const defaultName = this.args.template === PRISMA_STARTER_TEMPLATE
? "hello-prisma"
: this.args.template?.replace("/", "_");
this.args.name = await prompts.getProjectName(defaultName);
}

if (!this.args.path.length) {
Expand Down
33 changes: 26 additions & 7 deletions src/cli/prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,25 @@ import SearchList from "inquirer-search-list";

inquirer.registerPrompt("search-list", SearchList);

const selectStarterOrExample = async (): Promise<string> => {
const { starter } = await inquirer.prompt({
type: "list",
message: `How would you like to start your new project?`,
name: "starter",
choices: [
{
name: `Prisma Starter (Recommended)\n\xa0\xa0- ${chalk.gray("The Prisma Starter is pre-configured with Prisma ORM, Accelerate, Pulse, and a Prisma Postgres database.")}`,
value: "starter",
},
{
name: "Explore other examples",
value: "example",
},
],
});
return starter;
}

const getTemplate = async (projects: string[]): Promise<string> => {
logger.success(
`\nDon't see what you're looking for? Request a new template here:\n\xa0\xa0➡ ${chalk.underline.gray(
Expand Down Expand Up @@ -36,25 +55,24 @@ const selectORMorPDP = async (): Promise<string> => {
`\nThese options correspond to the root directories in the prisma-examples repository:\n`,
);
const { start } = await inquirer.prompt({
// @ts-expect-error Inquirer doesn't register the type.
type: "search-list",
message: `Which Prisma product would you like to explore?`,
type: "list",
message: `Which Prisma examples would you like to explore?`,
name: "start",
choices: [
{
name: "Prisma ORM (Define Prisma schema and run queries)",
name: `Prisma ORM\n\xa0\xa0- ${chalk.gray("Define Prisma schema and run queries")}`,
value: "orm",
},
{
name: "Prisma Accelerate (Perform caching and connection pooling)",
name: `Prisma Accelerate\n\xa0\xa0- ${chalk.gray("Perform caching and connection pooling")}`,
value: "accelerate",
},
{
name: "Prisma Pulse (Monitor and react to real-time database changes)",
name: `Prisma Pulse\n\xa0\xa0- ${chalk.gray("Monitor and react to real-time database changes")}`,
value: "pulse",
},
{
name: "Prisma Optimize (Analyze and improve query performance)",
name: `Prisma Optimize\n\xa0\xa0- ${chalk.gray("Analyze and improve query performance")}`,
value: "optimize",
}
],
Expand Down Expand Up @@ -133,6 +151,7 @@ const getProjectDirectory = async (): Promise<string> => {
};

export default {
selectStarterOrExample,
selectORMorPDP,
getInstallSelection,
getProjectDirectory,
Expand Down
1 change: 1 addition & 0 deletions test/cli/input-collector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ vi.mock("../../src/helpers/getProjects", () => ({

vi.mock("../../src/cli/prompts", () => ({
default: {
selectStarterOrExample: async () => "example",
getTemplate: async () => "template",
getInstallSelection: async () => true,
selectManager: async () => "npm",
Expand Down

0 comments on commit a01556d

Please sign in to comment.