<commit_before>const { EOL } = require('os')
const R = require('ramda')
const semver = require('semver')
const pSeries = require('p-series')
const TerminalUtils = require('constellate-dev-utils/modules/terminal')
const GitUtils = require('constellate-dev-utils/modules/git')
const AppUtils = require('../../utils/app')
const ProjectUtils = require('../../utils/projects')
const requestNextVersion = require('./requestNextVersion')

module.exports = function publish(projectsToPublish, options = {}) {
  const force = !!options.force

  const allProjects = ProjectUtils.getAllProjects()

  if (!GitUtils.isInitialized()) {
    TerminalUtils.error(
      'Constellate publishing requires that your project is initialised as a Git repository.',
    )
    process.exit(1)
  }

  const appConfig = AppUtils.getConfig()
  const enableGitPublishing = !R.path(['publishing', 'git', 'disable'], appConfig)
  const enableNPMPublishing = !R.path(['publishing', 'npm', 'disable'], appConfig)
  const lastVersionTag = AppUtils.getLastVersionTag()
  const lastVersion = lastVersionTag ? semver.clean(lastVersionTag) : '0.0.0'
  TerminalUtils.verbose(`Last version is ${lastVersion}`)

  // Ensure there are no uncommitted changes
  const projectsWithUncommitedChanges = allProjects.filter(ProjectUtils.hasUncommittedChanges)
  if (projectsWithUncommitedChanges.length > 0) {
    TerminalUtils.error(
      `The following projects have uncommitted changes within them. Please commit your changes and then try again.${EOL}${projectsWithUncommitedChanges
        .map(R.prop('name'))
        .join(', ')}`,
    )
    process.exit(1)
  }

  // Ensure on correct branch
  const targetBranch = R.path(['publishing', 'git', 'branch'], appConfig) || 'master'
  const targetRemote = R.path(['publishing', 'git', 'remote'], appConfig) || 'origin'
  const actualBranch = GitUtils.getCurrentBranch()
  if (targetBranch !== actualBranch) {
    try {
      GitUtils.checkout(targetBranch)
    } catch (err) {
      TerminalUtils.error(`Could not switch to the publish branch (${targetBranch})`)
    }
  }

  // Does the target remote exist?
  const remoteExists = GitUtils.doesRemoteExist(targetRemote)

  if (enableGitPublishing && !remoteExists) {
    TerminalUtils.error(`Target git remote '${targetRemote}' does not exist.`)
    process.exit(1)
  }

  if (enableGitPublishing && !GitUtils.isUpToDateWithRemote(targetRemote)) {
    TerminalUtils.error(
      `There are changes on remote '${targetRemote}' that need to be merged into your local repository.`,
    )
    process.exit(1)
  }

  // Ask for the next version
  return requestNextVersion(lastVersion).then((nextVersion) => {
    const isFirstPublish = lastVersion === '0.0.0'
    const nextVersionTag = `v${nextVersion}`

    const toPublish = isFirstPublish
      ? // We will publish all the ProjectUtils as this is our first publish.
        allProjects
      : force
        ? projectsToPublish
        : projectsToPublish.filter(ProjectUtils.changedSince(lastVersionTag))
    if (toPublish.length === 0) {
      TerminalUtils.info('There are no changes to be published.')
      return undefined
    }

    TerminalUtils.verbose(`Publishing [${projectsToPublish.map(R.prop('name')).join(', ')}`)

    // Prep the correct version number for each project
    const versions = Object.assign(
      {},
      allProjects.reduce(
        (acc, cur) => Object.assign(acc, { [cur.name]: ProjectUtils.getLastVersion(cur) }),
        {},
      ),
      toPublish.reduce((acc, cur) => Object.assign(acc, { [cur.name]: nextVersion }), {}),
    )

    // Build..
    return (
      pSeries(allProjects.map(project => () => ProjectUtils.buildProject(project, { versions })))
        // Then publish to NPM...
        .then(() => {
          if (enableNPMPublishing) {
            return pSeries(toPublish.map(project => () => ProjectUtils.publishToNPM(project)))
          }
          return undefined
        })
        // Then tag the repo...
        .then(() => GitUtils.addAnnotatedTag(nextVersionTag))
        // Then publish the git repo to the remote git repo (if enabled)
        .then(() => {
          if (enableGitPublishing) {
            GitUtils.pushWithTags(targetRemote, [nextVersionTag])
          }
        })
    )
  })
}
<commit_msg>Updates<commit_after>const { EOL } = require('os')
const R = require('ramda')
const semver = require('semver')
const pSeries = require('p-series')
const TerminalUtils = require('constellate-dev-utils/modules/terminal')
const GitUtils = require('constellate-dev-utils/modules/git')
const AppUtils = require('../../utils/app')
const ProjectUtils = require('../../utils/projects')
const requestNextVersion = require('./requestNextVersion')

module.exports = function publish(projectsToPublish, options = {}) {
  const force = !!options.force

  const allProjects = ProjectUtils.getAllProjects()

  if (!GitUtils.isInitialized()) {
    TerminalUtils.error(
      'Constellate publishing requires that your project is initialised as a Git repository.',
    )
    process.exit(1)
  }

  const appConfig = AppUtils.getConfig()
  const enableGitPublishing = !R.path(['publishing', 'git', 'disable'], appConfig)
  const enableNPMPublishing = !R.path(['publishing', 'npm', 'disable'], appConfig)
  const lastVersionTag = AppUtils.getLastVersionTag()
  const lastVersion = lastVersionTag ? semver.clean(lastVersionTag) : '0.0.0'
  TerminalUtils.verbose(`Last version is ${lastVersion}`)

  // Ensure there are no uncommitted changes
  const projectsWithUncommitedChanges = allProjects.filter(ProjectUtils.hasUncommittedChanges)
  if (projectsWithUncommitedChanges.length > 0) {
    TerminalUtils.error(
      `The following projects have uncommitted changes within them. Please commit your changes and then try again.${EOL}${projectsWithUncommitedChanges
        .map(R.prop('name'))
        .join(', ')}`,
    )
    process.exit(1)
  }

  // Ensure on correct branch
  const targetBranch = R.path(['publishing', 'git', 'branch'], appConfig) || 'master'
  const targetRemote = R.path(['publishing', 'git', 'remote'], appConfig) || 'origin'
  const actualBranch = GitUtils.getCurrentBranch()
  if (targetBranch !== actualBranch) {
    try {
      GitUtils.checkout(targetBranch)
    } catch (err) {
      TerminalUtils.error(`Could not switch to the publish branch (${targetBranch})`)
    }
  }

  // Does the target remote exist?
  const remoteExists = GitUtils.doesRemoteExist(targetRemote)

  if (enableGitPublishing && !remoteExists) {
    TerminalUtils.error(`Target git remote '${targetRemote}' does not exist.`)
    process.exit(1)
  }

  if (enableGitPublishing && !GitUtils.isUpToDateWithRemote(targetRemote)) {
    TerminalUtils.error(
      `There are changes on remote '${targetRemote}' that need to be merged into your local repository.`,
    )
    process.exit(1)
  }

  // Ask for the next version
  return requestNextVersion(lastVersion).then((nextVersion) => {
    const isFirstPublish = lastVersion === '0.0.0'
    const nextVersionTag = `v${nextVersion}`

    const toPublish = isFirstPublish
      ? // We will publish all the ProjectUtils as this is our first publish.
        allProjects
      : force
        ? projectsToPublish
        : projectsToPublish.filter(ProjectUtils.changedSince(lastVersionTag))
    if (toPublish.length === 0) {
      TerminalUtils.info('There are no changes to be published.')
      return undefined
    }

    TerminalUtils.verbose(`Publishing [${toPublish.map(R.prop('name')).join(', ')}]`)

    // Prep the correct version number for each project
    const versions = Object.assign(
      {},
      allProjects.reduce(
        (acc, cur) => Object.assign(acc, { [cur.name]: ProjectUtils.getLastVersion(cur) }),
        {},
      ),
      toPublish.reduce((acc, cur) => Object.assign(acc, { [cur.name]: nextVersion }), {}),
    )

    // Build..
    return (
      pSeries(allProjects.map(project => () => ProjectUtils.buildProject(project, { versions })))
        // Then publish to NPM...
        .then(() => {
          if (enableNPMPublishing) {
            return pSeries(toPublish.map(project => () => ProjectUtils.publishToNPM(project)))
          }
          return undefined
        })
        // Then tag the repo...
        .then(() => GitUtils.addAnnotatedTag(nextVersionTag))
        // Then publish the git repo to the remote git repo (if enabled)
        .then(() => {
          if (enableGitPublishing) {
            GitUtils.pushWithTags(targetRemote, [nextVersionTag])
          }
        })
    )
  })
}

{'fraction_non_alphanumeric': 0.13212404915155063, 'fraction_numerical': 0.0028086600351082502, 'mean_word_length': 3.344687341128622, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, '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': '251157', 'n_tokens_mistral': 2506, 'n_tokens_neox': 2467, 'n_words': 592}