Skip to content
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

fix(cli): use @nuxt/kit to load nuxt config #205

Merged
merged 3 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
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
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
"dotenv": "^16.4.7",
"git-url-parse": "^16.0.0",
"is-docker": "^3.0.0",
"jiti": "^2.4.2",
"ofetch": "^1.4.1",
"package-manager-detector": "^0.2.8",
"parse-git-config": "^3.0.0",
Expand Down
3 changes: 0 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 10 additions & 13 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { destr } from 'destr'
import * as rc from 'rc9'
import { colors as c } from 'consola/utils'
import { consola } from 'consola'
import { createJiti } from 'jiti'
import { loadNuxtConfig } from '@nuxt/kit'
import { isTest } from 'std-env'
import { parse as parseDotenv } from 'dotenv'
import { createMain, defineCommand } from 'citty'
Expand Down Expand Up @@ -115,13 +115,14 @@ async function _checkDisabled(dir: string): Promise<string | false | undefined>
}
}

const disabledByConf = (conf: any) => conf.telemetry === false
|| (conf.telemetry && conf.telemetry.enabled === false)
const disabledByConf = (conf: any) => conf.telemetry === false || (conf.telemetry && conf.telemetry.enabled === false)

try {
const configPath = resolveNuxtConfigPath(dir)
if (configPath && disabledByConf(createJiti(dir).import(configPath, { default: true }))) {
return 'by ' + configPath
const config = await loadNuxtConfig({ cwd: dir })
for (const layer of config._layers) {
if (disabledByConf(layer.config)) {
return 'by ' + config._layers[0].configFile
}
}
}
catch {
Expand Down Expand Up @@ -157,17 +158,13 @@ function setRC(dir: string, key: any, val: any, global: boolean) {
}
}

function resolveNuxtConfigPath(dir: string) {
const jiti = createJiti(dir)
return jiti.esmResolve('./nuxt.config', { try: true }) || jiti.esmResolve('./.config/nuxt', { try: true })
}

function ensureNuxtProject(args: { global: boolean, dir: string }) {
async function ensureNuxtProject(args: { global: boolean, dir: string }) {
if (args.global) {
return
}
const dir = resolve(args.dir)
if (!resolveNuxtConfigPath(dir)) {
const nuxtConfig = await loadNuxtConfig({ cwd: dir })
if (!nuxtConfig || !nuxtConfig._layers[0]?.configFile) {
consola.error('You are not in a Nuxt project.')
consola.info('You can try specifying a directory or by using the `--global` flag to configure telemetry for your machine.')
process.exit()
Expand Down
Loading