Skip to content

Commit

Permalink
fix(cli): use @nuxt/kit to load nuxt config (#205)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe authored Jan 6, 2025
1 parent d34b669 commit 0914f26
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 17 deletions.
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

0 comments on commit 0914f26

Please sign in to comment.