Skip to content

Commit 79268d1

Browse files
feat: support-pnpm-package-manager [ZEND-6808]
Based on [this PR](#2631) , with minor tweaks.
1 parent 4a3afc0 commit 79268d1

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

packages/contentful--create-contentful-app/src/utils.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { spawn, SpawnOptionsWithoutStdio } from 'child_process';
2-
import { existsSync, rmSync } from 'fs';
2+
import { existsSync, readFileSync, rmSync } from 'fs';
33
import { basename } from 'path';
44
import { choice, highlight, warn } from './logger';
55
import { CLIOptions, ContentfulExample, PackageManager } from './types';
@@ -27,15 +27,23 @@ export function rmIfExists(path: string) {
2727
}
2828

2929
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+
3041
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';
3544
case 'npx-cli.js':
3645
case 'npm-cli.js':
37-
default:
38-
return 'npm';
46+
default: return 'npm';
3947
}
4048
}
4149

@@ -79,12 +87,12 @@ export function normalizeOptions(options: CLIOptions): CLIOptions {
7987
});
8088

8189
// Select active package manager
82-
(normalizedOptions as Record<string, boolean>)[activePackageManager] = true;
90+
(normalizedOptions as CLIOptions)[activePackageManager] = true;
8391
}
8492

8593
// No package manager flags were provided, use active package manager
8694
if (selectedPackageManagers.length === 0) {
87-
(normalizedOptions as Record<string, boolean>)[activePackageManager] = true;
95+
(normalizedOptions as CLIOptions)[activePackageManager] = true;
8896
}
8997

9098
let fallbackOption = '--typescript';

packages/contentful--create-contentful-app/test/utils.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/// <reference types="node" />
2+
// The above directive tells TypeScript to include Node.js type definitions (i.e. process)
13
import { expect } from 'chai';
24
import {
35
detectActivePackageManager,

0 commit comments

Comments
 (0)