Skip to content
Snippets Groups Projects
Select Git revision
  • c40ac32cf00fe6fd843626c8411cd87315c3a737
  • develop default protected
  • feature/module-gpiod
  • 0.5.6
  • 0.5.5
  • 0.5.4
  • 0.5.3
  • 0.5.2
  • 0.5.1
  • 0.5.0
  • 0.4.1
  • 0.4.0
  • 0.3.2
  • 0.3.1
  • 0.3.0
  • 0.2.8
  • 0.2.7
  • 0.2.6
  • 0.2.5
  • 0.2.4
  • 0.2.3
  • 0.2.2
  • 0.2.1
23 results

tox.ini

Blame
  • extract.ts 2.13 KiB
    import { logger } from '../../../logger';
    import { findLocalSiblingOrParent, localPathExists } from '../../../util/fs';
    import { newlineRegex, regEx } from '../../../util/regex';
    import { HexDatasource } from '../../datasource/hex';
    import type { PackageDependency, PackageFile } from '../types';
    
    const depSectionRegExp = regEx(/defp\s+deps.*do/g);
    const depMatchRegExp = regEx(
      /{:(?<depName>\w+),\s*(?<datasource>[^:"]+)?:?\s*"(?<currentValue>[^"]+)",?\s*(?:organization: "(?<organization>.*)")?.*}/gm
    );
    
    export async function extractPackageFile(
      content: string,
      fileName: string
    ): Promise<PackageFile | null> {
      logger.trace('mix.extractPackageFile()');
      const deps: PackageDependency[] = [];
      const contentArr = content.split(newlineRegex);
    
      for (let lineNumber = 0; lineNumber < contentArr.length; lineNumber += 1) {
        if (contentArr[lineNumber].match(depSectionRegExp)) {
          logger.trace(`Matched dep section on line ${lineNumber}`);
          let depBuffer = '';
          do {
            depBuffer += contentArr[lineNumber] + '\n';
            lineNumber += 1;
          } while (!contentArr[lineNumber].includes('end'));
          let depMatchGroups = depMatchRegExp.exec(depBuffer)?.groups;
          while (depMatchGroups) {
            const { depName, datasource, currentValue, organization } =
              depMatchGroups;
    
            const dep: PackageDependency = {
              depName,
              currentValue,
            };
    
            dep.datasource = datasource || HexDatasource.id;
    
            if (dep.datasource === HexDatasource.id) {
              dep.currentValue = currentValue;
              dep.packageName = depName;
            }
    
            if (organization) {
              dep.packageName += ':' + organization;
            }
    
            if (dep.datasource !== HexDatasource.id) {
              dep.skipReason = 'non-hex-dep-types';
            }
    
            deps.push(dep);
            depMatchGroups = depMatchRegExp.exec(depBuffer)?.groups;
          }
        }
      }
      const res: PackageFile = { deps };
      const lockFileName =
        (await findLocalSiblingOrParent(fileName, 'mix.lock')) ?? 'mix.lock';
      // istanbul ignore if
      if (await localPathExists(lockFileName)) {
        res.lockFiles = [lockFileName];
      }
      return res;
    }