Skip to content

parseDependencies

Parses the value of a package dependency field (Depends, Requires, depends, …) into a structure: groups of alternatives, each alternative a name with an optional version constraint. format says how the text is written, and decides the exact shape of each dependency and the operator spellings (see DependencyFormats); the options are those of that format only.

'debian' — a relationship field of the Debian Policy Manual, chapter 7.1 (Depends, Pre-Depends, Recommends, Suggests, Enhances, Breaks, Conflicts, Provides, Replaces, Build-Depends, …). Commas separate the entries that must all hold (AND), | separates the alternatives inside an entry (OR). Each alternative is name[:arch] [(op version)] [[archs]] [<profiles>], in that order, with any whitespace (line breaks included, as in a multi-line Depends) between the parts. The operators are Debian’s <<, <=, =, >= and >>, kept as written; the obsolete < and > throw unless options.compat is set, in which case they are read as <= and >= like dpkg does. Whitespace-only entries (a trailing comma, a,, b) are skipped, as dpkg does. The ${substvars} of source debian/control files are not supported.

'rpm' — an RPM Requires value: capabilities separated by commas and/or whitespace, each name [op version] with <, <=, =, >= or >; boolean dependencies (a or b) give the alternatives of a group.

'arch' — an Arch Linux depends array or .PKGINFO list: entries separated by whitespace (quotes and surrounding parentheses of a PKGBUILD array are accepted), each name[op version].

'alpine' — an Alpine depends list: entries separated by whitespace, each [!]name[op version] (! is a conflict).

'pep508' — PEP 508 requirements, one per line (as in requirements.txt or the dependencies of a pyproject.toml): name[extras] specifiers ; marker, or name @ url.

Every entry that is not valid throws a SyntaxError quoting the offending text. Parsing is linear in the input.

Available since v3.1.10

import { parseDependencies } from '@helpers4/package';
// or, from the all-in-one package (same code, one install):
import { parseDependencies } from 'helpers4/package';
parseDependencies<Format extends keyof DependencyFormats>(field: string, format: Format, options: ParseDependenciesOptions<Format>): DependencyGroup<DependencyOf<Format>>[]
ParameterTypeDescription
fieldstringThe value of a dependency field
formatFormatHow the field is written
optionsParseDependenciesOptions<Format>The options of `format`, if it has any

DependencyGroup<DependencyOf<Format>>[] — One group of alternatives per entry

Commas are AND, | is OR inside an entry; each alternative carries its qualifier, version and restrictions.

parseDependencies('libc6 (>= 2.34), python3:any | python3 (>= 3.8) [amd64], foo <!nocheck>', 'debian')
// => [
//   [{ name: 'libc6', version: { operator: '>=', version: '2.34' } }],
//   [
//     { name: 'python3', arch: 'any' },
//     { name: 'python3', version: { operator: '>=', version: '3.8' }, architectures: ['amd64'] },
//   ],
//   [{ name: 'foo', profiles: [['!nocheck']] }],
// ]

Read a multi-line field and list the package names

Section titled “Read a multi-line field and list the package names”

Line breaks are plain whitespace, and a trailing comma is tolerated.

parseDependencies('debhelper-compat (= 13),\n dh-python,\n pkg-config,\n', 'debian').map((group) => group[0].name)
// => ['debhelper-compat', 'dh-python', 'pkg-config']

A fragment that is not valid throws a SyntaxError that quotes it.

parseDependencies('libc6 (>= 2.34', 'debian')
// => throws SyntaxError: parseDependencies: unexpected "(>= 2.34" in "libc6 (>= 2.34" ...

They are refused by default; compat mode reads them as dpkg does, <= and >=.

parseDependencies('libfoo (< 2)', 'debian', { compat: true })
// => [[{ name: 'libfoo', version: { operator: '<=', version: '2' } }]]

Boolean dependencies (a or b) become the alternatives of a group.

parseDependencies('glibc >= 2.28, (nginx or httpd)', 'rpm')
// => [[{ name: 'glibc', version: { operator: '>=', version: '2.28' } }], [{ name: 'nginx' }, { name: 'httpd' }]]

Quotes and the surrounding parentheses of the array are accepted.

parseDependencies("('glibc>=2.38' bash)", 'arch')
// => [[{ name: 'glibc', version: { operator: '>=', version: '2.38' } }], [{ name: 'bash' }]]

Read an Alpine depends list, with a conflict

Section titled “Read an Alpine depends list, with a conflict”

A leading ! marks a package that must not be installed.

parseDependencies('musl>=1.2 !openrc', 'alpine')
// => [[{ name: 'musl', version: { operator: '>=', version: '1.2' } }], [{ name: 'openrc', conflict: true }]]

Extras, specifiers and the environment marker are separated; the marker is kept as written.

parseDependencies('requests[security]>=2.8,<3 ; python_version < "3.12"', 'pep508')
// => [[{ name: 'requests', extras: ['security'], specifiers: [{ operator: '>=', version: '2.8' }, { operator: '<', version: '3' }], marker: 'python_version < "3.12"' }]]

For each supported format, the shape of one dependency and the options its parser takes. This is what makes parseDependencies and stringifyDependencies type-safe per format.

interface DependencyFormats {
  alpine: object;
  arch: object;
  debian: object;
  pep508: object;
  rpm: object;
}

The alternatives (a | b) that satisfy one entry of a dependency field. A format without alternatives ('arch', 'alpine', 'pep508') always has a single one.

type DependencyGroup<Dependency> = Dependency[]

The dependency type of a format.

type DependencyOf<Format extends DependencyFormat> = DependencyFormats[Format]['dependency']

The extra arguments of parseDependencies: the options of the format if it has some, none otherwise.

type ParseDependenciesOptions<Format extends DependencyFormat> = DependencyFormats[Format]['options'] extends undefined
  ? []
  : [options?: DependencyFormats[Format]['options']]

The formats of dependency fields: 'debian', 'rpm', 'arch', 'alpine' and 'pep508'.

type DependencyFormat = keyof DependencyFormats

View source on GitHub