<commit_before>import type { PackageJson } from 'type-fest';
import { logger } from '../../../../logger';
import { api as semver } from '../../../../versioning/npm';
import type { UpdateLockedConfig } from '../../../types';
import { updateDependency } from '../dependency';
import { findDepConstraints } from './dep-constraints';
import { getLockedDependencies } from './get-locked';
import { findFirstParentVersion } from './parent-version';
import type { PackageLockOrEntry } from './types';

export function validateInputs(config: UpdateLockedConfig): boolean {
  const { currentVersion, newVersion, lockFile } = config;
  if (!lockFile.endsWith('package-lock.json')) {
    logger.debug({ lockFile }, 'Unsupported lock file');
    return false;
  }
  if (!(semver.isVersion(currentVersion) && semver.isVersion(newVersion))) {
    logger.warn(
      { currentVersion, newVersion },
      'Update versions are not valid'
    );
    return false;
  }
  return true;
}

export async function updateLockedDependency(
  config: UpdateLockedConfig,
  isParentUpdate = false
): Promise<Record<string, string>> {
  const {
    depName,
    currentVersion,
    newVersion,
    packageFile,
    packageFileContent,
    lockFile,
    lockFileContent,
  } = config;
  logger.debug(
    `npm.updateLockedDependency: ${depName}@${currentVersion} -> ${newVersion} [${lockFile}]`
  );
  try {
    if (!validateInputs(config)) {
      return null;
    }
    let packageJson: PackageJson;
    let packageLockJson: PackageLockOrEntry;
    let newPackageJsonContent: string;
    try {
      packageJson = JSON.parse(packageFileContent);
      packageLockJson = JSON.parse(lockFileContent);
    } catch (err) {
      logger.warn({ err }, 'Failed to parse files');
      return null;
    }
    if (packageLockJson.lockfileVersion === 2) {
      logger.debug('Only lockfileVersion 1 is supported');
      return null;
    }
    const lockedDeps = getLockedDependencies(
      packageLockJson,
      depName,
      currentVersion
    );
    if (!lockedDeps.length) {
      logger.debug(
        `${depName}@${currentVersion} not found in ${lockFile} - no work to do`
      );
      // Don't return null if we're a parent update or else the whole update will fail
      // istanbul ignore if: too hard to replicate
      if (isParentUpdate) {
        return {
          packageFile: packageFileContent,
          lockFile: lockFileContent,
        };
      }
      return null;
    }<commit_msg>fix: parent update empty return<commit_after>import type { PackageJson } from 'type-fest';
import { logger } from '../../../../logger';
import { api as semver } from '../../../../versioning/npm';
import type { UpdateLockedConfig } from '../../../types';
import { updateDependency } from '../dependency';
import { findDepConstraints } from './dep-constraints';
import { getLockedDependencies } from './get-locked';
import { findFirstParentVersion } from './parent-version';
import type { PackageLockOrEntry } from './types';

export function validateInputs(config: UpdateLockedConfig): boolean {
  const { currentVersion, newVersion, lockFile } = config;
  if (!lockFile.endsWith('package-lock.json')) {
    logger.debug({ lockFile }, 'Unsupported lock file');
    return false;
  }
  if (!(semver.isVersion(currentVersion) && semver.isVersion(newVersion))) {
    logger.warn(
      { currentVersion, newVersion },
      'Update versions are not valid'
    );
    return false;
  }
  return true;
}

export async function updateLockedDependency(
  config: UpdateLockedConfig,
  isParentUpdate = false
): Promise<Record<string, string>> {
  const {
    depName,
    currentVersion,
    newVersion,
    packageFile,
    packageFileContent,
    lockFile,
    lockFileContent,
  } = config;
  logger.debug(
    `npm.updateLockedDependency: ${depName}@${currentVersion} -> ${newVersion} [${lockFile}]`
  );
  try {
    if (!validateInputs(config)) {
      return null;
    }
    let packageJson: PackageJson;
    let packageLockJson: PackageLockOrEntry;
    let newPackageJsonContent: string;
    try {
      packageJson = JSON.parse(packageFileContent);
      packageLockJson = JSON.parse(lockFileContent);
    } catch (err) {
      logger.warn({ err }, 'Failed to parse files');
      return null;
    }
    if (packageLockJson.lockfileVersion === 2) {
      logger.debug('Only lockfileVersion 1 is supported');
      return null;
    }
    const lockedDeps = getLockedDependencies(
      packageLockJson,
      depName,
      currentVersion
    );
    if (!lockedDeps.length) {
      logger.debug(
        `${depName}@${currentVersion} not found in ${lockFile} - no work to do`
      );
      // Don't return null if we're a parent update or else the whole update will fail
      // istanbul ignore if: too hard to replicate
      if (isParentUpdate) {
        const res = {};
        res[packageFile] = packageFileContent;
        res[lockFile] = lockFileContent;
        return res;
      }
      return null;
    }
{'fraction_non_alphanumeric': 0.12575210589651023, 'fraction_numerical': 0.0008022462896109105, 'mean_word_length': 3.02827140549273, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5824555', 'n_tokens_mistral': 1402, 'n_tokens_neox': 1406, 'n_words': 381}