"Fossies" - the Fresh Open Source Software Archive

Member "cli-1.1260.0/src/lib/protect-update-notification.ts" (4 Dec 2023, 3532 Bytes) of package /linux/misc/snyk-cli-1.1260.0.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 { EOL } from 'os';
    2 import * as theme from './theme';
    3 import * as fs from 'fs';
    4 import * as path from 'path';
    5 import * as createDebug from 'debug';
    6 
    7 const debug = createDebug('snyk-protect-update-notification');
    8 
    9 export function getProtectUpgradeWarningForPaths(
   10   packageJsonPaths: string[],
   11 ): string {
   12   try {
   13     if (packageJsonPaths?.length > 0) {
   14       let message = theme.color.status.warn(
   15         `${theme.icon.WARNING} WARNING: It looks like you have the \`snyk\` dependency in the \`package.json\` file(s) at the following path(s):` +
   16           EOL,
   17       );
   18 
   19       packageJsonPaths.forEach((p) => {
   20         message += theme.color.status.warn(`  - ${p}` + EOL);
   21       });
   22 
   23       const githubReadmeUrlShort = 'https://snyk.co/ud1cR'; // https://github.com/snyk/snyk/tree/master/packages/snyk-protect#migrating-from-snyk-protect-to-snykprotect
   24 
   25       message += theme.color.status.warn(
   26         `For more information and migration instructions, see ${githubReadmeUrlShort}` +
   27           EOL,
   28       );
   29 
   30       return message;
   31     } else {
   32       return '';
   33     }
   34   } catch (e) {
   35     debug('Error in getProtectUpgradeWarningForPaths()', e);
   36     return '';
   37   }
   38 }
   39 
   40 export function packageJsonFileExistsInDirectory(
   41   directoryPath: string,
   42 ): boolean {
   43   try {
   44     const packageJsonPath = path.resolve(directoryPath, 'package.json');
   45     const fileExists = fs.existsSync(packageJsonPath);
   46     return fileExists;
   47   } catch (e) {
   48     debug('Error in packageJsonFileExistsInDirectory()', e);
   49     return false;
   50   }
   51 }
   52 
   53 export function checkPackageJsonForSnykDependency(
   54   packageJsonPath: string,
   55 ): boolean {
   56   try {
   57     const fileExists = fs.existsSync(packageJsonPath);
   58     if (fileExists) {
   59       const packageJson = fs.readFileSync(packageJsonPath, 'utf8');
   60       const packageJsonObject = JSON.parse(packageJson);
   61       const snykDependency = packageJsonObject.dependencies['snyk'];
   62       if (snykDependency) {
   63         return true;
   64       }
   65     }
   66   } catch (e) {
   67     debug('Error in checkPackageJsonForSnykDependency()', e);
   68   }
   69   return false;
   70 }
   71 
   72 export function getPackageJsonPathsContainingSnykDependency(
   73   fileOption: string | undefined,
   74   paths: string[],
   75 ): string[] {
   76   const packageJsonPathsWithSnykDepForProtect: string[] = [];
   77 
   78   try {
   79     if (fileOption) {
   80       if (
   81         fileOption.endsWith('package.json') ||
   82         fileOption.endsWith('package-lock.json')
   83       ) {
   84         const directoryWithPackageJson = path.dirname(fileOption);
   85         if (packageJsonFileExistsInDirectory(directoryWithPackageJson)) {
   86           const packageJsonPath = path.resolve(
   87             directoryWithPackageJson,
   88             'package.json',
   89           );
   90           const packageJsonContainsSnykDep = checkPackageJsonForSnykDependency(
   91             packageJsonPath,
   92           );
   93           if (packageJsonContainsSnykDep) {
   94             packageJsonPathsWithSnykDepForProtect.push(packageJsonPath);
   95           }
   96         }
   97       }
   98     } else {
   99       paths.forEach((testPath) => {
  100         if (packageJsonFileExistsInDirectory(testPath)) {
  101           const packageJsonPath = path.resolve(testPath, 'package.json');
  102           const packageJsonContainsSnykDep = checkPackageJsonForSnykDependency(
  103             packageJsonPath,
  104           );
  105           if (packageJsonContainsSnykDep) {
  106             packageJsonPathsWithSnykDepForProtect.push(packageJsonPath);
  107           }
  108         }
  109       });
  110     }
  111   } catch (e) {
  112     debug('Error in getPackageJsonPathsContainingSnykDependency()', e);
  113   }
  114 
  115   return packageJsonPathsWithSnykDepForProtect;
  116 }