Skip to content

Commit 42a23de

Browse files
authored
fix(build): resolve deploy and preview commands (#3481)
1 parent a1b3b1b commit 42a23de

File tree

8 files changed

+33
-47
lines changed

8 files changed

+33
-47
lines changed

src/core/build/prod.ts

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import { snapshotStorage } from "../utils/storage";
1313
import { formatRollupError } from "./error";
1414
import { writeTypes } from "./types";
1515
import { getProperty } from "dot-prop";
16-
import { consola } from "consola";
1716

1817
export async function buildProduction(
1918
nitro: Nitro,
@@ -45,8 +44,8 @@ export async function buildProduction(
4544
nitro: nitroVersion,
4645
},
4746
commands: {
48-
preview: nitro.options.commands.preview,
49-
deploy: nitro.options.commands.deploy,
47+
preview: resolveTmplPath(nitro.options.commands.preview, nitro),
48+
deploy: resolveTmplPath(nitro.options.commands.deploy, nitro),
5049
},
5150
config: {
5251
...Object.fromEntries(
@@ -72,26 +71,14 @@ export async function buildProduction(
7271
await nitro.hooks.callHook("compiled", nitro);
7372

7473
// Show deploy and preview hints
75-
const rOutput = relative(process.cwd(), nitro.options.output.dir);
76-
const rewriteRelativePaths = (input: string) => {
77-
return input.replace(/([\s:])\.\/(\S*)/g, `$1${rOutput}/$2`);
78-
};
79-
if (buildInfo.commands!.preview) {
74+
if (buildInfo.commands?.preview) {
8075
nitro.logger.success(
81-
`You can preview this build using \`${_compilePathCommandTemplate(
82-
rewriteRelativePaths(buildInfo.commands!.preview),
83-
nitro.options,
84-
nitro.options.rootDir
85-
)}\``
76+
`You can preview this build using \`${buildInfo.commands?.preview}\``
8677
);
8778
}
88-
if (buildInfo.commands!.deploy) {
79+
if (buildInfo.commands?.deploy) {
8980
nitro.logger.success(
90-
`You can deploy this build using \`${_compilePathCommandTemplate(
91-
rewriteRelativePaths(buildInfo.commands!.deploy),
92-
nitro.options,
93-
nitro.options.rootDir
94-
)}\``
81+
`You can deploy this build using \`${buildInfo.commands?.deploy}\``
9582
);
9683
}
9784
}
@@ -123,22 +110,20 @@ async function _snapshot(nitro: Nitro) {
123110
);
124111
}
125112

126-
function _compilePathCommandTemplate(
127-
contents: string,
128-
data: Record<string, any>,
129-
base: string
130-
) {
131-
if (!contents.includes("{{")) {
132-
return contents;
113+
function resolveTmplPath(input: string | undefined, nitro: Nitro) {
114+
if (!input || !input.includes("{{")) {
115+
return input;
133116
}
134-
135-
return contents.replace(/{{ ?([\w.]+) ?}}/g, (_, match) => {
136-
let val = getProperty<Record<string, string>, string>(data, match);
117+
return input.replace(/{{ ?([\w.]+) ?}}/g, (_, match) => {
118+
let val = getProperty<Record<string, string>, string>(
119+
nitro.options as unknown as Record<string, string>,
120+
match
121+
);
137122
if (val) {
138-
val = relative(base, val);
123+
val = relative(nitro.options.rootDir, val);
139124
} else {
140-
consola.warn(
141-
`cannot resolve template param '${match}' in ${contents.slice(0, 20)}`
125+
nitro.logger.warn(
126+
`cannot resolve template param '${match}' in ${input.slice(0, 20)}`
142127
);
143128
}
144129
return val || `${match}`;

src/presets/cloudflare/preset.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ const cloudflarePages = defineNitroPreset(
2121
entry: "./runtime/cloudflare-pages",
2222
exportConditions: ["workerd"],
2323
commands: {
24-
preview: "npx wrangler --cwd ./ pages dev",
25-
deploy: "npx wrangler --cwd ./ pages deploy",
24+
preview: "npx wrangler --cwd {{ output.dir }} pages dev",
25+
deploy: "npx wrangler --cwd {{ output.dir }} pages deploy",
2626
},
2727
output: {
2828
dir: "{{ rootDir }}/dist",
@@ -80,8 +80,8 @@ const cloudflarePagesStatic = defineNitroPreset(
8080
publicDir: "{{ output.dir }}/{{ baseURL }}",
8181
},
8282
commands: {
83-
preview: "npx wrangler --cwd ./ pages dev",
84-
deploy: "npx wrangler --cwd ./ pages deploy",
83+
preview: "npx wrangler --cwd {{ output.dir }} pages dev",
84+
deploy: "npx wrangler --cwd {{ output.dir }} pages deploy",
8585
},
8686
hooks: {
8787
"build:before": async (nitro) => {
@@ -132,8 +132,8 @@ const cloudflareModule = defineNitroPreset(
132132
},
133133
exportConditions: ["workerd"],
134134
commands: {
135-
preview: "npx wrangler --cwd ./ dev",
136-
deploy: "npx wrangler --cwd ./ deploy",
135+
preview: "npx wrangler --cwd {{ output.dir }} dev",
136+
deploy: "npx wrangler --cwd {{ output.dir }} deploy",
137137
},
138138
unenv: [unenvCfExternals],
139139
rollupConfig: {

src/presets/deno/preset-legacy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const denoServerLegacy = defineNitroPreset(
1414
entry: "./runtime/deno-server",
1515
exportConditions: ["deno"],
1616
commands: {
17-
preview: "deno task --config ./deno.json start",
17+
preview: "deno task --config {{ output.dir }}/deno.json start",
1818
},
1919
unenv: {
2020
inject: {

src/presets/deno/preset.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const denoDeploy = defineNitroPreset(
1515
commands: {
1616
preview: "",
1717
deploy:
18-
"cd ./ && deployctl deploy --project=<project_name> {{ output.serverDir }}/index.ts",
18+
"cd {{ output.dir }} && deployctl deploy --project=<project_name> ./server/index.ts",
1919
},
2020
unenv: unenvDenoPreset,
2121
rollupConfig: {
@@ -41,7 +41,7 @@ const denoServer = defineNitroPreset(
4141
entry: "./runtime/deno-server",
4242
exportConditions: ["deno"],
4343
commands: {
44-
preview: "deno task --config ./deno.json start",
44+
preview: "deno task --config {{ output.dir }}/deno.json start",
4545
},
4646
rollupConfig: {
4747
external: (id) => id.startsWith("https://"),

src/presets/edgio/preset.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ const edgio = defineNitroPreset(
77
{
88
extends: "node-server",
99
commands: {
10-
deploy: "cd ./ && npm run deploy",
11-
preview: "cd ./ && npm run preview",
10+
deploy: "cd {{ output.dir }} && npm run deploy",
11+
preview: "cd {{ output.dir }} && npm run preview",
1212
},
1313
hooks: {
1414
async compiled(nitro) {

src/presets/netlify/legacy/preset.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ const netlifyStatic = defineNitroPreset(
134134
publicDir: "{{ rootDir }}/dist",
135135
},
136136
commands: {
137-
preview: "npx serve ./",
137+
preview: "npx serve {{ output.dir }}",
138138
},
139139
hooks: {
140140
"rollup:before": (nitro: Nitro) => {

src/presets/netlify/preset.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ const netlifyStatic = defineNitroPreset(
123123
publicDir: "{{ rootDir }}/dist/{{ baseURL }}",
124124
},
125125
commands: {
126-
preview: "npx serve ./",
126+
preview: "npx serve {{ output.dir }}",
127127
},
128128
hooks: {
129129
async compiled(nitro: Nitro) {

src/presets/vercel/preset.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ const vercel = defineNitroPreset(
2222
publicDir: "{{ output.dir }}/static/{{ baseURL }}",
2323
},
2424
commands: {
25-
deploy: "",
2625
preview: "",
26+
deploy: "npx vercel deploy --prebuilt",
2727
},
2828
hooks: {
2929
"rollup:before": (nitro: Nitro) => {
@@ -52,8 +52,8 @@ const vercelEdge = defineNitroPreset(
5252
publicDir: "{{ output.dir }}/static/{{ baseURL }}",
5353
},
5454
commands: {
55-
deploy: "",
5655
preview: "",
56+
deploy: "npx vercel deploy --prebuilt",
5757
},
5858
unenv: {
5959
external: builtnNodeModules.flatMap((m) => `node:${m}`),
@@ -99,6 +99,7 @@ const vercelStatic = defineNitroPreset(
9999
},
100100
commands: {
101101
preview: "npx serve {{ output.publicDir }}",
102+
deploy: "npx vercel deploy --prebuilt",
102103
},
103104
hooks: {
104105
"rollup:before": (nitro: Nitro) => {

0 commit comments

Comments
 (0)