hunk.code_window: import exists from 'path-exists'
   import sinon from 'sinon'
   import deepRequireCwd from 'deep-require-cwd'
   import { testDefaults } from '../utils'
   afterEach(async () => {
     // @ts-expect-error
     await global.finishWorkers?.()
   })
   test('successfully install optional dependency with subdependencies', async () => {
     prepareEmpty()
     await addDependenciesToPackage({}, ['fsevents@1.0.14'], await testDefaults({ targetDependenciesField: 'optionalDependencies' }))
   })
  
hunk.labels: keep keep keep keep keep replace replace replace replace replace keep keep keep keep keep
hunk.file_path: pkg-manager/core/test/install/optionalDependencies.ts
hunk.type: replace
file: import { PnpmError } from '@pnpm/error'
  
  export type PinnedVersion = 'major' | 'minor' | 'patch' | 'none'
  
  export const getPrefix = (alias: string, name: string) => alias !== name ? `npm:${name}@` : ''
  
  export function getPref (
    alias: string,
    name: string,
    version: string | undefined,
    opts: {
      pinnedVersion?: PinnedVersion
    }
  ) {
    const prefix = getPrefix(alias, name)
    return `${prefix}${createVersionSpec(version, { pinnedVersion: opts.pinnedVersion })}`
  }
  
  export function createVersionSpec (version: string | undefined, opts: { pinnedVersion?: PinnedVersion, rolling?: boolean }) {
    switch (opts.pinnedVersion ?? 'major') {
    case 'none':
    case 'major':
      if (opts.rolling) return '^'
      return !version ? '*' : `^${version}`
    case 'minor':
      if (opts.rolling) return '~'
      return !version ? '*' : `~${version}`
    case 'patch':
      if (opts.rolling) return '*'
      return !version ? '*' : `${version}`
    default:
      throw new PnpmError('BAD_PINNED_VERSION', `Cannot pin '${opts.pinnedVersion ?? 'undefined'}'`)
    }
  }
  