UNPKG

snyk-nuget-plugin

Version:
83 lines 3.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getTargetFrameworksFromProjFile = getTargetFrameworksFromProjFile; const errors_1 = require("../../errors"); const fs = require("fs"); const path = require("path"); const parseXML = require("xml2js"); const debugModule = require("debug"); const framework_1 = require("../framework"); const debug = debugModule('snyk'); function findFile(rootDir, filter) { if (!fs.existsSync(rootDir)) { throw new errors_1.FileNotFoundError('No such path: ' + rootDir); } const files = fs.readdirSync(rootDir); for (const file of files) { const filename = path.resolve(rootDir, file); if (filter.test(filename)) { return filename; } } return; } function readEncodedFile(path) { const buffer = fs.readFileSync(path); const firstChar = buffer.readUInt16LE(0); let contents; if (firstChar === 0xfeff) { contents = buffer.toString('utf16le'); } else { contents = buffer.toString('utf8'); } return contents; } function getTargetFrameworksFromProjFile(rootDir) { debug('Looking for your .csproj file in ' + rootDir); const csprojPath = findFile(rootDir, /.*\.csproj$/); if (!csprojPath) { debug('.csproj file not found in ' + rootDir + '.'); return []; } debug(`Checking .NET framework version in .csproj file ${csprojPath}`); const csprojContents = readEncodedFile(csprojPath); let result = []; try { parseXML.parseString(csprojContents, (err, parsedCsprojContents) => { if (err) { throw new errors_1.FileNotProcessableError(err); } const parsedTargetFrameworks = parsedCsprojContents?.Project?.PropertyGroup?.reduce((targetFrameworks, propertyGroup) => { const targetFrameworkSource = propertyGroup?.TargetFrameworkVersion?.[0] || propertyGroup?.TargetFramework?.[0] || propertyGroup?.TargetFrameworks?.[0] || ''; return targetFrameworks .concat(targetFrameworkSource.split(';')) .filter(Boolean); }, []) || []; if (parsedTargetFrameworks.length < 1) { debug('Could not find TargetFrameworkVersion/TargetFramework' + '/TargetFrameworks defined in the Project.PropertyGroup field of ' + 'your .csproj file'); result = []; return; } const targetFrameworks = parsedTargetFrameworks .map(framework_1.toReadableFramework) .filter(Boolean); if (parsedTargetFrameworks.length > 1 && targetFrameworks.length < 1) { debug('Could not find valid/supported .NET version in csproj file located at' + csprojPath); } result = targetFrameworks; return; }); } catch (err) { throw new errors_1.FileNotProcessableError(`Could not parse ${csprojPath}`); } return result; } //# sourceMappingURL=csproj-parser.js.map