Skip to content

feat(init): add --nightly flag #650

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
45 changes: 45 additions & 0 deletions packages/nuxi/src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { downloadTemplate, startShell } from 'giget'
import { installDependencies } from 'nypm'
import { $fetch } from 'ofetch'
import { join, relative, resolve } from 'pathe'
import { readPackageJSON, writePackageJSON } from 'pkg-types'
import { hasTTY } from 'std-env'

import { x } from 'tinyexec'
Expand Down Expand Up @@ -87,6 +88,10 @@ export default defineCommand({
description: 'Nuxt modules to install (comma separated without spaces)',
alias: 'M',
},
nightly: {
type: 'string',
description: 'Use Nuxt nightly release channel (3x or latest)',
},
},
async run(ctx) {
if (hasTTY) {
Expand Down Expand Up @@ -169,6 +174,46 @@ export default defineCommand({
process.exit(1)
}

if (ctx.args.nightly !== undefined && !ctx.args.offline && !ctx.args.preferOffline) {
const response = await $fetch<{
'dist-tags': {
[key: string]: string
}
}>('https://registry.npmjs.org/nuxt-nightly')

const nightlyChannelTag = ctx.args.nightly
|| Object.keys(response['dist-tags'])
.filter(key => /^\d+x$/.test(key))
.sort()
.reverse()[0]

if (!nightlyChannelTag) {
logger.error(`Error getting nightly channel tag.`)
process.exit(1)
}

const nightlyChannelVersion = response['dist-tags'][nightlyChannelTag]

if (!nightlyChannelVersion) {
logger.error(`Nightly channel version for tag '${nightlyChannelTag}' not found.`)
process.exit(1)
}

const nightlyNuxtPackageJsonVersion = `npm:nuxt-nightly@${nightlyChannelVersion}`
const packageJsonPath = resolve(cwd, ctx.args.dir)

const packageJson = await readPackageJSON(packageJsonPath)

if (packageJson.dependencies && 'nuxt' in packageJson.dependencies) {
packageJson.dependencies.nuxt = nightlyNuxtPackageJsonVersion
}
else if (packageJson.devDependencies && 'nuxt' in packageJson.devDependencies) {
packageJson.devDependencies.nuxt = nightlyNuxtPackageJsonVersion
}

await writePackageJSON(join(packageJsonPath, 'package.json'), packageJson)
}

function detectCurrentPackageManager() {
const userAgent = process.env.npm_config_user_agent
if (!userAgent) {
Expand Down