@@ -3,7 +3,6 @@ import os from 'node:os'
33
44let cachedPythonVersion = undefined
55
6- /** @returns {string|null } the major.minor Python version, or null if python3 is unavailable */
76function getPythonVersion ( ) {
87 if ( cachedPythonVersion !== undefined ) { return cachedPythonVersion }
98 try {
@@ -16,7 +15,7 @@ function getPythonVersion() {
1615 return cachedPythonVersion
1716}
1817
19- /** @returns {Record<string, string> } PEP 508 marker variables derived from the current Node.js/OS environment */
18+ /** @returns {Record<string, string> } */
2019export function getEnvironmentMarkers ( ) {
2120 let platform = process . platform
2221 let systemMap = { win32 : 'Windows' , linux : 'Linux' , darwin : 'Darwin' }
@@ -35,11 +34,6 @@ export function getEnvironmentMarkers() {
3534 }
3635}
3736
38- /**
39- * @param {string } left
40- * @param {string } right
41- * @returns {number } negative if left < right, 0 if equal, positive if left > right
42- */
4337function compareVersions ( left , right ) {
4438 let lParts = left . split ( '.' ) . map ( Number )
4539 let rParts = right . split ( '.' ) . map ( Number )
@@ -53,13 +47,6 @@ function compareVersions(left, right) {
5347 return 0
5448}
5549
56- /**
57- * @param {string } variable - PEP 508 marker variable name (e.g. sys_platform, python_version)
58- * @param {string } op - comparison operator (==, !=, >=, <=, >, <, ~=, in, not in)
59- * @param {string } value - the literal value to compare against
60- * @param {Record<string, string> } env - current environment marker values
61- * @returns {boolean }
62- */
6350function evaluateComparison ( variable , op , value , env ) {
6451 let envVal = env [ variable ]
6552 if ( envVal === undefined || envVal === '' ) {
@@ -95,10 +82,6 @@ function evaluateComparison(variable, op, value, env) {
9582 }
9683}
9784
98- /**
99- * @param {string } expr - a single marker comparison (e.g. "sys_platform == 'linux'" or "'linux' == sys_platform")
100- * @returns {{variable: string, op: string, value: string}|null } parsed atom, or null if not a valid comparison
101- */
10285function parseAtom ( expr ) {
10386 let m = expr . match ( / ^ \s * ( [ \w . ] + ) \s * ( ~ = | ! = | = = | > = | < = | > | < | n o t \s + i n | i n ) \s * [ " ' ] ( [ ^ " ' ] * ) [ " ' ] \s * $ / )
10487 if ( m ) { return { variable : m [ 1 ] , op : m [ 2 ] . replace ( / \s + / g, ' ' ) , value : m [ 3 ] } }
@@ -110,22 +93,15 @@ function parseAtom(expr) {
11093}
11194
11295/**
113- * Evaluates a PEP 508 environment marker expression against the current platform.
114- * @param {string } markerExpr - marker expression (e.g. "sys_platform == 'win32' and python_version >= '3.8'")
115- * @returns {boolean } true if the marker matches (or is empty), false otherwise
96+ * @param {string } markerExpr
97+ * @returns {boolean }
11698 */
11799export function evaluateMarker ( markerExpr ) {
118100 if ( ! markerExpr || ! markerExpr . trim ( ) ) { return true }
119101 let env = getEnvironmentMarkers ( )
120102 return evaluateExpr ( markerExpr . trim ( ) , env )
121103}
122104
123- /**
124- * Recursively evaluates a marker expression supporting and/or operators and parenthesized groups.
125- * @param {string } expr
126- * @param {Record<string, string> } env
127- * @returns {boolean }
128- */
129105function evaluateExpr ( expr , env ) {
130106 let orParts = splitLogical ( expr , ' or ' )
131107 if ( orParts . length > 1 ) {
@@ -148,12 +124,6 @@ function evaluateExpr(expr, env) {
148124 return evaluateComparison ( atom . variable , atom . op , atom . value , env )
149125}
150126
151- /**
152- * Splits a marker expression by a logical separator (and/or) respecting parentheses and quoted strings.
153- * @param {string } expr
154- * @param {string } sep - separator including spaces (e.g. " and ", " or ")
155- * @returns {string[] }
156- */
157127function splitLogical ( expr , sep ) {
158128 let parts = [ ]
159129 let depth = 0
0 commit comments