All files / lib/parsers requirements-parser.js

93.75% Statements 15/16
77.77% Branches 7/9
100% Functions 1/1
93.75% Lines 15/16

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                              3x 3x     3x 3x         3x     3x   3x   13x 13x 2x       11x 11x 11x 11x       3x  
/**
 * Python Requirements Parser
 *
 * Parses requirements.txt files to extract Python dependencies
 */
 
import fs from 'fs-extra';
import path from 'path';
 
/**
 * Parse requirements.txt file and return dependencies object
 * @param {string} cwd - Current working directory
 * @returns {Promise<Object>} Dependencies object with package names as keys
 */
export async function parseRequirements(cwd) {
  const requirementsPath = path.join(cwd, 'requirements.txt');
  const deps = {};
 
  // Check if file exists
  const exists = await fs.pathExists(requirementsPath);
  Iif (!exists) {
    return deps;
  }
 
  // Read file content
  const content = await fs.readFile(requirementsPath, 'utf8');
 
  // Parse each line
  const lines = content.split('\n');
 
  for (const line of lines) {
    // Skip comments and empty lines
    const trimmed = line.trim();
    if (!trimmed || trimmed.startsWith('#') || trimmed.startsWith('-')) {
      continue;
    }
 
    // Parse package name (strip version specs and extras)
    const packageMatch = trimmed.match(/^([a-zA-Z0-9_-]+)/);
    Eif (packageMatch) {
      const packageName = packageMatch[1].toLowerCase();
      deps[packageName] = true;
    }
  }
 
  return deps;
}