All files / src/utils package-manager.ts

100% Statements 52/52
100% Branches 10/10
100% Functions 2/2
100% Lines 52/52

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93                                                        1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x         137x 137x   137x 137x 137x     98x 98x           1x 137x 137x 137x 137x 137x 137x  
/**
 * Package manager detection and command generation utilities
 */
 
export type PackageManager = 'npm' | 'yarn' | 'pnpm' | 'bun';
 
export type CommandType =
    | 'install'
    | 'dev'
    | 'build'
    | 'start'
    | 'create'
    | 'build:type'
    | 'lint:type';
 
interface PackageManagerCommands {
    install: string;
    dev: string;
    build: string;
    start: string;
    create: string;
    'build:type': string;
    'lint:type': string;
}
 
/**
 * Configuration for different package managers and their commands
 */
const PACKAGE_MANAGER_CONFIG: Record<PackageManager, PackageManagerCommands> = {
    npm: {
        install: 'npm install',
        dev: 'npm run dev',
        build: 'npm run build',
        start: 'npm start',
        create: 'npm create esmx@latest',
        'build:type': 'npm run build:type',
        'lint:type': 'npm run lint:type'
    },
    yarn: {
        install: 'yarn install',
        dev: 'yarn dev',
        build: 'yarn build',
        start: 'yarn start',
        create: 'yarn create esmx',
        'build:type': 'yarn build:type',
        'lint:type': 'yarn lint:type'
    },
    pnpm: {
        install: 'pnpm install',
        dev: 'pnpm dev',
        build: 'pnpm build',
        start: 'pnpm start',
        create: 'pnpm create esmx',
        'build:type': 'pnpm build:type',
        'lint:type': 'pnpm lint:type'
    },
    bun: {
        install: 'bun install',
        dev: 'bun dev',
        build: 'bun run build',
        start: 'bun start',
        create: 'bun create esmx',
        'build:type': 'bun run build:type',
        'lint:type': 'bun run lint:type'
    }
};
 
/**
 * Detect the package manager being used based on user agent
 */
function detectPackageManager(userAgent?: string): PackageManager {
    const agent = userAgent || process.env.npm_config_user_agent || '';
 
    if (agent.includes('pnpm')) return 'pnpm';
    if (agent.includes('yarn')) return 'yarn';
    if (agent.includes('bun')) return 'bun';
 
    // Default to npm
    return 'npm';
}
 
/**
 * Get a specific command for the detected package manager
 * Business logic only needs to specify what command to execute
 */
export function getCommand(
    commandType: CommandType,
    userAgent?: string
): string {
    const packageManager = detectPackageManager(userAgent);
    return PACKAGE_MANAGER_CONFIG[packageManager][commandType];
}