|
1 | | -// import { defaultConfigs } from '..'; |
| 1 | +import { existsSync, readFileSync } from 'node:fs'; |
| 2 | +import * as path from 'node:path'; |
2 | 3 | import { getAllDependencies } from './utils.js'; |
| 4 | +import { findMonorepoWorkspaceRoot, getProjectRootPath } from './project.js'; |
| 5 | + |
| 6 | +/** |
| 7 | + * A flavor declared by a framework package in its own package.json: |
| 8 | + * |
| 9 | + * "nativescript": { "vite": { "flavor": "octane", "config": { "import": "octaneConfig", "from": "@nativescript-community/vite-octane" } } } |
| 10 | + * |
| 11 | + * The dependency that carries it identifies the flavor for detection, and |
| 12 | + * `config` tells `nativescript-vite init` which helper to scaffold. |
| 13 | + */ |
| 14 | +export interface DeclaredViteFlavor { |
| 15 | + flavor: string; |
| 16 | + package: string; |
| 17 | + config?: { import: string; from: string }; |
| 18 | +} |
| 19 | + |
| 20 | +function readDeclaredViteFlavor(dependency: string): DeclaredViteFlavor | null { |
| 21 | + const projectRoot = getProjectRootPath(); |
| 22 | + const roots = [projectRoot, findMonorepoWorkspaceRoot(projectRoot)].filter((root): root is string => !!root); |
| 23 | + for (const root of roots) { |
| 24 | + const manifest = path.join(root, 'node_modules', dependency, 'package.json'); |
| 25 | + if (!existsSync(manifest)) continue; |
| 26 | + try { |
| 27 | + const vite = JSON.parse(readFileSync(manifest, 'utf8'))?.nativescript?.vite; |
| 28 | + if (vite && typeof vite.flavor === 'string' && vite.flavor) { |
| 29 | + const config = vite.config && typeof vite.config.import === 'string' && typeof vite.config.from === 'string' ? { import: vite.config.import, from: vite.config.from } : undefined; |
| 30 | + return { flavor: vite.flavor, package: dependency, config }; |
| 31 | + } |
| 32 | + } catch {} |
| 33 | + return null; |
| 34 | + } |
| 35 | + return null; |
| 36 | +} |
| 37 | + |
| 38 | +/** The first installed dependency that declares a Vite flavor, if any. */ |
| 39 | +export function findDeclaredViteFlavor(): DeclaredViteFlavor | null { |
| 40 | + for (const dependency of getAllDependencies()) { |
| 41 | + const declared = readDeclaredViteFlavor(dependency); |
| 42 | + if (declared) return declared; |
| 43 | + } |
| 44 | + return null; |
| 45 | +} |
3 | 46 |
|
4 | 47 | let targetFlavor: string; |
5 | 48 |
|
@@ -59,6 +102,11 @@ export function determineProjectFlavor(): string | false { |
59 | 102 | return 'svelte'; |
60 | 103 | } |
61 | 104 |
|
| 105 | + const declared = findDeclaredViteFlavor(); |
| 106 | + if (declared) { |
| 107 | + return declared.flavor; |
| 108 | + } |
| 109 | + |
62 | 110 | // the order is important - angular, react, and svelte also include these deps |
63 | 111 | // but should return prior to this condition! |
64 | 112 | if (dependencies.includes('@nativescript/core') && dependencies.includes('typescript')) { |
|
0 commit comments