Skip to content

feat(preview): support cwd as positional arg #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 29, 2025
Merged
Changes from all 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
27 changes: 18 additions & 9 deletions src/commands/preview.mjs
Original file line number Diff line number Diff line change
@@ -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,21 +42,21 @@ 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)
}

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`