import { type AnyCommand, defineCommand, flag } from "@prisma/cli-engine"; import { notOk } from "@prisma/cli-engine/protocol"; import { composerSectionMissingError, deployConfigSection, isComposerApp, } from "./config"; import { type ComposerDeployHandler, deployService } from "./service"; /** * `deploy`: Composer's deploy for a repository with a Composer config. A * repository without one is deployed as one Service, built from its own * commands. Composer's command is taken as a parameter so its flags stay * Composer's own definitions. */ export function createDeployCommand(composerDeploy: AnyCommand) { if (composerDeploy.kind !== "result-command") { throw new Error("Composer's deploy must be a result command"); } const runComposerDeploy = composerDeploy.handler as ComposerDeployHandler; return defineCommand({ help: { summary: "Deploy the app in this folder to Prisma Cloud: build it, upload it and start it", description: "What is deployed depends on the repository. With a `composer` section in prisma.config.ts, this deploys the Prisma Composer app whose root is the entry module's default export, with its services, databases and buckets. Without one, this deploys the folder as one Service: one program that answers HTTP requests, or one folder of static files. It runs the repository's own commands and holds no framework rules. Install comes from the lockfile. Build is the `build` script of package.json. Start is the first of: the `start` script unless it runs a development server, a server file or a static site that the build wrote, an entry file such as index.ts. The Service must listen on the port in the PORT environment variable. Each of these can be replaced in the `service` section of prisma.config.ts: `install`, `build`, `start`, `static` (a folder of static files to serve) and `port`. The Service is deployed into the Project that has its name. A Project groups everything deployed for one app. The first deploy creates it, and needs its region in the PRISMA_REGION environment variable.", examples: [ "{bin} deploy", "{bin} deploy --environment staging", "{bin} deploy --name web", "{bin} deploy --entry other-module.ts", ], }, args: { flags: { ...composerDeploy.args.flags, name: flag.string({ brief: "Deploy under this name. A different name is a separate app on Prisma Cloud, with its own Project. Default for a Composer app: the name its root module declares. Default for one Service: the `name` in package.json, else the folder name", placeholder: "name", }), }, }, needs: { config: deployConfigSection, credentials: true }, handler: async (args, ctx) => { const { config } = ctx; if (config !== undefined) { return runComposerDeploy(args, { ...ctx, config }); } // Composer's flags are declared by spreading, so their types are not known here. const flags: Record = args.flags; if ( isComposerApp({ entry: typeof flags.entry === "string" ? flags.entry : undefined, cwd: ctx.cwd, configFiles: ctx.configFiles, }) ) { return notOk(composerSectionMissingError(ctx.configFiles)); } return deployService(args, ctx, runComposerDeploy); }, }); }