Skip to content
Snippets Groups Projects
Select Git revision
  • e90593e339bf22c99f33ffbb9c69fe1bc17ae919
  • main default protected
  • renovate/main-ghcr.io-renovatebot-base-image-10.x
  • renovate/main-ghcr.io-containerbase-devcontainer-13.x
  • next
  • revert-31645-feat/rename-gradle-wrapper-validation-action
  • renovate/main-redis-5.x
  • fix/36615b-branch-reuse-no-cache
  • chore/punycode
  • fix/36615-branch-reuse-bug
  • 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
  • 41.26.1
  • 41.26.0
  • 41.25.1
  • 41.25.0
  • 41.24.0
  • 41.23.5
  • 41.23.4
  • 41.23.3
  • 41.23.2
  • 41.23.1
  • 41.23.0
  • 41.22.0
  • 41.21.4
  • 41.21.3
  • 41.21.2
  • 41.21.1
  • 41.21.0
  • 41.20.2
  • 41.20.1
  • 41.20.0
41 results

index.ts

Blame
  • npm.js 3.42 KiB
    const fs = require('fs');
    const cp = require('child_process');
    const tmp = require('tmp');
    const path = require('path');
    
    let logger = require('../../logger');
    
    module.exports = {
      generateLockFile,
      getLockFile,
      maintainLockFile,
    };
    
    async function generateLockFile(newPackageJson, npmrcContent) {
      logger.debug('Generating new package-lock.json file');
      const tmpDir = tmp.dirSync({ unsafeCleanup: true });
      let packageLock;
      try {
        fs.writeFileSync(path.join(tmpDir.name, 'package.json'), newPackageJson);
        if (npmrcContent) {
          fs.writeFileSync(path.join(tmpDir.name, '.npmrc'), npmrcContent);
        }
        logger.debug('Spawning npm install');
        const result = cp.spawnSync('npm', ['install', '--ignore-scripts'], {
          cwd: tmpDir.name,
          shell: true,
        });
        logger.debug(
          { stdout: String(result.stdout), stderr: String(result.stderr) },
          'npm install complete'
        );
        packageLock = fs.readFileSync(path.join(tmpDir.name, 'package-lock.json'));
      } catch (error) /* istanbul ignore next */ {
        try {
          tmpDir.removeCallback();
        } catch (err2) {
          logger.warn(`Failed to remove tmpDir ${tmpDir.name}`);
        }
        throw error;
      }
      try {
        tmpDir.removeCallback();
      } catch (err2) {
        logger.warn(`Failed to remove tmpDir ${tmpDir.name}`);
      }
      return packageLock;
    }
    
    async function getLockFile(
      packageFile,
      packageContent,
      api,
      npmVersion,
      parentLogger
    ) {
      logger = parentLogger || logger;
      // Detect if a package-lock.json file is in use
      const packageLockFileName = path.join(
        path.dirname(packageFile),
        'package-lock.json'
      );
      if (!await api.getFileContent(packageLockFileName)) {
        return null;
      }
      if (npmVersion === '') {
        throw new Error(
          'Need to generate package-lock.json but npm is not installed'
        );
      }
      // TODO: have a more forwards-compatible check
      if (npmVersion[0] !== '5') {
        throw new Error(
          `Need to generate package-lock.json but npm version is "${npmVersion}"`
        );
      }
      // Copy over custom config commitFiles
      const npmrcContent = await api.getFileContent('.npmrc');
      // Generate package-lock.json using shell command
      const newPackageLockContent = await module.exports.generateLockFile(
        packageContent,
        npmrcContent
      );
      // Return file object
      return {
        name: packageLockFileName,
        contents: newPackageLockContent,
      };
    }
    
    async function maintainLockFile(inputConfig) {
      logger = inputConfig.logger || logger;
      logger.trace({ config: inputConfig }, `maintainLockFile`);
      const packageContent = await inputConfig.api.getFileContent(
        inputConfig.packageFile
      );
      const packageLockFileName = path.join(
        path.dirname(inputConfig.packageFile),
        'package-lock.json'
      );
      logger.debug(`Checking for ${packageLockFileName}`);
      const existingPackageLock = await inputConfig.api.getFileContent(
        packageLockFileName
      );
      logger.trace(`existingPackageLock:\n${existingPackageLock}`);
      if (!existingPackageLock) {
        return null;
      }
      logger.debug('Found existing package-lock.json file');
      const newPackageLock = await module.exports.getLockFile(
        inputConfig.packageFile,
        packageContent,
        inputConfig.api
      );
      logger.trace(`newPackageLock:\n${newPackageLock.contents}`);
      if (existingPackageLock.toString() === newPackageLock.contents.toString()) {
        logger.debug('npm lock file does not need updating');
        return null;
      }
      logger.debug('npm lock needs updating');
      return newPackageLock;
    }