"Fossies" - the Fresh Open Source Software Archive

Member "cli-1.1280.1/src/lib/plugins/nodejs-plugin/index.ts" (20 Feb 2024, 2118 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 modulesParser from './npm-modules-parser';
    2 import * as lockParser from './npm-lock-parser';
    3 import * as types from '../types';
    4 import * as analytics from '../../analytics';
    5 import { MissingTargetFileError } from '../../errors/missing-targetfile-error';
    6 import { MultiProjectResult } from '@snyk/cli-interface/legacy/plugin';
    7 import { DepGraph } from '@snyk/dep-graph';
    8 import {
    9   PkgTree,
   10   getLockfileVersionFromFile,
   11   NodeLockfileVersion,
   12 } from 'snyk-nodejs-lockfile-parser';
   13 
   14 import * as path from 'path';
   15 
   16 export async function inspect(
   17   root: string,
   18   targetFile: string,
   19   options: types.Options = {},
   20 ): Promise<MultiProjectResult> {
   21   if (!targetFile) {
   22     throw MissingTargetFileError(root);
   23   }
   24   const isLockFileBased =
   25     targetFile.endsWith('package-lock.json') ||
   26     targetFile.endsWith('yarn.lock');
   27 
   28   const getLockFileDeps = isLockFileBased && !options.traverseNodeModules;
   29   const depRes: PkgTree | DepGraph = getLockFileDeps
   30     ? await lockParser.parse(root, targetFile, options)
   31     : await modulesParser.parse(root, targetFile, options);
   32 
   33   let scannedProjects: any[] = [];
   34   if (isResDepGraph(depRes)) {
   35     scannedProjects = [{ depGraph: depRes }];
   36   } else {
   37     scannedProjects = [{ depTree: depRes }];
   38   }
   39 
   40   if (isLockFileBased) {
   41     const lockFileFullPath = path.resolve(root, targetFile);
   42     const lockfileVersion = getLockfileVersionFromFile(lockFileFullPath);
   43     switch (lockfileVersion) {
   44       case NodeLockfileVersion.NpmLockV1:
   45       case NodeLockfileVersion.YarnLockV1:
   46         analytics.add('lockfileVersion', 1);
   47         break;
   48       case NodeLockfileVersion.NpmLockV2:
   49       case NodeLockfileVersion.YarnLockV2:
   50         analytics.add('lockfileVersion', 2);
   51         break;
   52       case NodeLockfileVersion.NpmLockV3:
   53         analytics.add('lockfileVersion', 3);
   54         break;
   55       default:
   56         break;
   57     }
   58   }
   59 
   60   return {
   61     plugin: {
   62       name: 'snyk-nodejs-lockfile-parser',
   63       runtime: process.version,
   64     },
   65     scannedProjects,
   66   };
   67 }
   68 
   69 function isResDepGraph(depRes: PkgTree | DepGraph): depRes is DepGraph {
   70   return 'rootPkg' in depRes;
   71 }