From 2780cac2dc96f6059cb09a1fd878ac3ed85ac753 Mon Sep 17 00:00:00 2001 From: ghe Date: Fri, 28 Aug 2020 16:06:00 +0100 Subject: [PATCH] chore: move getFileContents to a helper file --- src/lib/get-file-contents.ts | 22 +++++++++++++++++++ .../nodejs-plugin/yarn-workspaces-parser.ts | 22 +------------------ 2 files changed, 23 insertions(+), 21 deletions(-) create mode 100644 src/lib/get-file-contents.ts diff --git a/src/lib/get-file-contents.ts b/src/lib/get-file-contents.ts new file mode 100644 index 00000000000..bb000759efd --- /dev/null +++ b/src/lib/get-file-contents.ts @@ -0,0 +1,22 @@ +import * as fs from 'fs'; +import * as path from 'path'; + +export function getFileContents( + root: string, + fileName: string, +): { + content: string; + name: string; +} { + const fullPath = path.resolve(root, fileName); + if (!fs.existsSync(fullPath)) { + throw new Error( + 'Manifest ' + fileName + ' not found at location: ' + fileName, + ); + } + const content = fs.readFileSync(fullPath, 'utf-8'); + return { + content, + name: fileName, + }; +} diff --git a/src/lib/plugins/nodejs-plugin/yarn-workspaces-parser.ts b/src/lib/plugins/nodejs-plugin/yarn-workspaces-parser.ts index 1ea3cf7b2f7..3bbb75ad952 100644 --- a/src/lib/plugins/nodejs-plugin/yarn-workspaces-parser.ts +++ b/src/lib/plugins/nodejs-plugin/yarn-workspaces-parser.ts @@ -3,7 +3,6 @@ import * as pathUtil from 'path'; import * as _ from '@snyk/lodash'; const debug = baseDebug('snyk:yarn-workspaces'); -import * as fs from 'fs'; import * as lockFileParser from 'snyk-nodejs-lockfile-parser'; import * as path from 'path'; import { NoSupportedManifestsFoundError } from '../../errors'; @@ -11,6 +10,7 @@ import { MultiProjectResultCustom, ScannedProjectCustom, } from '../get-multi-plugin-result'; +import { getFileContents } from '../../get-file-contents'; export async function processYarnWorkspaces( root: string, @@ -102,26 +102,6 @@ export async function processYarnWorkspaces( return result; } -function getFileContents( - root: string, - fileName: string, -): { - content: string; - name: string; -} { - const fullPath = path.resolve(root, fileName); - if (!fs.existsSync(fullPath)) { - throw new Error( - 'Manifest ' + fileName + ' not found at location: ' + fileName, - ); - } - const content = fs.readFileSync(fullPath, 'utf-8'); - return { - content, - name: fileName, - }; -} - interface YarnWorkspacesMap { [packageJsonName: string]: { workspaces: string[];