diff --git a/src/commands/preview.mjs b/src/commands/preview.mjs index 984f06f..ddc6b2e 100644 --- a/src/commands/preview.mjs +++ b/src/commands/preview.mjs @@ -1,7 +1,7 @@ import { consola } from 'consola' import { defineCommand } from 'citty' import { readFile, writeFile, unlink } from 'node:fs/promises' -import { join, relative } from 'pathe' +import { join, relative, resolve } from 'pathe' import { execa } from 'execa' import { existsSync } from 'node:fs' import { loadJsonFile } from 'load-json-file' @@ -12,9 +12,18 @@ export default defineCommand({ name: 'preview', description: 'Preview your project locally (using `wrangler pages dev`).', }, - args: {}, - async run() { - const distDir = join(process.cwd(), 'dist') + args: { + cwd: { + type: 'positional', + description: 'The directory of the application to preview.', + required: false, + default: '.' + }, + }, + async run({ args }) { + const cmdCwd = process.cwd() + const cwd = resolve(cmdCwd, args.cwd) + const distDir = join(cwd, 'dist') // Read the dist/hub.config.json file const hubConfig = await loadJsonFile(join(distDir, 'hub.config.json')).catch(() => null) if (!existsSync(distDir) || !hubConfig) { @@ -24,7 +33,7 @@ export default defineCommand({ const nitroConfig = await loadJsonFile(join(distDir, 'nitro.json')).catch(() => null) // Add .wrangler to .gitignore - const gitignorePath = join(process.cwd(), '.gitignore') + const gitignorePath = join(cwd, '.gitignore') const gitignore = await readFile(gitignorePath, 'utf-8').catch(() => '') if (gitignore && !gitignore.includes('.wrangler')) { await writeFile(gitignorePath, `${gitignore ? gitignore + '\n' : gitignore}.wrangler`, 'utf-8') @@ -33,10 +42,10 @@ export default defineCommand({ const fileSideEffects = [] // Wrangler does not support .env, only a .dev.vars // see https://developers.cloudflare.com/pages/functions/bindings/#interact-with-your-secrets-locally - const envPath = join(process.cwd(), '.env') + const envPath = join(cwd, '.env') const devVarsPath = join(distDir, '.dev.vars') if (existsSync(envPath)) { - consola.info(`Copying \`.env\` to \`${relative(process.cwd(), devVarsPath)}\`...`) + consola.info(`Copying \`.env\` to \`${relative(cwd, devVarsPath)}\`...`) const envVars = await readFile(envPath, 'utf-8').catch(() => '') await writeFile(devVarsPath, envVars, 'utf-8') fileSideEffects.push(devVarsPath) @@ -44,10 +53,10 @@ export default defineCommand({ const wrangler = generateWrangler(hubConfig, { preset: nitroConfig.preset }) const wranglerPath = join(distDir, 'wrangler.toml') - consola.info(`Generating \`${relative(process.cwd(), wranglerPath)}\`...`) + consola.info(`Generating \`${relative(cwd, wranglerPath)}\`...`) fileSideEffects.push(wranglerPath) await writeFile(wranglerPath, wrangler) - const options = { stdin: 'inherit', stdout: 'inherit', cwd: distDir, preferLocal: true, localDir: process.cwd() } + const options = { stdin: 'inherit', stdout: 'inherit', cwd: distDir, preferLocal: true, localDir: cwd } if (hubConfig.database && existsSync(join(distDir, 'database/migrations'))) { consola.info('Applying migrations...') await execa({ ...options, stdin: 'ignore' })`wrangler d1 migrations apply default --local`