11import { consola } from 'consola'
22import { defineCommand } from 'citty'
33import { readFile , writeFile , unlink } from 'node:fs/promises'
4- import { join , relative } from 'pathe'
4+ import { join , relative , resolve } from 'pathe'
55import { execa } from 'execa'
66import { existsSync } from 'node:fs'
77import { loadJsonFile } from 'load-json-file'
@@ -12,9 +12,18 @@ export default defineCommand({
1212 name : 'preview' ,
1313 description : 'Preview your project locally (using `wrangler pages dev`).' ,
1414 } ,
15- args : { } ,
16- async run ( ) {
17- const distDir = join ( process . cwd ( ) , 'dist' )
15+ args : {
16+ cwd : {
17+ type : 'positional' ,
18+ description : 'The directory of the application to preview.' ,
19+ required : false ,
20+ default : '.'
21+ } ,
22+ } ,
23+ async run ( { args } ) {
24+ const cmdCwd = process . cwd ( )
25+ const cwd = resolve ( cmdCwd , args . cwd )
26+ const distDir = join ( cwd , 'dist' )
1827 // Read the dist/hub.config.json file
1928 const hubConfig = await loadJsonFile ( join ( distDir , 'hub.config.json' ) ) . catch ( ( ) => null )
2029 if ( ! existsSync ( distDir ) || ! hubConfig ) {
@@ -24,7 +33,7 @@ export default defineCommand({
2433 const nitroConfig = await loadJsonFile ( join ( distDir , 'nitro.json' ) ) . catch ( ( ) => null )
2534
2635 // Add .wrangler to .gitignore
27- const gitignorePath = join ( process . cwd ( ) , '.gitignore' )
36+ const gitignorePath = join ( cwd , '.gitignore' )
2837 const gitignore = await readFile ( gitignorePath , 'utf-8' ) . catch ( ( ) => '' )
2938 if ( gitignore && ! gitignore . includes ( '.wrangler' ) ) {
3039 await writeFile ( gitignorePath , `${ gitignore ? gitignore + '\n' : gitignore } .wrangler` , 'utf-8' )
@@ -33,21 +42,21 @@ export default defineCommand({
3342 const fileSideEffects = [ ]
3443 // Wrangler does not support .env, only a .dev.vars
3544 // see https://developers.cloudflare.com/pages/functions/bindings/#interact-with-your-secrets-locally
36- const envPath = join ( process . cwd ( ) , '.env' )
45+ const envPath = join ( cwd , '.env' )
3746 const devVarsPath = join ( distDir , '.dev.vars' )
3847 if ( existsSync ( envPath ) ) {
39- consola . info ( `Copying \`.env\` to \`${ relative ( process . cwd ( ) , devVarsPath ) } \`...` )
48+ consola . info ( `Copying \`.env\` to \`${ relative ( cwd , devVarsPath ) } \`...` )
4049 const envVars = await readFile ( envPath , 'utf-8' ) . catch ( ( ) => '' )
4150 await writeFile ( devVarsPath , envVars , 'utf-8' )
4251 fileSideEffects . push ( devVarsPath )
4352 }
4453
4554 const wrangler = generateWrangler ( hubConfig , { preset : nitroConfig . preset } )
4655 const wranglerPath = join ( distDir , 'wrangler.toml' )
47- consola . info ( `Generating \`${ relative ( process . cwd ( ) , wranglerPath ) } \`...` )
56+ consola . info ( `Generating \`${ relative ( cwd , wranglerPath ) } \`...` )
4857 fileSideEffects . push ( wranglerPath )
4958 await writeFile ( wranglerPath , wrangler )
50- const options = { stdin : 'inherit' , stdout : 'inherit' , cwd : distDir , preferLocal : true , localDir : process . cwd ( ) }
59+ const options = { stdin : 'inherit' , stdout : 'inherit' , cwd : distDir , preferLocal : true , localDir : cwd }
5160 if ( hubConfig . database && existsSync ( join ( distDir , 'database/migrations' ) ) ) {
5261 consola . info ( 'Applying migrations...' )
5362 await execa ( { ...options , stdin : 'ignore' } ) `wrangler d1 migrations apply default --local`
0 commit comments