<commit_before>/**
 * Copyright (c) 2019 The xterm.js authors. All rights reserved.
 * @license MIT
 */

const cp = require('child_process');
const fs = require('fs');
const path = require('path');

// Setup auth
fs.writeFileSync(`${process.env['HOME']}/.npmrc`, `//registry.npmjs.org/:_authToken=${process.env['NPM_AUTH_TOKEN']}`);

const isDryRun = process.argv.indexOf('--dry') !== -1;
if (isDryRun) {
  console.log('Publish dry run');
}

const changedFiles = getChangedFilesInCommit('HEAD');

// Publish xterm if any files were changed outside of the addons directory
if (changedFiles.some(e => e.search(/^addons\//) === -1)) {
  checkAndPublishPackage(path.resolve(__dirname, '..'));
}

// Publish addons if any files were changed inside of the addon
const addonPackageDirs = [
  path.resolve(__dirname, '../addons/xterm-addon-attach'),
  path.resolve(__dirname, '../addons/xterm-addon-fit'),
  path.resolve(__dirname, '../addons/xterm-addon-search'),
  path.resolve(__dirname, '../addons/xterm-addon-web-links')
];
addonPackageDirs.forEach(p => {
  const addon = path.basename(p);
  if (changedFiles.some(e => e.indexOf(addon) !== -1)) {
    checkAndPublishPackage(p);
  }
});

function checkAndPublishPackage(packageDir) {
  const packageJson = require(path.join(packageDir, 'package.json'));

  // Determine if this is a stable or beta release
  const publishedVersions = getPublishedVersions(packageJson);
  const isStableRelease = publishedVersions.indexOf(packageJson.version) === -1;

  // Get the next version
  let nextVersion = isStableRelease ? packageJson.version : getNextBetaVersion(packageJson);
  console.log(`Publishing version: ${nextVersion}`);

  // Set the version in package.json
  const packageJsonFile = path.join(packageDir, 'package.json');
  packageJson.version = nextVersion;
  if (isDryRun) {
    console.log(`Set version of ${packageJsonFile} to ${nextVersion}`);
  } else {
    fs.writeFileSync(packageJsonFile, JSON.stringify(packageJson, null, 2));
  }

  // Publish
  const args = ['publish'];
  if (!isStableRelease) {
    args.push('--tag', 'beta');
  }
  if (isDryRun) {
    console.log(`Spawn: npm ${args.join(' ')}`);
  } else {
    const result = cp.spawn('npm', args, { stdio: 'inherit' });
    result.on('exit', code => process.exit(code));
  }

  console.groupEnd();
}

function getNextBetaVersion(packageJson) {
  if (!/^[0-9]+\.[0-9]+\.[0-9]+$/.exec(packageJson.version)) {
    console.error('The package.json version must be of the form x.y.z');
    process.exit(1);
  }
  const tag = 'beta';
  const stableVersion = packageJson.version.split('.');
  const nextStableVersion = `${stableVersion[0]}.${parseInt(stableVersion[1]) + 1}.0`;
  const publishedVersions = getPublishedVersions(packageJson, nextStableVersion, tag);
  if (publishedVersions.length === 0) {
    return `${nextStableVersion}-${tag}1`;
  }
  const latestPublishedVersion = publishedVersions.sort((a, b) => {
    const aVersion = parseInt(a.substr(a.search(/[0-9]+$/)));
    const bVersion = parseInt(b.substr(b.search(/[0-9]+$/)));
    return aVersion > bVersion ? -1 : 1;
  })[0];
  const latestTagVersion = parseInt(latestPublishedVersion.substr(latestPublishedVersion.search(/[0-9]+$/)), 10);
  return `${nextStableVersion}-${tag}${latestTagVersion + 1}`;
}

function getPublishedVersions(packageJson, version, tag) {
  const versionsProcess = cp.spawnSync('npm', ['view', packageJson.name, 'versions', '--json']);
  const versionsJson = JSON.parse(versionsProcess.stdout);
  if (tag) {
    return versionsJson.filter(v => !v.search(new RegExp(`${version}-${tag}[0-9]+`)));<commit_msg>Publish addons with right cwd and don't exit<commit_after>/**
 * Copyright (c) 2019 The xterm.js authors. All rights reserved.
 * @license MIT
 */

const cp = require('child_process');
const fs = require('fs');
const path = require('path');

// Setup auth
fs.writeFileSync(`${process.env['HOME']}/.npmrc`, `//registry.npmjs.org/:_authToken=${process.env['NPM_AUTH_TOKEN']}`);

const isDryRun = process.argv.indexOf('--dry') !== -1;
if (isDryRun) {
  console.log('Publish dry run');
}

const changedFiles = getChangedFilesInCommit('HEAD');

// Publish xterm if any files were changed outside of the addons directory
if (changedFiles.some(e => e.search(/^addons\//) === -1)) {
  checkAndPublishPackage(path.resolve(__dirname, '..'));
}

// Publish addons if any files were changed inside of the addon
const addonPackageDirs = [
  path.resolve(__dirname, '../addons/xterm-addon-attach'),
  path.resolve(__dirname, '../addons/xterm-addon-fit'),
  path.resolve(__dirname, '../addons/xterm-addon-search'),
  path.resolve(__dirname, '../addons/xterm-addon-web-links')
];
addonPackageDirs.forEach(p => {
  const addon = path.basename(p);
  if (changedFiles.some(e => e.indexOf(addon) !== -1)) {
    checkAndPublishPackage(p);
  }
});

function checkAndPublishPackage(packageDir) {
  const packageJson = require(path.join(packageDir, 'package.json'));

  // Determine if this is a stable or beta release
  const publishedVersions = getPublishedVersions(packageJson);
  const isStableRelease = publishedVersions.indexOf(packageJson.version) === -1;

  // Get the next version
  let nextVersion = isStableRelease ? packageJson.version : getNextBetaVersion(packageJson);
  console.log(`Publishing version: ${nextVersion}`);

  // Set the version in package.json
  const packageJsonFile = path.join(packageDir, 'package.json');
  packageJson.version = nextVersion;
  console.log(`Set version of ${packageJsonFile} to ${nextVersion}`);
  if (!isDryRun) {
    fs.writeFileSync(packageJsonFile, JSON.stringify(packageJson, null, 2));
  }

  // Publish
  const args = ['publish'];
  if (!isStableRelease) {
    args.push('--tag', 'beta');
  }
  console.log(`Spawn: npm ${args.join(' ')}`);
  if (!isDryRun) {
    const result = cp.spawnSync('npm', args, {
      cwd: packageDir,
      stdio: 'inherit'
    });
    if (result.status) {
      console.error(`Spawn exited with code ${result.status}`);
      process.exit(result.status);
    }
  }

  console.groupEnd();
}

function getNextBetaVersion(packageJson) {
  if (!/^[0-9]+\.[0-9]+\.[0-9]+$/.exec(packageJson.version)) {
    console.error('The package.json version must be of the form x.y.z');
    process.exit(1);
  }
  const tag = 'beta';
  const stableVersion = packageJson.version.split('.');
  const nextStableVersion = `${stableVersion[0]}.${parseInt(stableVersion[1]) + 1}.0`;
  const publishedVersions = getPublishedVersions(packageJson, nextStableVersion, tag);
  if (publishedVersions.length === 0) {
    return `${nextStableVersion}-${tag}1`;
  }
  const latestPublishedVersion = publishedVersions.sort((a, b) => {
    const aVersion = parseInt(a.substr(a.search(/[0-9]+$/)));
    const bVersion = parseInt(b.substr(b.search(/[0-9]+$/)));
    return aVersion > bVersion ? -1 : 1;
  })[0];
  const latestTagVersion = parseInt(latestPublishedVersion.substr(latestPublishedVersion.search(/[0-9]+$/)), 10);
  return `${nextStableVersion}-${tag}${latestTagVersion + 1}`;
}

function getPublishedVersions(packageJson, version, tag) {
  const versionsProcess = cp.spawnSync('npm', ['view', packageJson.name, 'versions', '--json']);
  const versionsJson = JSON.parse(versionsProcess.stdout);
  if (tag) {
    return versionsJson.filter(v => !v.search(new RegExp(`${version}-${tag}[0-9]+`)));
{'fraction_non_alphanumeric': 0.17558915679062798, 'fraction_numerical': 0.009807928075194115, 'mean_word_length': 4.524454477050414, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 16, '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': '7506588', 'n_tokens_mistral': 2444, 'n_tokens_neox': 2410, 'n_words': 497}