UNPKG

snyk-nuget-plugin

Version:
119 lines 4.75 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getTargetFrameworksFromProjFile = getTargetFrameworksFromProjFile; const errors_1 = require("../../errors"); const fs = __importStar(require("fs")); const path = __importStar(require("path")); const parseXML = __importStar(require("xml2js")); const debug_1 = __importDefault(require("debug")); const framework_1 = require("../framework"); const debug = (0, debug_1.default)('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 { throw new errors_1.FileNotProcessableError(`Could not parse ${csprojPath}`); } return result; } //# sourceMappingURL=csproj-parser.js.map