From d4b04a6425c4e1169fa3a61c959624e3eaa6f6e5 Mon Sep 17 00:00:00 2001 From: saark-snyk Date: Mon, 20 Dec 2021 09:24:38 +0200 Subject: [PATCH] chore: reorganize interfaces exports --- src/analysis.ts | 57 ++++++++++---------- src/bundles.ts | 6 ++- src/files.ts | 18 ++----- src/index.ts | 13 +++-- src/interfaces/analysis-options.interface.ts | 20 +++++++ src/interfaces/files.interface.ts | 9 ++++ tests/analysis.spec.ts | 6 +-- 7 files changed, 77 insertions(+), 52 deletions(-) diff --git a/src/analysis.ts b/src/analysis.ts index ce4c4f30..25262d94 100644 --- a/src/analysis.ts +++ b/src/analysis.ts @@ -1,7 +1,7 @@ /* eslint-disable no-await-in-loop */ import omit from 'lodash.omit'; -import { AnalyzeFoldersOptions, prepareExtendingBundle, resolveBundleFilePath, calcHash } from './files'; +import { prepareExtendingBundle, resolveBundleFilePath, calcHash } from './files'; import { POLLING_INTERVAL } from './constants'; import { GetAnalysisErrorCodes, @@ -10,13 +10,19 @@ import { Result, GetAnalysisResponseDto, AnalysisFailedResponse, - AnalysisOptions, - ConnectionOptions, GetAnalysisOptions, } from './http'; -import { createBundleFromFolders, FileBundle, remoteBundleFactory } from './bundles'; +import { createBundleFromFolders, remoteBundleFactory } from './bundles'; import { emitter } from './emitter'; -import { AnalysisResult, AnalysisResultLegacy, AnalysisResultSarif, AnalysisFiles, Suggestion } from './interfaces/analysis-result.interface'; +import { + AnalysisResult, + AnalysisResultLegacy, + AnalysisResultSarif, + AnalysisFiles, + Suggestion, +} from './interfaces/analysis-result.interface'; +import { FileAnalysisOptions } from './interfaces/analysis-options.interface'; +import { FileAnalysis } from './interfaces/files.interface'; const sleep = (duration: number) => new Promise(resolve => setTimeout(resolve, duration)); @@ -86,17 +92,6 @@ function normalizeResultFiles(files: AnalysisFiles, baseDir: string): AnalysisFi return files; } -interface FileAnalysisOptions { - connection: ConnectionOptions; - analysisOptions: AnalysisOptions; - fileOptions: AnalyzeFoldersOptions; -} - -export interface FileAnalysis extends FileAnalysisOptions { - fileBundle: FileBundle; - analysisResults: AnalysisResult; -} - export async function analyzeFolders(options: FileAnalysisOptions): Promise { const fileBundle = await createBundleFromFolders({ ...options.connection, @@ -120,19 +115,24 @@ export async function analyzeFolders(options: FileAnalysisOptions): Promise { +export async function extendAnalysis(options: FileAnalysis & { files: string[] }): Promise { const { files, removedFiles } = await prepareExtendingBundle( options.fileBundle.baseDir, options.fileBundle.supportedFiles, @@ -303,7 +298,13 @@ export async function extendAnalysis(options: ExtendAnalysisOptions): Promise> { +async function* prepareRemoteBundle( + options: PrepareRemoteBundleOptions, +): AsyncGenerator> { let response: Result; let { bundleHash } = options; let cumulativeProgress = 0; diff --git a/src/files.ts b/src/files.ts index 0a8459cf..796adad9 100644 --- a/src/files.ts +++ b/src/files.ts @@ -17,7 +17,7 @@ import { CACHE_KEY, DOTSNYK_FILENAME, } from './constants'; - +import { CollectBundleFilesOptions } from './interfaces/analysis-options.interface'; import { SupportedFiles, FileInfo } from './interfaces/files.interface'; const isWindows = nodePath.sep === '\\'; @@ -225,18 +225,6 @@ async function* searchFiles( } } -export interface AnalyzeFoldersOptions { - paths: string[]; - symlinksEnabled?: boolean; - defaultFileIgnores?: string[]; -} - -export interface CollectBundleFilesOptions extends AnalyzeFoldersOptions { - supportedFiles: SupportedFiles; - baseDir: string; - fileIgnores: string[]; -} - /** * Returns bundle files from requested paths * */ @@ -350,7 +338,7 @@ export async function prepareExtendingBundle( }; } -export function getBundleFilePath(filePath: string, baseDir: string) { +export function getBundleFilePath(filePath: string, baseDir: string): string { const relPath = baseDir ? nodePath.relative(baseDir, filePath) : filePath; // relPath without explicit base makes no sense const posixPath = !isWindows ? relPath : relPath.replace(/\\/g, '/'); return encodeURI(posixPath); @@ -358,7 +346,7 @@ export function getBundleFilePath(filePath: string, baseDir: string) { export function calcHash(content: string): string { return crypto.createHash(HASH_ALGORITHM).update(content).digest(ENCODE_TYPE); -}; +} export async function getFileInfo( filePath: string, diff --git a/src/index.ts b/src/index.ts index 2a6b2415..a856c6de 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,13 +1,20 @@ -import { analyzeFolders, extendAnalysis, FileAnalysis } from './analysis'; +import { analyzeFolders, extendAnalysis } from './analysis'; import { createBundleFromFolders } from './bundles'; import { emitter } from './emitter'; import { startSession, checkSession, getAnalysis, getIpFamily, IpFamily } from './http'; import * as constants from './constants'; import { getGlobPatterns } from './files'; -import { SupportedFiles } from './interfaces/files.interface'; +import { SupportedFiles, FileAnalysis } from './interfaces/files.interface'; import { AnalysisSeverity } from './interfaces/analysis-options.interface'; -import { AnalysisResult, AnalysisResultLegacy, FilePath, FileSuggestion, Suggestion, Marker } from './interfaces/analysis-result.interface'; +import { + AnalysisResult, + AnalysisResultLegacy, + FilePath, + FileSuggestion, + Suggestion, + Marker, +} from './interfaces/analysis-result.interface'; export { getGlobPatterns, diff --git a/src/interfaces/analysis-options.interface.ts b/src/interfaces/analysis-options.interface.ts index 3a94eec5..66265794 100644 --- a/src/interfaces/analysis-options.interface.ts +++ b/src/interfaces/analysis-options.interface.ts @@ -1,6 +1,26 @@ +import { SupportedFiles } from '..'; +import { AnalysisOptions, ConnectionOptions } from '../http'; + // eslint-disable-next-line import/prefer-default-export, no-shadow export enum AnalysisSeverity { info = 1, warning = 2, critical = 3, } +export interface FileAnalysisOptions { + connection: ConnectionOptions; + analysisOptions: AnalysisOptions; + fileOptions: AnalyzeFoldersOptions; +} + +export interface AnalyzeFoldersOptions { + paths: string[]; + symlinksEnabled?: boolean; + defaultFileIgnores?: string[]; +} + +export interface CollectBundleFilesOptions extends AnalyzeFoldersOptions { + supportedFiles: SupportedFiles; + baseDir: string; + fileIgnores: string[]; +} diff --git a/src/interfaces/files.interface.ts b/src/interfaces/files.interface.ts index 100a7b67..87472079 100644 --- a/src/interfaces/files.interface.ts +++ b/src/interfaces/files.interface.ts @@ -1,3 +1,7 @@ +import { AnalysisResult } from '..'; +import { FileBundle } from '../bundles'; +import { FileAnalysisOptions } from './analysis-options.interface'; + export interface File { hash: string; content: string; @@ -19,3 +23,8 @@ export type SupportedFiles = { configFiles: string[]; extensions: string[]; }; + +export interface FileAnalysis extends FileAnalysisOptions { + fileBundle: FileBundle; + analysisResults: AnalysisResult; +} diff --git a/tests/analysis.spec.ts b/tests/analysis.spec.ts index 02b34e9f..4a561819 100644 --- a/tests/analysis.spec.ts +++ b/tests/analysis.spec.ts @@ -1,7 +1,7 @@ import path from 'path'; import jsonschema from 'jsonschema'; -import { analyzeFolders, extendAnalysis, FileAnalysis } from '../src/analysis'; +import { analyzeFolders, extendAnalysis } from '../src/analysis'; import { uploadRemoteBundle } from '../src/bundles'; import { baseURL, sessionToken, source, TEST_TIMEOUT } from './constants/base'; import { sampleProjectPath, bundleFiles, bundleFilesFull, bundleExtender } from './constants/sample'; @@ -262,9 +262,7 @@ describe('Functional test of analysis', () => { if (!sampleRes) return; // TS trick expect(sampleRes.ruleIndex).toBeDefined(); if (!sampleRes.ruleIndex) return; // TS trick - expect(sampleRes!.ruleId).toEqual( - sarifResults.runs[0].tool.driver.rules![sampleRes!.ruleIndex!].id, - ); + expect(sampleRes!.ruleId).toEqual(sarifResults.runs[0].tool.driver.rules![sampleRes!.ruleIndex!].id); expect(extendedBundle.analysisResults.timing.analysis).toBeGreaterThanOrEqual( extendedBundle.analysisResults.timing.fetchingCode,