|
1 | 1 | import { existsSync, readFileSync } from 'node:fs'; |
2 | | -import { resolve } from 'node:path'; |
| 2 | +import { dirname, resolve } from 'node:path'; |
3 | 3 |
|
4 | 4 | const { toString } = Object.prototype; |
5 | 5 |
|
@@ -57,26 +57,54 @@ export function isLibMode(config: { build?: { lib?: any } }): boolean { |
57 | 57 | return !!config.build?.lib; |
58 | 58 | } |
59 | 59 |
|
60 | | -export function isNuxtProject(config: { root?: string }): boolean { |
61 | | - const root = config.root || process.cwd(); |
62 | | - const packageJsonPath = resolve(root, 'package.json'); |
63 | | - |
64 | | - if (existsSync(packageJsonPath)) { |
65 | | - try { |
66 | | - const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf-8')); |
67 | | - const dependencies = { ...packageJson.dependencies, ...packageJson.devDependencies }; |
68 | | - if (dependencies.nuxt) return true; |
69 | | - } catch { |
70 | | - /* empty */ |
71 | | - } |
| 60 | +function hasNuxtDependency(packageJsonPath: string): boolean { |
| 61 | + if (!existsSync(packageJsonPath)) return false; |
| 62 | + try { |
| 63 | + const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf-8')); |
| 64 | + const dependencies = { ...packageJson.dependencies, ...packageJson.devDependencies }; |
| 65 | + return !!dependencies?.nuxt; |
| 66 | + } catch { |
| 67 | + return false; |
72 | 68 | } |
| 69 | +} |
73 | 70 |
|
74 | | - const nuxtPaths = [ |
| 71 | +function hasNuxtConfigFile(root: string): boolean { |
| 72 | + return [ |
75 | 73 | resolve(root, 'nuxt.config.js'), |
76 | 74 | resolve(root, 'nuxt.config.ts'), |
77 | | - resolve(root, '.nuxt'), |
78 | | - resolve(root, '.output'), |
79 | | - ]; |
| 75 | + resolve(root, 'nuxt.config.mjs'), |
| 76 | + resolve(root, 'nuxt.config.cjs'), |
| 77 | + ].some(p => existsSync(p)); |
| 78 | +} |
| 79 | + |
| 80 | +function* walkUpDirs(startDir: string): Generator<string> { |
| 81 | + let current = startDir; |
| 82 | + while (true) { |
| 83 | + yield current; |
| 84 | + const parent = dirname(current); |
| 85 | + if (parent === current) break; |
| 86 | + current = parent; |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +function hasNuxtPlugins(config: any): boolean { |
| 91 | + const plugins = config?.plugins; |
| 92 | + if (!Array.isArray(plugins)) return false; |
| 93 | + return plugins.some(p => typeof p?.name === 'string' && (p.name === 'nuxt' || p.name.startsWith('nuxt:'))); |
| 94 | +} |
| 95 | + |
| 96 | +export function isNuxtProject(config: { root?: string; plugins?: any } = {}): boolean { |
| 97 | + if (hasNuxtPlugins(config)) return true; |
80 | 98 |
|
81 | | - return nuxtPaths.some(path => existsSync(path)); |
| 99 | + const startDir = config.root || process.cwd(); |
| 100 | + |
| 101 | + for (const dir of walkUpDirs(startDir)) { |
| 102 | + if (hasNuxtConfigFile(dir)) return true; |
| 103 | + const packageJsonPath = resolve(dir, 'package.json'); |
| 104 | + if (existsSync(packageJsonPath)) { |
| 105 | + return hasNuxtDependency(packageJsonPath); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + return false; |
82 | 110 | } |
0 commit comments