"Fossies" - the Fresh Open Source Software Archive

Member "cli-1.1280.1/packages/snyk-fix/src/plugins/python/handlers/pipenv-pipfile/update-dependencies/pipenv-add.ts" (20 Feb 2024, 2620 Bytes) of package /linux/misc/snyk-cli-1.1280.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) TypeScript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 import * as pathLib from 'path';
    2 import * as pipenvPipfileFix from '@snyk/fix-pipenv-pipfile';
    3 import * as debugLib from 'debug';
    4 
    5 import {
    6   EntityToFix,
    7   FixChangesSummary,
    8   FixOptions,
    9 } from '../../../../../types';
   10 import { validateRequiredData } from '../../validate-required-data';
   11 
   12 import {
   13   generateFailedChanges,
   14   generateSuccessfulChanges,
   15 } from '../../attempted-changes-summary';
   16 import { CommandFailedError } from '../../../../../lib/errors/command-failed-to-run-error';
   17 import { NoFixesCouldBeAppliedError } from '../../../../../lib/errors/no-fixes-applied';
   18 
   19 const debug = debugLib('snyk-fix:python:pipenvAdd');
   20 
   21 export async function pipenvAdd(
   22   entity: EntityToFix,
   23   options: FixOptions,
   24   upgrades: string[],
   25 ): Promise<FixChangesSummary[]> {
   26   const changes: FixChangesSummary[] = [];
   27   let pipenvCommand;
   28   const { remediation, targetFile } = validateRequiredData(entity);
   29   try {
   30     const targetFilePath = pathLib.resolve(entity.workspace.path, targetFile);
   31     const { dir } = pathLib.parse(targetFilePath);
   32     if (!options.dryRun && upgrades.length) {
   33       const {
   34         stderr,
   35         stdout,
   36         command,
   37         exitCode,
   38       } = await pipenvPipfileFix.pipenvInstall(dir, upgrades, {
   39         python: entity.options.command,
   40       });
   41       debug('`pipenv add` returned:', { stderr, stdout, command });
   42       if (exitCode !== 0) {
   43         pipenvCommand = command;
   44         throwPipenvError(stderr, stdout, command);
   45       }
   46     }
   47     changes.push(...generateSuccessfulChanges(upgrades, remediation.pin));
   48   } catch (error) {
   49     changes.push(
   50       ...generateFailedChanges(upgrades, remediation.pin, error, pipenvCommand),
   51     );
   52   }
   53   return changes;
   54 }
   55 
   56 function throwPipenvError(stderr: string, stdout: string, command?: string) {
   57   const incompatibleDeps =
   58     'There are incompatible versions in the resolved dependencies';
   59   const lockingFailed = 'Locking failed';
   60   const versionNotFound = 'Could not find a version that matches';
   61 
   62   const errorsToBubbleUp = [incompatibleDeps, lockingFailed, versionNotFound];
   63 
   64   for (const error of errorsToBubbleUp) {
   65     if (
   66       stderr.toLowerCase().includes(error.toLowerCase()) ||
   67       stdout.toLowerCase().includes(error.toLowerCase())
   68     ) {
   69       throw new CommandFailedError(error, command);
   70     }
   71   }
   72 
   73   const SOLVER_PROBLEM = /SolverProblemError(.* version solving failed)/gms;
   74   const solverProblemError =
   75     SOLVER_PROBLEM.exec(stderr) || SOLVER_PROBLEM.exec(stdout);
   76   if (solverProblemError) {
   77     throw new CommandFailedError(solverProblemError[0].trim(), command);
   78   }
   79 
   80   throw new NoFixesCouldBeAppliedError();
   81 }