Skip to content

Commit 366ec35

Browse files
authored
feat: link token from CLI (#20)
1 parent 43ee397 commit 366ec35

File tree

2 files changed

+60
-4
lines changed

2 files changed

+60
-4
lines changed

README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ COMMANDS
3737
Use nuxthub <command> --help for more information about a command.
3838
```
3939
40-
## Deployment
40+
## Deploy
4141
42-
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+
To deploy your project with NuxtHub, use the `nuxthub deploy` command. This will build your project and deploy it to your Cloudflare account with zero-configuration.
4343
4444
```bash
4545
# Deploy to production or preview based on your current branch
@@ -54,7 +54,7 @@ nuxthub deploy --preview
5454
5555
See [how to deploy with a GitHub action](https://hub.nuxt.com/docs/getting-started/deploy#github-action).
5656
57-
## Open in Browser
57+
## Open in browser
5858
5959
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.
6060
@@ -69,6 +69,14 @@ nuxthub open --production
6969
nuxthub open --preview
7070
```
7171
72+
## Open the project admin
73+
74+
To open your project's admin in the browser, you can use the `nuxthub manage` command. This will open the NuxtHub admin URL of your project in the default browser.
75+
76+
```bash
77+
nuxthub manage
78+
```
79+
7280
## License
7381

7482
[Apache 2.0](./LICENSE)

src/utils/data.mjs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { consola } from 'consola'
22
import { colors } from 'consola/utils'
3-
import { isCancel, select, text } from '@clack/prompts'
3+
import { isCancel, select, text, password } from '@clack/prompts'
44
import { joinURL } from 'ufo'
55
import { ofetch } from 'ofetch'
66
import { gitInfo } from './git.mjs'
@@ -47,6 +47,54 @@ export async function selectTeam() {
4747
} else {
4848
team = teams[0]
4949
}
50+
51+
if (!team.cloudflareAccountId) {
52+
return await linkCloudflareAccount(team)
53+
}
54+
return team
55+
}
56+
57+
export async function linkCloudflareAccount(team, retry = false) {
58+
if (!retry) {
59+
const tokenLink = joinURL(NUXT_HUB_URL, `cloudflare-token?name=NuxtHub+Team+${team.name}`)
60+
consola.info(`You need to link your Cloudflare account to the \`${team.name}\` team.`)
61+
consola.info(`Create a new Cloudflare API token by following this link:\n\`${encodeURI(tokenLink)}\``)
62+
}
63+
let apiToken = await password({
64+
message: 'Cloudflare API token'
65+
})
66+
if (isCancel(apiToken)) return null
67+
68+
const cfAccounts = await $api('/cloudflare/accounts', {
69+
params: { apiToken }
70+
}).catch(() => {
71+
consola.error('Couldn\'t list Cloudflare accounts\nPlease check your API Token, make sure to have "Account Settings: Read" permission.')
72+
return null
73+
})
74+
if (!cfAccounts) return linkCloudflareAccount(team, true)
75+
76+
let accountId
77+
if (cfAccounts.length > 1) {
78+
accountId = await select({
79+
message: 'Select a Cloudflare account',
80+
options: cfAccounts
81+
})
82+
} else {
83+
accountId = cfAccounts[0].id
84+
}
85+
if (isCancel(accountId)) return null
86+
87+
const account = await $api(`/teams/${team.slug}/accounts/cloudflare`, {
88+
method: 'PUT',
89+
body: { apiToken, accountId }
90+
})
91+
.catch((err) => {
92+
consola.error(err.data?.message || err.message)
93+
return null
94+
})
95+
if (!account) return linkCloudflareAccount(team, true)
96+
consola.success('Cloudflare account linked.')
97+
5098
return team
5199
}
52100

0 commit comments

Comments
 (0)