Skip to content

Commit 3c2e5a2

Browse files
authored
feat: add env aware nuxthub open (#2)
1 parent bbe7da4 commit 3c2e5a2

File tree

2 files changed

+58
-6
lines changed

2 files changed

+58
-6
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,36 @@ COMMANDS
3636
Use nuxthub <command> --help for more information about a command.
3737
```
3838
39+
## Deployment
40+
41+
To deploy your project to NuxtHub, you can use the `nuxthub deploy` command. This will build your project and deploy it to your Cloudflare account with zero-configuration.
42+
43+
```bash
44+
# Deploy to production or preview based on your current branch
45+
nuxthub deploy
46+
47+
# Deploy to production
48+
nuxthub deploy --production
49+
50+
# Deploy to preview
51+
nuxthub deploy --preview
52+
```
53+
54+
## Open in Browser
55+
56+
To open your project in the browser, you can use the `nuxthub open` command. This will open the URL of your project in the default browser.
57+
58+
```bash
59+
# Open the production or preview deployment based on your current branch
60+
nuxthub open
61+
62+
# Open the production deployment
63+
nuxthub open --production
64+
65+
# Open the latest preview deployment
66+
nuxthub open --preview
67+
```
68+
3969
## License
4070
4171
[Apache 2.0](./LICENSE)

src/commands/open.mjs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { consola } from 'consola'
22
import { colors } from 'consola/utils'
33
import { isCancel, confirm } from '@clack/prompts'
44
import { defineCommand, runCommand } from 'citty'
5-
import { fetchUser, projectPath, fetchProject } from '../utils/index.mjs'
5+
import { fetchUser, projectPath, fetchProject, gitInfo } from '../utils/index.mjs'
66
import open from 'open'
77
import login from './login.mjs'
88
import link from './link.mjs'
@@ -12,7 +12,19 @@ export default defineCommand({
1212
name: 'open',
1313
description: 'Open in browser the project\'s URL linked to the current directory.',
1414
},
15-
async setup() {
15+
args: {
16+
production: {
17+
type: 'boolean',
18+
description: 'Open the production deployment.',
19+
default: false
20+
},
21+
preview: {
22+
type: 'boolean',
23+
description: 'Open the latest preview deployment.',
24+
default: false
25+
}
26+
},
27+
async setup({ args }) {
1628
let user = await fetchUser()
1729
if (!user) {
1830
consola.info('Please login to open a project in your browser.')
@@ -36,14 +48,24 @@ export default defineCommand({
3648
return console.log('project is null')
3749
}
3850
}
51+
// Get the environment based on branch
52+
let env = 'production'
53+
if (args.preview) {
54+
env = 'preview'
55+
} else if (!args.production && !args.preview) {
56+
const git = gitInfo()
57+
// Guess the env based on the branch
58+
env = (git.branch === project.productionBranch) ? 'production' : 'preview'
59+
}
60+
const url = (env === 'production' ? project.url : project.previewUrl)
3961

40-
if (!project.url) {
41-
consola.info(`Project \`${project.slug}\` does not have a URL, please run \`nuxthub deploy\`.`)
62+
if (!url) {
63+
consola.info(`Project \`${project.slug}\` does not have a \`${env}\` URL, please run \`nuxthub deploy --${env}\`.`)
4264
return
4365
}
4466

45-
open(project.url)
67+
open(url)
4668

47-
consola.success(`Project \`${project.url}\` opened in the browser.`)
69+
consola.success(`Project \`${url}\` opened in the browser.`)
4870
},
4971
})

0 commit comments

Comments
 (0)