Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions src/build/rolldown/prod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ export async function buildProduction(nitro: Nitro, config: RolldownOptions) {
const rewriteRelativePaths = (input: string) => {
return input.replace(/([\s:])\.\/(\S*)/g, `$1${rOutput}/$2`);
};
nitro.logger.success(`You can preview this build using \`npx nitro preview\``);
nitro.logger.success("You can preview this build using `npx nitro preview`");
if (buildInfo.commands!.deploy) {
nitro.logger.success(
`You can deploy this build using \`${rewriteRelativePaths(buildInfo.commands!.deploy)}\``
rewriteRelativePaths("You can deploy this build using `npx nitro deploy --prebuilt`")
);
}
}
11 changes: 2 additions & 9 deletions src/build/rollup/prod.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { Nitro, RollupConfig } from "nitro/types";
import { formatCompatibilityDate } from "compatx";
import { relative } from "pathe";
import { scanHandlers } from "../../scan.ts";
import { generateFSTree } from "../../utils/fs-tree.ts";
import { writeTypes } from "../types.ts";
Expand Down Expand Up @@ -47,14 +46,8 @@ export async function buildProduction(nitro: Nitro, rollupConfig: RollupConfig)
await nitro.hooks.callHook("compiled", nitro);

// Show deploy and preview hints
const rOutput = relative(process.cwd(), nitro.options.output.dir);
const rewriteRelativePaths = (input: string) => {
return input.replace(/([\s:])\.\/(\S*)/g, `$1${rOutput}/$2`);
};
nitro.logger.success(`You can preview this build using \`npx nitro preview\``);
nitro.logger.success("You can preview this build using `npx nitro preview`");
if (buildInfo.commands!.deploy) {
nitro.logger.success(
`You can deploy this build using \`${rewriteRelativePaths(buildInfo.commands!.deploy)}\``
);
nitro.logger.success("You can deploy this build using `npx nitro deploy --prebuilt`");
}
}
13 changes: 3 additions & 10 deletions src/build/vite/prod.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ViteBuilder } from "vite";
import type { NitroPluginContext } from "./types.ts";

import { basename, dirname, relative, resolve } from "pathe";
import { basename, dirname, resolve } from "pathe";
import { formatCompatibilityDate } from "compatx";
import { colors as C } from "consola/utils";
import { copyPublicAssets } from "../../builder.ts";
Expand Down Expand Up @@ -120,17 +120,10 @@ export async function buildEnvironments(ctx: NitroPluginContext, builder: ViteBu
await writeBuildInfo(nitro, output);

// Show deploy and preview commands
const rOutput = relative(process.cwd(), nitro.options.output.dir);
const rewriteRelativePaths = (input: string) => {
return input.replace(/([\s:])\.\/(\S*)/g, `$1${rOutput}/$2`);
};

if (!isTest && !isCI) console.log();
nitro.logger.success(`You can preview this build using \`npx vite preview\``);
nitro.logger.success("You can preview this build using `npx vite preview`");
if (nitro.options.commands.deploy) {
nitro.logger.success(
`You can deploy this build using \`${rewriteRelativePaths(nitro.options.commands.deploy)}\``
);
nitro.logger.success("You can deploy this build using `npx nitro deploy --prebuilt`");
}
}

Expand Down
45 changes: 23 additions & 22 deletions src/cli/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,34 @@ import { build, copyPublicAssets, createNitro, prepare, prerender } from "nitro/
import { resolve } from "pathe";
import { commonArgs } from "../common.ts";

export const buildArgs = {
...commonArgs,
minify: {
type: "boolean",
description:
"Minify the output (overrides preset defaults you can also use `--no-minify` to disable).",
},
preset: {
type: "string",
description: "The build preset to use (you can also use `NITRO_PRESET` environment variable).",
},
builder: {
type: "string",
description: "The builder to use (you can also use `NITRO_BUILDER` environment variable).",
},
compatibilityDate: {
type: "string",
description:
"The date to use for preset compatibility (you can also use `NITRO_COMPATIBILITY_DATE` environment variable).",
},
} as const;

export default defineCommand({
meta: {
name: "build",
description: "Build nitro project for production",
},
args: {
...commonArgs,
minify: {
type: "boolean",
description:
"Minify the output (overrides preset defaults you can also use `--no-minify` to disable).",
},
preset: {
type: "string",
description:
"The build preset to use (you can also use `NITRO_PRESET` environment variable).",
},
builder: {
type: "string",
description: "The builder to use (you can also use `NITRO_BUILDER` environment variable).",
},
compatibilityDate: {
type: "string",
description:
"The date to use for preset compatibility (you can also use `NITRO_COMPATIBILITY_DATE` environment variable).",
},
},
args: buildArgs,
async run({ args }) {
const rootDir = resolve((args.dir || args._dir || ".") as string);
const nitro = await createNitro(
Expand Down
45 changes: 45 additions & 0 deletions src/cli/commands/deploy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { defineCommand } from "citty";
import { relative, resolve } from "pathe";
import consola from "consola";
import { execSync } from "node:child_process";
import { getBuildInfo } from "../../build/info.ts";
import buildCmd, { buildArgs } from "./build.ts";

export default defineCommand({
meta: {
name: "deploy",
description: "Build and deploy nitro project for production",
},
args: {
...buildArgs,
prebuilt: {
type: "boolean",
description: "Skip the build step and deploy the existing build",
},
},
async run(ctx) {
if (!ctx.args.prebuilt) {
await buildCmd.run!(ctx as any);
}
const rootDir = resolve((ctx.args.dir || ctx.args._dir || ".") as string);
const { buildInfo, outputDir } = await getBuildInfo(rootDir);
if (!buildInfo) {
// throw new Error("No build info found, cannot deploy.");
consola.error("No build info found, cannot deploy.");
process.exit(1);
}
if (!buildInfo.commands?.deploy) {
consola.error(
`The \`${buildInfo.preset}\` preset does not have a default deploy command.\n\nTry using a different preset with the \`--preset\` option, or configure a deploy command in the Nitro config, or deploy manually.`
);
process.exit(1);
}

const deployCommand = buildInfo.commands.deploy.replace(
/([\s:])\.\/(\S*)/g,
`$1${relative(process.cwd(), outputDir)}/$2`
);
consola.info(`$ ${deployCommand}`);
execSync(deployCommand, { stdio: "inherit" });
},
});
1 change: 1 addition & 0 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const main = defineCommand({
subCommands: {
dev: () => import("./commands/dev.ts").then((r) => r.default),
build: () => import("./commands/build.ts").then((r) => r.default),
deploy: () => import("./commands/deploy.ts").then((r) => r.default),
prepare: () => import("./commands/prepare.ts").then((r) => r.default),
task: () => import("./commands/task/index.ts").then((r) => r.default),
preview: () => import("./commands/preview.ts").then((r) => r.default),
Expand Down
2 changes: 1 addition & 1 deletion src/presets/deno/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const denoDeploy = defineNitroPreset(
serveStatic: "deno",
commands: {
preview: "",
deploy: "cd ./ && deployctl deploy --project=<project_name> server/index.ts",
deploy: "cd ./ && deno run -A jsr:@deno/deployctl deploy server/index.ts",
},
unenv: unenvDeno,
rollupConfig: {
Expand Down
Loading