Skip to content
Snippets Groups Projects
Select Git revision
  • 7675f3a9f12728b6e5d7c68ac5b0897594a0cfd8
  • main default protected
  • feat/gnupg
  • next
  • fix/36615b-branch-reuse-no-cache
  • renovate/main-redis-5.x
  • chore/punycode
  • refactor/pin-new-value
  • feat/36219--git-x509-signing
  • feat/structured-logger
  • hotfix/39.264.1
  • feat/skip-dangling
  • gh-readonly-queue/next/pr-36034-7a061c4ca1024a19e2c295d773d9642625d1c2be
  • hotfix/39.238.3
  • refactor/gitlab-auto-approve
  • feat/template-strings
  • gh-readonly-queue/next/pr-35654-137d934242c784e0c45d4b957362214f0eade1d7
  • fix/32307-global-extends-merging
  • fix/32307-global-extends-repositories
  • gh-readonly-queue/next/pr-35009-046ebf7cb84ab859f7fefceb5fa53a54ce9736f8
  • gh-readonly-queue/next/pr-35009-9d5e583b7d7251148ab0d11ee8dd38149618d162
  • 41.43.5
  • 41.43.4
  • 41.43.3
  • 41.43.2
  • 41.43.1
  • 41.43.0
  • 41.42.12
  • 41.42.11
  • 41.42.10
  • 41.42.9
  • 41.42.8
  • 41.42.7
  • 41.42.6
  • 41.42.5
  • 41.42.4
  • 41.42.3
  • 41.42.2
  • 41.42.1
  • 41.42.0
  • 41.41.0
41 results

lock-files.js

Blame
  • user avatar
    Rhys Arkins authored and singapore committed
    * refactor: don’t store yarn.lock content in packageFile
    
    * chore: don’t log full lockfile
    
    * refactor: don’t save package-lock.json
    
    * update tests
    804d7ba9
    History
    lock-files.js 7.95 KiB
    const fs = require('fs-extra');
    const path = require('path');
    const npm = require('./npm');
    const yarn = require('./yarn');
    
    module.exports = {
      hasPackageLock,
      hasYarnLock,
      determineLockFileDirs,
      writeExistingFiles,
      writeUpdatedPackageFiles,
      getUpdatedLockFiles,
    };
    
    function hasPackageLock(config, packageFile) {
      logger.trace(
        { packageFiles: config.packageFiles, packageFile },
        'hasPackageLock'
      );
      for (const p of config.packageFiles) {
        if (p.packageFile === packageFile) {
          if (p.packageLock) {
            return true;
          }
          return false;
        }
      }
      throw new Error(`hasPackageLock cannot find ${packageFile}`);
    }
    
    function hasYarnLock(config, packageFile) {
      logger.trace(
        { packageFiles: config.packageFiles, packageFile },
        'hasYarnLock'
      );
      for (const p of config.packageFiles) {
        if (p.packageFile === packageFile) {
          if (p.yarnLock) {
            return true;
          }
          return false;
        }
      }
      throw new Error(`hasYarnLock cannot find ${packageFile}`);
    }
    
    function determineLockFileDirs(config) {
      const packageLockFileDirs = [];
      const yarnLockFileDirs = [];
    
      for (const upgrade of config.upgrades) {
        if (upgrade.type === 'lockFileMaintenance') {
          // Return every direcotry that contains a lockfile
          for (const packageFile of config.packageFiles) {
            const dirname = path.dirname(packageFile.packageFile);
            if (packageFile.yarnLock) {
              yarnLockFileDirs.push(dirname);
            }
            if (packageFile.packageLock) {
              packageLockFileDirs.push(dirname);
            }
          }
          return { packageLockFileDirs, yarnLockFileDirs };
        }
      }
    
      for (const packageFile of config.updatedPackageFiles) {
        if (module.exports.hasYarnLock(config, packageFile.name)) {
          yarnLockFileDirs.push(path.dirname(packageFile.name));
        }
        if (module.exports.hasPackageLock(config, packageFile.name)) {
          packageLockFileDirs.push(path.dirname(packageFile.name));
        }
      }
    
      // If yarn workspaces are in use, then we need to generate yarn.lock from the workspaces dir
      if (config.updatedPackageFiles.length && config.workspaceDir) {
        const updatedPackageFileNames = config.updatedPackageFiles.map(p => p.name);
        for (const packageFile of config.packageFiles) {
          if (
            updatedPackageFileNames.includes(packageFile.packageFile) &&
            packageFile.workspaceDir &&
            !yarnLockFileDirs.includes(packageFile.workspaceDir)
          )
            yarnLockFileDirs.push(packageFile.workspaceDir);
        }
      }
    
      return { yarnLockFileDirs, packageLockFileDirs };
    }
    
    async function writeExistingFiles(config) {
      if (config.npmrc) {
        logger.debug('Writing repo .npmrc');
        await fs.outputFile(path.join(config.tmpDir.path, '.npmrc'), config.npmrc);
      }
      if (config.yarnrc) {
        logger.debug('Writing repo .yarnrc');
        await fs.outputFile(
          path.join(config.tmpDir.path, '.yarnrc'),
          config.yarnrc
        );
      }
      if (!config.packageFiles) {
        return;
      }
      for (const packageFile of config.packageFiles) {
        const basedir = path.join(
          config.tmpDir.path,
          path.dirname(packageFile.packageFile)
        );
        if (packageFile.packageFile.endsWith('package.json')) {
          logger.debug(`Writing package.json to ${basedir}`);
          // Massage the file to eliminate yarn errors
          const massagedFile = { ...packageFile.content };
          if (massagedFile.name) {
            massagedFile.name = massagedFile.name.replace(/[{}]/g, '');
          }
          delete massagedFile.engines;
          delete massagedFile.scripts;
          await fs.outputFile(
            path.join(basedir, 'package.json'),
            JSON.stringify(massagedFile)
          );
        }
        if (packageFile.npmrc) {
          logger.debug(`Writing .npmrc to ${basedir}`);
          await fs.outputFile(path.join(basedir, '.npmrc'), packageFile.npmrc);
        } else if (
          config.npmrc &&
          (packageFile.hasYarnLock || packageFile.hasPackageLock)
        ) {
          logger.debug('Writing repo .npmrc to package file dir');
          await fs.outputFile(path.join(basedir, '.npmrc'), config.npmrc);
        }
        if (packageFile.yarnrc) {
          logger.debug(`Writing .yarnrc to ${basedir}`);
          await fs.outputFile(
            path.join(basedir, '.yarnrc'),
            packageFile.yarnrc.replace('--install.pure-lockfile true', '')
          );
        }
        if (packageFile.packageLock && config.type !== 'lockFileMaintenance') {
          logger.debug(`Writing package-lock.json to ${basedir}`);
          const packageLock = await platform.getFile(packageFile.packageLock);
          await fs.outputFile(path.join(basedir, 'package-lock.json'), packageLock);
        } else {
          await fs.remove(path.join(basedir, 'package-lock.json'));
        }
        if (packageFile.yarnLock && config.type !== 'lockFileMaintenance') {
          logger.debug(`Writing yarn.lock to ${basedir}`);
          const yarnLock = await platform.getFile(packageFile.yarnLock);
          await fs.outputFile(path.join(basedir, 'yarn.lock'), yarnLock);
        } else {
          await fs.remove(path.join(basedir, 'yarn.lock'));
        }
      }
    }
    
    async function writeUpdatedPackageFiles(config) {
      logger.trace({ config }, 'writeUpdatedPackageFiles');
      logger.debug('Writing any updated package files');
      if (!config.updatedPackageFiles) {
        logger.debug('No files found');
        return;
      }
      for (const packageFile of config.updatedPackageFiles) {
        if (!packageFile.name.endsWith('package.json')) {
          continue; // eslint-disable-line
        }
        logger.debug(`Writing ${packageFile.name}`);
        const massagedFile = JSON.parse(packageFile.contents);
        if (massagedFile.name) {
          massagedFile.name = massagedFile.name.replace(/[{}]/g, '');
        }
        delete massagedFile.engines;
        delete massagedFile.scripts;
        await fs.outputFile(
          path.join(config.tmpDir.path, packageFile.name),
          JSON.stringify(massagedFile)
        );
      }
    }
    
    async function getUpdatedLockFiles(config) {
      logger.trace({ config }, 'getUpdatedLockFiles');
      logger.debug('Getting updated lock files');
      const lockFileErrors = [];
      const updatedLockFiles = [];
      if (
        config.type === 'lockFileMaintenance' &&
        (await platform.branchExists(config.branchName))
      ) {
        return { lockFileErrors, updatedLockFiles };
      }
      const dirs = module.exports.determineLockFileDirs(config);
      logger.debug({ dirs }, 'lock file dirs');
      await module.exports.writeExistingFiles(config);
      await module.exports.writeUpdatedPackageFiles(config);
    
      for (const lockFileDir of dirs.packageLockFileDirs) {
        logger.debug(`Generating package-lock.json for ${lockFileDir}`);
        const lockFileName = path.join(lockFileDir, 'package-lock.json');
        const res = await npm.generateLockFile(
          path.join(config.tmpDir.path, lockFileDir)
        );
        if (res.error) {
          lockFileErrors.push({
            lockFile: lockFileName,
            stderr: res.stderr,
          });
        } else {
          const existingContent = await platform.getFile(
            lockFileName,
            config.parentBranch
          );
          if (res.lockFile !== existingContent) {
            logger.debug('package-lock.json needs updating');
            updatedLockFiles.push({
              name: lockFileName,
              contents: res.lockFile,
            });
          } else {
            logger.debug("package-lock.json hasn't changed");
          }
        }
      }
    
      for (const lockFileDir of dirs.yarnLockFileDirs) {
        logger.debug(`Generating yarn.lock for ${lockFileDir}`);
        const lockFileName = path.join(lockFileDir, 'yarn.lock');
        const res = await yarn.generateLockFile(
          path.join(config.tmpDir.path, lockFileDir)
        );
        if (res.error) {
          lockFileErrors.push({
            lockFile: lockFileName,
            stderr: res.stderr,
          });
        } else {
          const existingContent = await platform.getFile(
            lockFileName,
            config.parentBranch
          );
          if (res.lockFile !== existingContent) {
            logger.debug('yarn.lock needs updating');
            updatedLockFiles.push({
              name: lockFileName,
              contents: res.lockFile,
            });
          } else {
            logger.debug("yarn.lock hasn't changed");
          }
        }
      }
      return { lockFileErrors, updatedLockFiles };
    }