From d98e4bac4e3a97fbf1289260530c0a2e02bf6d18 Mon Sep 17 00:00:00 2001 From: Siddharth Suresh Date: Tue, 11 Oct 2022 13:20:30 +0530 Subject: [PATCH] Migrate `blitz routes` command (#3890) --- .changeset/few-shrimps-leave.md | 5 ++ packages/blitz/src/cli/commands/routes.ts | 73 +++++++++++++++++++ packages/blitz/src/cli/index.ts | 2 + packages/blitz/src/cli/utils/next-console.ts | 21 ++---- .../blitz/src/cli/utils/routes-manifest.ts | 2 +- 5 files changed, 89 insertions(+), 14 deletions(-) create mode 100644 .changeset/few-shrimps-leave.md create mode 100644 packages/blitz/src/cli/commands/routes.ts diff --git a/.changeset/few-shrimps-leave.md b/.changeset/few-shrimps-leave.md new file mode 100644 index 0000000000..e7f1586275 --- /dev/null +++ b/.changeset/few-shrimps-leave.md @@ -0,0 +1,5 @@ +--- +"blitz": patch +--- + +Add `blitz routes` CLI command back to toolkit diff --git a/packages/blitz/src/cli/commands/routes.ts b/packages/blitz/src/cli/commands/routes.ts new file mode 100644 index 0000000000..55c65f60fe --- /dev/null +++ b/packages/blitz/src/cli/commands/routes.ts @@ -0,0 +1,73 @@ +import {CliCommand} from "../index" +import {log, newLine} from "../../logging" +import {collectAllRoutes, loadConfig} from "../utils/routes-manifest" +import chalk from "chalk" +import {prettyMs} from "../../utils" + +const getColor = (type: string) => { + switch (type) { + case "rpc": + return "magenta" + case "api": + return "blue" + default: + return "green" + } +} + +const routes: CliCommand = async () => { + const startTime = Date.now() + process.env.BLITZ_APP_DIR = process.cwd() + try { + const config = loadConfig(process.cwd()) + const routes = await collectAllRoutes(process.cwd(), config) + newLine() + const table = new log.Table({ + columns: [ + {name: "HTTP", alignment: "center"}, + {name: "Source File", alignment: "left"}, + {name: "Route Path", alignment: "left"}, + {name: "Type", alignment: "center"}, + ], + sort: (q, r) => { + // Sort pages to the top + if (q.Type === "PAGE" && r.Type !== "PAGE") { + return -1 + } + if (q.Type !== "PAGE" && r.Type === "PAGE") { + return 1 + } + + if (q.Type > r.Type) { + return 1 + } + if (q.Type < r.Type) { + return -1 + } + return 0 + }, + }) + routes.forEach(({filePath, route, verb, type}: any) => { + table.addRow( + { + [table.table.columns[0]!.name]: verb.toUpperCase(), + [table.table.columns[1]!.name]: filePath, + [table.table.columns[2]!.name]: route, + [table.table.columns[3]!.name]: type.toUpperCase(), + }, + {color: getColor(type)}, + ) + }) + console.log(table.render()) + newLine() + console.log( + `✨ Done ` + chalk.hex("#8a3df0").bold("in ") + `${prettyMs(Date.now() - startTime)}`, + ) + process.exit(0) + } catch (err) { + console.error(err) + process.exit(1) + } +} + +export {routes} diff --git a/packages/blitz/src/cli/index.ts b/packages/blitz/src/cli/index.ts index 430f972de8..262b0b42c0 100644 --- a/packages/blitz/src/cli/index.ts +++ b/packages/blitz/src/cli/index.ts @@ -36,6 +36,7 @@ const commands = { db: () => import("./commands/db").then((i) => i.db), install: () => import("./commands/install").then((i) => i.install), console: () => import("./commands/console").then((i) => i.consoleREPL), + routes: () => import("./commands/routes").then((i) => i.routes), } const aliases: Record = { @@ -46,6 +47,7 @@ const aliases: Record = { g: "generate", i: "install", c: "console", + r: "routes", } type Command = keyof typeof commands diff --git a/packages/blitz/src/cli/utils/next-console.ts b/packages/blitz/src/cli/utils/next-console.ts index 4ff091b5b7..11cae3413d 100644 --- a/packages/blitz/src/cli/utils/next-console.ts +++ b/packages/blitz/src/cli/utils/next-console.ts @@ -9,28 +9,28 @@ import ProgressBar from "progress" import {log} from "../../logging" export function getDbFolder() { - if (fs.existsSync(path.join(process.cwd(), "db"))) { - return "db" - } try { const packageJsonPath = path.join(process.cwd(), "package.json") const packageJson = fs.readFileSync(packageJsonPath, "utf8") const packageJsonObj = JSON.parse(packageJson) if (!packageJsonObj.prisma || !packageJsonObj.prisma.schema) { throw new Error( - "db folder does not exist and Prisma schema not found in package.json. Please either create the db folder or add the prisma schema path to the package.json", + "db/schema.prisma does not exist and Prisma configuration not found in package.json. Please either create the db folder or add the prisma schema path to the package.json", ) } const prismaSchemaPath = path.join(process.cwd(), packageJsonObj.prisma.schema) if (!fs.existsSync(prismaSchemaPath)) { throw new Error( - "prisma.schema file not found. Please either create the db folder or add the prisma schema path to the package.json", + `File not found in ${prismaSchemaPath}. Please either create the db/schema.prisma file or add the prisma schema path to the package.json`, ) } const folder = packageJsonObj.prisma.schema.split("/")[0] as string return folder } catch (e) { - throw new Error(e) + if (fs.existsSync(path.join(process.cwd(), "db/schema.prisma"))) { + return "db" + } + throw e } } @@ -39,13 +39,8 @@ export function getProjectRootSync() { } export function getConfigSrcPath() { - const tsPath = path.resolve(path.join(process.cwd(), "next.config.ts")) - if (fs.existsSync(tsPath)) { - return tsPath - } else { - const jsPath = path.resolve(path.join(process.cwd(), "next.config.js")) - return jsPath - } + const jsPath = path.resolve(path.join(process.cwd(), "next.config.js")) + return jsPath } const projectRoot = getProjectRootSync() diff --git a/packages/blitz/src/cli/utils/routes-manifest.ts b/packages/blitz/src/cli/utils/routes-manifest.ts index 3f54cb8179..8231a11389 100644 --- a/packages/blitz/src/cli/utils/routes-manifest.ts +++ b/packages/blitz/src/cli/utils/routes-manifest.ts @@ -151,7 +151,7 @@ const normalizeConfig = (phase: string, config: any) => { } return config } -const loadConfig = (pagesDir: string) => { +export const loadConfig = (pagesDir: string) => { let userConfigModule try {