<commit_before>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// @flow

/*:: type AgentsVersion = { [agentName: string]: string }; */

/*
 * This file is run when a user runs `yarn install`, before doing anything else.
 * We check that various tools we need have the correct version. For now we
 * check Node and Yarn.
 */
checkVersions();

function checkVersions() {
  // The "userAgent" is something like 'yarn/0.28.1 npm/? node/v8.4.0 linux x64'
  // when ran with yarn. With npm it's similar, without the `yarn` part of course.
  const userAgent = process.env.npm_config_user_agent;
  if (!userAgent) {
    console.error('Error: this script cannot be run directly.');
    process.exit(-1);
    return;
  }

  const agents /*: AgentsVersion */ = userAgent
    .split(' ')
    .reduce((agents, agent) => {
      const [key, value] = agent.split('/');
      agents[key] = value;
      return agents;
    }, {});

  const checks = [checkNode(agents), checkYarn(agents)];

  const successful = checks.every(returnValue => returnValue);
  if (successful) {
    console.log(
      'All project requirements are satisfied, moving forward with the installation.'
    );
    process.exit(0);
  }
  process.exit(-1);
}

// This function compares two string versions. This compares only major.minor
// and disrespect any textual prevision (like pre or beta).
function versionCompare(a, b) {
  return a.localeCompare(b, undefined, { numeric: true });
}

function checkNode(agents /*: AgentsVersion */) {
  // Node versions usually have a starting `v`.
  const nodeVersion = agents.node.replace(/^v/, '');
  const expectedNodeVersion = parseExpectedNodeVersion();
  if (versionCompare(nodeVersion, expectedNodeVersion) < 0) {
    console.error(
      `This project expects at least Node version ${expectedNodeVersion}.`
    );
    console.error(
      'You can use a tool like `nvm` to install and manage installed node versions.'
    );<commit_msg>Slightly change the error message when the node version is incorrect, to show the current node version (PR #2587)<commit_after>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// @flow

/*:: type AgentsVersion = { [agentName: string]: string }; */

/*
 * This file is run when a user runs `yarn install`, before doing anything else.
 * We check that various tools we need have the correct version. For now we
 * check Node and Yarn.
 */
checkVersions();

function checkVersions() {
  // The "userAgent" is something like 'yarn/0.28.1 npm/? node/v8.4.0 linux x64'
  // when ran with yarn. With npm it's similar, without the `yarn` part of course.
  const userAgent = process.env.npm_config_user_agent;
  if (!userAgent) {
    console.error('Error: this script cannot be run directly.');
    process.exit(-1);
    return;
  }

  const agents /*: AgentsVersion */ = userAgent
    .split(' ')
    .reduce((agents, agent) => {
      const [key, value] = agent.split('/');
      agents[key] = value;
      return agents;
    }, {});

  const checks = [checkNode(agents), checkYarn(agents)];

  const successful = checks.every(returnValue => returnValue);
  if (successful) {
    console.log(
      'All project requirements are satisfied, moving forward with the installation.'
    );
    process.exit(0);
  }
  process.exit(-1);
}

// This function compares two string versions. This compares only major.minor
// and disrespect any textual prevision (like pre or beta).
function versionCompare(a, b) {
  return a.localeCompare(b, undefined, { numeric: true });
}

function checkNode(agents /*: AgentsVersion */) {
  // Node versions usually have a starting `v`.
  const nodeVersion = agents.node.replace(/^v/, '');
  const expectedNodeVersion = parseExpectedNodeVersion();
  if (versionCompare(nodeVersion, expectedNodeVersion) < 0) {
    console.error(
      `This project expects at least Node version ${expectedNodeVersion} but version ${nodeVersion} is currently used.`
    );
    console.error(
      'You can use a tool like `nvm` to install and manage installed node versions.'
    );
{'fraction_non_alphanumeric': 0.12191749250979489, 'fraction_numerical': 0.008757778289928554, 'mean_word_length': 3.3705941591137965, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '5242072', 'n_tokens_mistral': 1296, 'n_tokens_neox': 1233, 'n_words': 472}