Repository: nuxt/nuxt
File: packages/nuxt/src/core/nuxt.ts (TypeScript)

Code before:
  'nuxt': 'nuxt-nightly',
  '@nuxt/schema': '@nuxt/schema-nightly',
  '@nuxt/kit': '@nuxt/kit-nightly',
}

export const keyDependencies = [
  '@nuxt/kit',
]

let warnedAboutCompatDate = false

async function initNuxt (nuxt: Nuxt) {
  const layerConfigs = nuxt.options._layers.map(layer => layer.config)
  const reversedConfigs = layerConfigs.slice().reverse()

  // Register user hooks
  for (const config of reversedConfigs) {
    if (config.hooks) {
      nuxt.hooks.addHooks(config.hooks)
    }
  }

  // Correct css order
  nuxt.options.css = reversedConfigs
    .flatMap(config => config.css || [])
    .filter((css): css is string => typeof css === 'string')

  // Prompt to set compatibility date
  nuxt.options.compatibilityDate = resolveCompatibilityDatesFromEnv(nuxt.options.compatibilityDate)

  if (!nuxt.options.compatibilityDate.default) {
    const todaysDate = formatDate(new Date())
    nuxt.options.compatibilityDate.default = fallbackCompatibilityDate

    const shouldShowPrompt = nuxt.options.dev && hasTTY && !isCI
    if (!shouldShowPrompt) {
      logger.info(`Using \`${fallbackCompatibilityDate}\` as fallback compatibility date.`)
    }

    async function promptAndUpdate () {
      const result = await consola.prompt(`Do you want to update your ${colorize('cyan', 'nuxt.config')} to set ${colorize('cyan', `compatibilityDate: '${todaysDate}'`)}?`, {
        type: 'confirm',
        default: true,
      })
      if (result !== true) {
        logger.info(`Using \`${fallbackCompatibilityDate}\` as fallback compatibility date.`)
        return
      }

      try {
        const res = await updateConfig({

Review comment (refactor):
Maybe we could also de-duplicate?

And we could add a test similar to `packages/kit/test/load-nuxt-config.spec.ts` (except in `packages/nuxt/test/`).

Code after:
  'nuxt': 'nuxt-nightly',
  '@nuxt/schema': '@nuxt/schema-nightly',
  '@nuxt/kit': '@nuxt/kit-nightly',
}

export const keyDependencies = [
  '@nuxt/kit',
]

let warnedAboutCompatDate = false

async function initNuxt (nuxt: Nuxt) {
  // Register user hooks
  for (const config of nuxt.options._layers.map(layer => layer.config).reverse()) {
    if (config.hooks) {
      nuxt.hooks.addHooks(config.hooks)
    }
  }

  // Prompt to set compatibility date
  nuxt.options.compatibilityDate = resolveCompatibilityDatesFromEnv(nuxt.options.compatibilityDate)

  if (!nuxt.options.compatibilityDate.default) {
    const todaysDate = formatDate(new Date())
    nuxt.options.compatibilityDate.default = fallbackCompatibilityDate

    const shouldShowPrompt = nuxt.options.dev && hasTTY && !isCI
    if (!shouldShowPrompt) {
      logger.info(`Using \`${fallbackCompatibilityDate}\` as fallback compatibility date.`)
    }

    async function promptAndUpdate () {
      const result = await consola.prompt(`Do you want to update your ${colorize('cyan', 'nuxt.config')} to set ${colorize('cyan', `compatibilityDate: '${todaysDate}'`)}?`, {
        type: 'confirm',
        default: true,
      })
      if (result !== true) {
        logger.info(`Using \`${fallbackCompatibilityDate}\` as fallback compatibility date.`)
        return
      }

      try {
        const res = await updateConfig({
          configFile: 'nuxt.config',
          cwd: nuxt.options.rootDir,
          async onCreate ({ configFile }) {
            const shallCreate = await consola.prompt(`Do you want to create ${colorize('cyan', relative(nuxt.options.rootDir, configFile))}?`, {
              type: 'confirm',
              default: true,
            })
            if (shallCreate !== true) {