<commit_before>fs = require 'fs'
zlib = require 'zlib'
path = require 'path'

request = require 'request'
tar = require 'tar'
temp = require 'temp'

temp.track()

downloadFileToLocation = (url, filename, callback) ->
  stream = fs.createWriteStream(filename)
  stream.on('end', callback)
  stream.on('error', callback)
  request(url).pipe(stream)

downloadTarballAndExtract = (url, location, callback) ->
  tempPath = temp.mkdirSync('apm-node-')
  stream = tar.Extract(path: tempPath)
  stream.on('end', callback.bind(this, tempPath))
  stream.on('error', callback)
  request(url).pipe(zlib.createGunzip()).pipe(stream)

copyNodeBinToLocation = (callback, version, targetFilename, fromDirectory) ->
  subDir = "node-#{version}-#{process.platform}-#{process.arch}"
  fromPath = path.join(fromDirectory, subDir, 'bin', 'node')
  fs.rename(fromPath, targetFilename, callback)

module.exports = (grunt) ->
  grunt.registerTask 'node', 'Download node binary', ->
    @requiresConfig("#{@name}.version")
    done = @async()

    {version} = grunt.config(@name)
    if process.platform is 'win32'
      arch = if process.arch is 'x64' then 'x64/' else ''
      downloadURL = "http://nodejs.org/dist/#{version}/#{arch}node.exe"
      filename = path.join('bin', "node_win32_#{process.arch}.exe")
    else
      downloadURL = "http://nodejs.org/dist/#{version}/node-#{version}-#{process.platform}-#{process.arch}.tar.gz"
      filename = path.join('bin', "node_#{process.platform}_#{process.arch}")

    if fs.existsSync(filename)
      done()
      return

    if process.platform is 'win32'
      downloadFileToLocation(downloadURL, filename, done)
    else
      next = copyNodeBinToLocation.bind(this, done, version, filename)
      downloadTarballAndExtract(downloadURL, filename, next)
<commit_msg>node uses x86, not ia32 in download filename<commit_after>fs = require 'fs'
zlib = require 'zlib'
path = require 'path'

request = require 'request'
tar = require 'tar'
temp = require 'temp'

temp.track()

downloadFileToLocation = (url, filename, callback) ->
  stream = fs.createWriteStream(filename)
  stream.on('end', callback)
  stream.on('error', callback)
  request(url).pipe(stream)

downloadTarballAndExtract = (url, location, callback) ->
  tempPath = temp.mkdirSync('apm-node-')
  stream = tar.Extract(path: tempPath)
  stream.on('end', callback.bind(this, tempPath))
  stream.on('error', callback)
  request(url).pipe(zlib.createGunzip()).pipe(stream)

copyNodeBinToLocation = (callback, version, targetFilename, fromDirectory) ->
  arch = if process.arch == 'ia32' then 'x86' else process.arch
  subDir = "node-#{version}-#{process.platform}-#{arch}"
  fromPath = path.join(fromDirectory, subDir, 'bin', 'node')
  fs.rename(fromPath, targetFilename, callback)

module.exports = (grunt) ->
  grunt.registerTask 'node', 'Download node binary', ->
    @requiresConfig("#{@name}.version")
    done = @async()

    {version} = grunt.config(@name)
    if process.platform is 'win32'
      arch = if process.arch is 'x64' then 'x64/' else ''
      downloadURL = "http://nodejs.org/dist/#{version}/#{arch}node.exe"
      filename = path.join('bin', "node_win32_#{process.arch}.exe")
    else
      arch = if process.arch == 'ia32' then 'x86' else process.arch
      downloadURL = "http://nodejs.org/dist/#{version}/node-#{version}-#{process.platform}-#{arch}.tar.gz"
      filename = path.join('bin', "node_#{process.platform}_#{process.arch}")

    if fs.existsSync(filename)
      done()
      return

    if process.platform is 'win32'
      downloadFileToLocation(downloadURL, filename, done)
    else
      next = copyNodeBinToLocation.bind(this, done, version, filename)
      downloadTarballAndExtract(downloadURL, filename, next)

{'fraction_non_alphanumeric': 0.1586834359111587, 'fraction_numerical': 0.008563018464008563, 'mean_word_length': 4.294617563739377, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 19, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1984809', 'n_tokens_mistral': 1216, 'n_tokens_neox': 1213, 'n_words': 253}