Skip to content

Commit 66a8335

Browse files
authored
feat: environments (#61)
1 parent 22d25cb commit 66a8335

File tree

3 files changed

+49
-15
lines changed

3 files changed

+49
-15
lines changed

src/commands/deploy.mjs

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { defineCommand, runCommand } from 'citty'
77
import { join, resolve, relative } from 'pathe'
88
import { execa } from 'execa'
99
import { setupDotenv } from 'c12'
10-
import { $api, fetchUser, selectTeam, selectProject, projectPath, fetchProject, linkProject, gitInfo } from '../utils/index.mjs'
10+
import { $api, fetchUser, selectTeam, selectProject, projectPath, fetchProject, linkProject, gitInfo, determineEnvironment } from '../utils/index.mjs'
1111
import { getStorage, getPathsToDeploy, getFile, uploadAssetsToCloudflare, uploadWorkersAssetsToCloudflare, isMetaPath, isWorkerMetaPath, isServerPath, isWorkerServerPath, getPublicFiles, getWorkerPublicFiles } from '../utils/deploy.mjs'
1212
import { createMigrationsTable, fetchRemoteMigrations, queryDatabase } from '../utils/database.mjs'
1313
import login from './login.mjs'
@@ -40,6 +40,11 @@ export default defineCommand({
4040
description: 'Force the current deployment as preview.',
4141
default: false
4242
},
43+
env: {
44+
type: 'string',
45+
description: 'Force the environment of the current deployment. Available for Workers projects only.',
46+
default: ''
47+
},
4348
dotenv: {
4449
type: 'string',
4550
description: 'Point to another .env file to load, relative to the root directory.',
@@ -92,6 +97,7 @@ export default defineCommand({
9297
// Default to main branch
9398
git.branch = git.branch || 'main'
9499
let deployEnv = git.branch === linkedProject.productionBranch ? 'production' : 'preview'
100+
95101
if (args.production) {
96102
git.branch = linkedProject.productionBranch
97103
deployEnv = 'production'
@@ -100,21 +106,20 @@ export default defineCommand({
100106
git.branch += '-preview'
101107
}
102108
deployEnv = 'preview'
109+
} else if (linkedProject.type === 'worker' && args.env) {
110+
deployEnv = args.env
111+
} else if (linkedProject.type === 'worker' && git.branch !== linkedProject.productionBranch) {
112+
deployEnv = await determineEnvironment(linkedProject.teamSlug, linkedProject.slug, git.branch)
103113
}
104-
const deployEnvColored = deployEnv === 'production' ? colors.greenBright(deployEnv) : colors.yellowBright(deployEnv)
114+
115+
const deployEnvColored = deployEnv === 'production'
116+
? colors.greenBright(deployEnv)
117+
: deployEnv === 'preview'
118+
? colors.yellowBright(deployEnv)
119+
: colors.blueBright(deployEnv) // additional environments
105120
consola.success(`Connected to ${colors.blueBright(linkedProject.teamSlug)} team.`)
106121
consola.success(`Linked to ${colors.blueBright(linkedProject.slug)} project.`)
107122

108-
if (linkedProject.type === 'worker' && deployEnv === 'preview') {
109-
consola.warn('Currently NuxtHub on Workers (BETA) does not support preview environments.')
110-
const shouldDeploy = await confirm({
111-
message: `Deploy ${colors.blueBright(projectPath())} to production instead?`
112-
})
113-
if (!shouldDeploy || isCancel(shouldDeploy)) {
114-
return consola.log('Cancelled.')
115-
}
116-
}
117-
118123
// #region Build
119124
if (args.build) {
120125
consola.info('Building the Nuxt project...')
@@ -125,7 +130,15 @@ export default defineCommand({
125130
if (args.dotenv) {
126131
nuxiBuildArgs.push(`--dotenv=${args.dotenv}`)
127132
}
128-
await execa({ stdio: 'inherit', preferLocal: true, cwd, extendEnv: false, env: {} })`nuxi build ${nuxiBuildArgs}`
133+
await execa({
134+
stdio: 'inherit',
135+
preferLocal: true,
136+
cwd,
137+
extendEnv: false,
138+
env: {
139+
REMOTE_PROJECT_TYPE: linkedProject.type === 'worker' ? 'workers' : 'pages'
140+
}
141+
})`nuxi build ${nuxiBuildArgs}`
129142
.catch((err) => {
130143
if (err.code === 'ENOENT') {
131144
consola.error('`nuxt` is not installed, please make sure that you are inside a Nuxt project.')

src/utils/data.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ export async function selectProject(team) {
135135
message: 'Select your project type',
136136
initialValue: 'pages',
137137
options: [
138+
{ label: 'Cloudflare Workers', value: 'worker' },
138139
{ label: 'Cloudflare Pages', value: 'pages' },
139-
{ label: 'Cloudflare Workers (beta)', value: 'worker' },
140140
]
141141
})
142142
if (isCancel(projectType)) return null

src/utils/deploy.mjs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import { ofetch } from 'ofetch'
66
import { createStorage } from 'unstorage'
77
import fsDriver from 'unstorage/drivers/fs'
88
import mime from 'mime'
9-
import { withTilde, MAX_ASSET_SIZE, MAX_UPLOAD_CHUNK_SIZE, MAX_UPLOAD_ATTEMPTS, UPLOAD_RETRY_DELAY, CONCURRENT_UPLOADS } from './index.mjs'
9+
import { withTilde, MAX_ASSET_SIZE, MAX_UPLOAD_CHUNK_SIZE, MAX_UPLOAD_ATTEMPTS, UPLOAD_RETRY_DELAY, CONCURRENT_UPLOADS, $api } from './index.mjs'
1010
import prettyBytes from 'pretty-bytes'
1111
import { gzipSize as getGzipSize } from 'gzip-size'
12+
import { consola } from 'consola'
1213

1314
export function hashFile(filePath, data) {
1415
const extension = extname(filePath).substring(1)
@@ -273,3 +274,23 @@ export async function uploadWorkersAssetsToCloudflare(accountId, files, cloudfla
273274
}
274275
return completionToken
275276
}
277+
278+
/**
279+
* Determine the deployment environment based on the branch name for Workers projects
280+
* @param {string} teamSlug - The team slug
281+
* @param {string} projectSlug - The project slug
282+
* @param {string} branch - The git branch name
283+
* @returns {Promise<string>} The determined environment name
284+
*/
285+
export async function determineEnvironment(teamSlug, projectSlug, branch) {
286+
try {
287+
const result = await $api(`/teams/${teamSlug}/projects/${projectSlug}/deploy/environment?branch=${branch}`, {
288+
method: 'GET'
289+
})
290+
return result.name
291+
} catch (error) {
292+
// If API call fails, default to preview
293+
consola.error('Failed to determine environment:', error)
294+
process.exit(1)
295+
}
296+
}

0 commit comments

Comments
 (0)