|
1 | 1 | import { spawn, SpawnOptionsWithoutStdio } from 'child_process'; |
2 | | -import { existsSync, rmSync } from 'fs'; |
| 2 | +import { existsSync, readFileSync, rmSync } from 'fs'; |
3 | 3 | import { basename } from 'path'; |
4 | 4 | import { choice, highlight, warn } from './logger'; |
5 | 5 | import { CLIOptions, ContentfulExample, PackageManager } from './types'; |
@@ -27,15 +27,23 @@ export function rmIfExists(path: string) { |
27 | 27 | } |
28 | 28 |
|
29 | 29 | export function detectActivePackageManager(): PackageManager { |
| 30 | + if (existsSync('pnpm-lock.yaml')) return 'pnpm'; |
| 31 | + if (existsSync('yarn.lock')) return 'yarn'; |
| 32 | + if (existsSync('package-lock.json')) return 'npm'; |
| 33 | + |
| 34 | + try { |
| 35 | + const pkg = JSON.parse(readFileSync('package.json', 'utf8')); |
| 36 | + if (pkg.packageManager?.startsWith('pnpm')) return 'pnpm'; |
| 37 | + if (pkg.packageManager?.startsWith('yarn')) return 'yarn'; |
| 38 | + if (pkg.packageManager?.startsWith('npm')) return 'npm'; |
| 39 | + } catch { } |
| 40 | + |
30 | 41 | switch (basename(process.env.npm_execpath || '')) { |
31 | | - case 'yarn.js': |
32 | | - return 'yarn'; |
33 | | - case 'pnpm.cjs': |
34 | | - return 'pnpm'; |
| 42 | + case 'yarn.js': return 'yarn'; |
| 43 | + case 'pnpm.cjs': return 'pnpm'; |
35 | 44 | case 'npx-cli.js': |
36 | 45 | case 'npm-cli.js': |
37 | | - default: |
38 | | - return 'npm'; |
| 46 | + default: return 'npm'; |
39 | 47 | } |
40 | 48 | } |
41 | 49 |
|
@@ -79,12 +87,12 @@ export function normalizeOptions(options: CLIOptions): CLIOptions { |
79 | 87 | }); |
80 | 88 |
|
81 | 89 | // Select active package manager |
82 | | - (normalizedOptions as Record<string, boolean>)[activePackageManager] = true; |
| 90 | + (normalizedOptions as CLIOptions)[activePackageManager] = true; |
83 | 91 | } |
84 | 92 |
|
85 | 93 | // No package manager flags were provided, use active package manager |
86 | 94 | if (selectedPackageManagers.length === 0) { |
87 | | - (normalizedOptions as Record<string, boolean>)[activePackageManager] = true; |
| 95 | + (normalizedOptions as CLIOptions)[activePackageManager] = true; |
88 | 96 | } |
89 | 97 |
|
90 | 98 | let fallbackOption = '--typescript'; |
|
0 commit comments