<commit_before>'use strict'

const fs = require('fs-extra')
const handleTilde = require('./handle-tilde')
const path = require('path')
const template = require('lodash.template')

const CONFIG = require('../config')

module.exports = function (packagedAppPath, installDir) {
  const packagedAppFileName = path.basename(packagedAppPath)
  if (process.platform === 'darwin') {
    const installPrefix = installDir !== '' ? handleTilde(installDir) : path.join(path.sep, 'Applications')
    const installationDirPath = path.join(installPrefix, packagedAppFileName)
    if (fs.existsSync(installationDirPath)) {
      console.log(`Removing previously installed "${packagedAppFileName}" at "${installationDirPath}"`)
      fs.removeSync(installationDirPath)
    }
    console.log(`Installing "${packagedAppPath}" at "${installationDirPath}"`)
    fs.copySync(packagedAppPath, installationDirPath)
  } else if (process.platform === 'win32') {
    const installPrefix = installDir !== '' ? installDir : process.env.LOCALAPPDATA
    const installationDirPath = path.join(installPrefix, packagedAppFileName, 'app-dev')
    try {
      if (fs.existsSync(installationDirPath)) {
        console.log(`Removing previously installed "${packagedAppFileName}" at "${installationDirPath}"`)
        fs.removeSync(installationDirPath)
      }
      console.log(`Installing "${packagedAppPath}" at "${installationDirPath}"`)
      fs.copySync(packagedAppPath, installationDirPath)
    } catch (e) {
      console.log(`Administrator elevation required to install into "${installationDirPath}"`)
      const fsAdmin = require('fs-admin')
      return new Promise((resolve, reject) => {
        fsAdmin.recursiveCopy(packagedAppPath, installationDirPath, (error) => {
          error ? reject(error) : resolve()
        })
      })
    }
  } else {
    const atomExecutableName = CONFIG.channel === 'beta' ? 'atom-beta' : 'atom'
    const apmExecutableName = CONFIG.channel === 'beta' ? 'apm-beta' : 'apm'
    const appName = CONFIG.channel === 'beta' ? 'Atom Beta' : 'Atom'
    const appDescription = CONFIG.appMetadata.description
    const prefixDirPath = installDir !== '' ? handleTilde(installDir) : path.join('/usr', 'local')
    const shareDirPath = path.join(prefixDirPath, 'share')
    const installationDirPath = path.join(shareDirPath, atomExecutableName)
    const applicationsDirPath = path.join(shareDirPath, 'applications')
    const desktopEntryPath = path.join(applicationsDirPath, `${atomExecutableName}.desktop`)
    const binDirPath = path.join(prefixDirPath, 'bin')
    const atomBinDestinationPath = path.join(binDirPath, atomExecutableName)
    const apmBinDestinationPath = path.join(binDirPath, apmExecutableName)

    fs.mkdirpSync(applicationsDirPath)
    fs.mkdirpSync(binDirPath)

    if (fs.existsSync(installationDirPath)) {
      console.log(`Removing previously installed "${packagedAppFileName}" at "${installationDirPath}"`)
      fs.removeSync(installationDirPath)
    }
    console.log(`Installing "${packagedAppFileName}" at "${installationDirPath}"`)
    fs.copySync(packagedAppPath, installationDirPath)

    if (fs.existsSync(desktopEntryPath)) {
      console.log(`Removing existing desktop entry file at "${desktopEntryPath}"`)
      fs.removeSync(desktopEntryPath)
    }
    console.log(`Writing desktop entry file at "${desktopEntryPath}"`)
    const iconPath = path.join(CONFIG.repositoryRootPath, 'resources', 'app-icons', CONFIG.channel, 'png', '1024.png')
    const desktopEntryTemplate = fs.readFileSync(path.join(CONFIG.repositoryRootPath, 'resources', 'linux', 'atom.desktop.in'))
    const desktopEntryContents = template(desktopEntryTemplate)({
      appName,
      appFileName: atomExecutableName,
      description: appDescription,
      installDir: prefixDirPath,
      iconPath
    })
    fs.writeFileSync(desktopEntryPath, desktopEntryContents)

    if (fs.existsSync(atomBinDestinationPath)) {
      console.log(`Removing existing executable at "${atomBinDestinationPath}"`)
      fs.removeSync(atomBinDestinationPath)
    }
    console.log(`Copying atom.sh to "${atomBinDestinationPath}"`)
    fs.copySync(path.join(CONFIG.repositoryRootPath, 'atom.sh'), atomBinDestinationPath)

    try {
      fs.lstatSync(apmBinDestinationPath)
      console.log(`Removing existing executable at "${apmBinDestinationPath}"`)
      fs.removeSync(apmBinDestinationPath)
    } catch (e) { }
    console.log(`Symlinking apm to "${apmBinDestinationPath}"`)
    fs.symlinkSync(path.join('..', 'share', atomExecutableName, 'resources', 'app', 'apm', 'node_modules', '.bin', 'apm'), apmBinDestinationPath)<commit_msg>Remove duplicate code in install script<commit_after>'use strict'

const fs = require('fs-extra')
const handleTilde = require('./handle-tilde')
const path = require('path')
const template = require('lodash.template')

const CONFIG = require('../config')

function install (installationDirPath, packagedAppFileName, packagedAppPath) {
  if (fs.existsSync(installationDirPath)) {
    console.log(`Removing previously installed "${packagedAppFileName}" at "${installationDirPath}"`)
    fs.removeSync(installationDirPath)
  }

  console.log(`Installing "${packagedAppFileName}" at "${installationDirPath}"`)
  fs.copySync(packagedAppPath, installationDirPath)
}


module.exports = function (packagedAppPath, installDir) {
  const packagedAppFileName = path.basename(packagedAppPath)
  if (process.platform === 'darwin') {
    const installPrefix = installDir !== '' ? handleTilde(installDir) : path.join(path.sep, 'Applications')
    const installationDirPath = path.join(installPrefix, packagedAppFileName)
    install(installationDirPath, packagedAppFileName, packagedAppPath)
  } else if (process.platform === 'win32') {
    const installPrefix = installDir !== '' ? installDir : process.env.LOCALAPPDATA
    const installationDirPath = path.join(installPrefix, packagedAppFileName, 'app-dev')
    try {
      install(installationDirPath, packagedAppFileName, packagedAppPath)
    } catch (e) {
      console.log(`Administrator elevation required to install into "${installationDirPath}"`)
      const fsAdmin = require('fs-admin')
      return new Promise((resolve, reject) => {
        fsAdmin.recursiveCopy(packagedAppPath, installationDirPath, (error) => {
          error ? reject(error) : resolve()
        })
      })
    }
  } else {
    const atomExecutableName = CONFIG.channel === 'beta' ? 'atom-beta' : 'atom'
    const apmExecutableName = CONFIG.channel === 'beta' ? 'apm-beta' : 'apm'
    const appName = CONFIG.channel === 'beta' ? 'Atom Beta' : 'Atom'
    const appDescription = CONFIG.appMetadata.description
    const prefixDirPath = installDir !== '' ? handleTilde(installDir) : path.join('/usr', 'local')
    const shareDirPath = path.join(prefixDirPath, 'share')
    const installationDirPath = path.join(shareDirPath, atomExecutableName)
    const applicationsDirPath = path.join(shareDirPath, 'applications')
    const desktopEntryPath = path.join(applicationsDirPath, `${atomExecutableName}.desktop`)
    const binDirPath = path.join(prefixDirPath, 'bin')
    const atomBinDestinationPath = path.join(binDirPath, atomExecutableName)
    const apmBinDestinationPath = path.join(binDirPath, apmExecutableName)

    fs.mkdirpSync(applicationsDirPath)
    fs.mkdirpSync(binDirPath)

    install(installationDirPath, packagedAppFileName, packagedAppPath)

    if (fs.existsSync(desktopEntryPath)) {
      console.log(`Removing existing desktop entry file at "${desktopEntryPath}"`)
      fs.removeSync(desktopEntryPath)
    }
    console.log(`Writing desktop entry file at "${desktopEntryPath}"`)
    const iconPath = path.join(CONFIG.repositoryRootPath, 'resources', 'app-icons', CONFIG.channel, 'png', '1024.png')
    const desktopEntryTemplate = fs.readFileSync(path.join(CONFIG.repositoryRootPath, 'resources', 'linux', 'atom.desktop.in'))
    const desktopEntryContents = template(desktopEntryTemplate)({
      appName,
      appFileName: atomExecutableName,
      description: appDescription,
      installDir: prefixDirPath,
      iconPath
    })
    fs.writeFileSync(desktopEntryPath, desktopEntryContents)

    if (fs.existsSync(atomBinDestinationPath)) {
      console.log(`Removing existing executable at "${atomBinDestinationPath}"`)
      fs.removeSync(atomBinDestinationPath)
    }
    console.log(`Copying atom.sh to "${atomBinDestinationPath}"`)
    fs.copySync(path.join(CONFIG.repositoryRootPath, 'atom.sh'), atomBinDestinationPath)

    try {
      fs.lstatSync(apmBinDestinationPath)
      console.log(`Removing existing executable at "${apmBinDestinationPath}"`)
      fs.removeSync(apmBinDestinationPath)
    } catch (e) { }
    console.log(`Symlinking apm to "${apmBinDestinationPath}"`)
    fs.symlinkSync(path.join('..', 'share', atomExecutableName, 'resources', 'app', 'apm', 'node_modules', '.bin', 'apm'), apmBinDestinationPath)
{'fraction_non_alphanumeric': 0.12751150521944102, 'fraction_numerical': 0.0013469525199236727, 'mean_word_length': 4.781959766385464, 'pattern_counts': {'":': 0, '<': 3, '<?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': '648927', 'n_tokens_mistral': 2586, 'n_tokens_neox': 2538, 'n_words': 508}