<commit_before>#!/usr/bin/env node
var fs = require('fs')
var path = require('path')
var webpack = require('webpack')
var program = require('commander')
var parseAuthor = require('parse-author')
var chalk = require('chalk')
var objectAssign = require('object-assign')
var generateWebpackConfig = require('./utils/webpackConfig')
var getSkpmConfigFromPackageJSON = require('./utils/getSkpmConfigFromPackageJSON')
var getSketchVersion = require('./utils/getSketchVersion')

var buildEmojis = ['🔧', '🔨', '⚒', '🛠', '⛏', '🔩']
function randomBuildEmoji () {
  return buildEmojis[Math.floor(Math.random() * buildEmojis.length)]
}

program
  .description('Compile the javascript files into cocoascript')
  .usage('[options]')
  .option('-w, --watch', 'Watch and rebuild automatically')
  .option('-q, --quiet', 'Hide compilation warnings')
  .option('-r, --run', 'Run plugin after compiling')
  .parse(process.argv)

var packageJSON
try {
  packageJSON = require(path.join(process.cwd(), 'package.json'))
} catch (err) {
  console.error(chalk.red('error') + ' Error while reading the package.json file')
  console.error(err)
  process.exit(1)
}

var skpmConfig = getSkpmConfigFromPackageJSON(packageJSON)

if (!skpmConfig.main) {
  console.error(chalk.red('error') + ' Missing "skpm.main" fields in the package.json. Should point to the ".sketchplugin" file')
  process.exit(1)
}
if (!skpmConfig.manifest) {
  console.error(chalk.red('error') + ' Missing "skpm.manifest" fields in the package.json. Should point to the "manifest.json" file')
  process.exit(1)
}

var output = path.join(process.cwd(), skpmConfig.main)
var manifest = path.join(process.cwd(), skpmConfig.manifest)

if (!fs.existsSync(output)) {
  fs.mkdirSync(output)
}

if (!fs.existsSync(path.join(output, 'Contents'))) {
  fs.mkdirSync(path.join(output, 'Contents'))
}

if (!fs.existsSync(path.join(output, 'Contents', 'Sketch'))) {
  fs.mkdirSync(path.join(output, 'Contents', 'Sketch'))
}

var manifestFolder = path.dirname(manifest)

var webpackConfig = generateWebpackConfig(program, output, manifestFolder, skpmConfig)

var defaultAppcastURL = 'https://raw.githubusercontent.com/' + skpmConfig.repository + '/master/.appcast.xml'

function copyManifest (manifestJSON) {
  return new Promise(function (resolve, reject) {
    var copy = objectAssign({}, manifestJSON)
    copy.version = manifestJSON.version || skpmConfig.version
    copy.description = manifestJSON.description || skpmConfig.description
    copy.homepage = manifestJSON.homepage || skpmConfig.homepage
    copy.name = manifestJSON.name || skpmConfig.name
    copy.disableCocoaScriptPreprocessor = typeof manifestJSON.disableCocoaScriptPreprocessor === 'undefined' ? true : manifestJSON.disableCocoaScriptPreprocessor

    if (manifestJSON.appcast !== false && skpmConfig.appcast !== false) {
      copy.appcast = manifestJSON.appcast || skpmConfig.appcast || defaultAppcastURL
    } else {
      delete copy.appcast
    }

    if (!copy.author && skpmConfig.author) {
      var author = skpmConfig.author
      if (typeof skpmConfig.author === 'string') {
        author = parseAuthor(skpmConfig.author)
      }
      copy.author = author.name
      if (!copy.email && author.email) {
        copy.email = author.email
      }
    }

    copy.commands = manifestJSON.commands.map(function (command) {
      var basename = path.basename(command.script)
      return objectAssign({}, command, {script: basename})
    })

    fs.writeFile(path.join(output, 'Contents', 'Sketch', 'manifest.json'), JSON.stringify(copy, null, 2), function (err) {
      if (err) {
        reject(new Error('Error while writing the manifest: ' + err.message))
        return
      }
      resolve()
    })
  })
}

var counter = 0

function getCommands (manifestJSON) {
  var commandsAndHandlers = manifestJSON.commands.reduce(function (prev, c) {
    if (!prev[c.script]) {
      prev[c.script] = {
        script: c.script,
        handlers: [],
        identifiers: []
      }
    }
    if (c.handler) {
      prev[c.script].handlers.push(c.handler)
    } else if (c.handlers) {
      if (Array.isArray(c.handlers)) {
        c.handlers.forEach(h => {
          prev[c.script].handlers.push(h)
        })
      } else if (c.handlers.actions) {
        Object.keys(c.handlers.actions).forEach(k => {
          prev[c.script].handlers.push(c.handlers.actions[k])
        })
      } else {
        console.log(chalk.green('error') + ' Not sure what to do with the ' + c.identifier + ' handlers')
        process.exit(1)
      }
    }

    // always expose the default
    if (!prev[c.script].handlers.includes('onRun')) {
      prev[c.script].handlers.push('onRun')
    }
    if (c.identifier) {
      prev[c.script].identifiers.push(c.identifier)
    }
    return prev
  }, {})

  return Object.keys(commandsAndHandlers).map(function (k) {
    return commandsAndHandlers[k]
  })
}<commit_msg>Use `indexOf` instead of `includes`<commit_after>#!/usr/bin/env node
var fs = require('fs')
var path = require('path')
var webpack = require('webpack')
var program = require('commander')
var parseAuthor = require('parse-author')
var chalk = require('chalk')
var objectAssign = require('object-assign')
var generateWebpackConfig = require('./utils/webpackConfig')
var getSkpmConfigFromPackageJSON = require('./utils/getSkpmConfigFromPackageJSON')
var getSketchVersion = require('./utils/getSketchVersion')

var buildEmojis = ['🔧', '🔨', '⚒', '🛠', '⛏', '🔩']
function randomBuildEmoji () {
  return buildEmojis[Math.floor(Math.random() * buildEmojis.length)]
}

program
  .description('Compile the javascript files into cocoascript')
  .usage('[options]')
  .option('-w, --watch', 'Watch and rebuild automatically')
  .option('-q, --quiet', 'Hide compilation warnings')
  .option('-r, --run', 'Run plugin after compiling')
  .parse(process.argv)

var packageJSON
try {
  packageJSON = require(path.join(process.cwd(), 'package.json'))
} catch (err) {
  console.error(chalk.red('error') + ' Error while reading the package.json file')
  console.error(err)
  process.exit(1)
}

var skpmConfig = getSkpmConfigFromPackageJSON(packageJSON)

if (!skpmConfig.main) {
  console.error(chalk.red('error') + ' Missing "skpm.main" fields in the package.json. Should point to the ".sketchplugin" file')
  process.exit(1)
}
if (!skpmConfig.manifest) {
  console.error(chalk.red('error') + ' Missing "skpm.manifest" fields in the package.json. Should point to the "manifest.json" file')
  process.exit(1)
}

var output = path.join(process.cwd(), skpmConfig.main)
var manifest = path.join(process.cwd(), skpmConfig.manifest)

if (!fs.existsSync(output)) {
  fs.mkdirSync(output)
}

if (!fs.existsSync(path.join(output, 'Contents'))) {
  fs.mkdirSync(path.join(output, 'Contents'))
}

if (!fs.existsSync(path.join(output, 'Contents', 'Sketch'))) {
  fs.mkdirSync(path.join(output, 'Contents', 'Sketch'))
}

var manifestFolder = path.dirname(manifest)

var webpackConfig = generateWebpackConfig(program, output, manifestFolder, skpmConfig)

var defaultAppcastURL = 'https://raw.githubusercontent.com/' + skpmConfig.repository + '/master/.appcast.xml'

function copyManifest (manifestJSON) {
  return new Promise(function (resolve, reject) {
    var copy = objectAssign({}, manifestJSON)
    copy.version = manifestJSON.version || skpmConfig.version
    copy.description = manifestJSON.description || skpmConfig.description
    copy.homepage = manifestJSON.homepage || skpmConfig.homepage
    copy.name = manifestJSON.name || skpmConfig.name
    copy.disableCocoaScriptPreprocessor = typeof manifestJSON.disableCocoaScriptPreprocessor === 'undefined' ? true : manifestJSON.disableCocoaScriptPreprocessor

    if (manifestJSON.appcast !== false && skpmConfig.appcast !== false) {
      copy.appcast = manifestJSON.appcast || skpmConfig.appcast || defaultAppcastURL
    } else {
      delete copy.appcast
    }

    if (!copy.author && skpmConfig.author) {
      var author = skpmConfig.author
      if (typeof skpmConfig.author === 'string') {
        author = parseAuthor(skpmConfig.author)
      }
      copy.author = author.name
      if (!copy.email && author.email) {
        copy.email = author.email
      }
    }

    copy.commands = manifestJSON.commands.map(function (command) {
      var basename = path.basename(command.script)
      return objectAssign({}, command, {script: basename})
    })

    fs.writeFile(path.join(output, 'Contents', 'Sketch', 'manifest.json'), JSON.stringify(copy, null, 2), function (err) {
      if (err) {
        reject(new Error('Error while writing the manifest: ' + err.message))
        return
      }
      resolve()
    })
  })
}

var counter = 0

function getCommands (manifestJSON) {
  var commandsAndHandlers = manifestJSON.commands.reduce(function (prev, c) {
    if (!prev[c.script]) {
      prev[c.script] = {
        script: c.script,
        handlers: [],
        identifiers: []
      }
    }
    if (c.handler) {
      prev[c.script].handlers.push(c.handler)
    } else if (c.handlers) {
      if (Array.isArray(c.handlers)) {
        c.handlers.forEach(h => {
          prev[c.script].handlers.push(h)
        })
      } else if (c.handlers.actions) {
        Object.keys(c.handlers.actions).forEach(k => {
          prev[c.script].handlers.push(c.handlers.actions[k])
        })
      } else {
        console.log(chalk.green('error') + ' Not sure what to do with the ' + c.identifier + ' handlers')
        process.exit(1)
      }
    }

    // always expose the default
    if (prev[c.script].handlers.indexOf('onRun') == -1) {
      prev[c.script].handlers.push('onRun')
    }
    if (c.identifier) {
      prev[c.script].identifiers.push(c.identifier)
    }
    return prev
  }, {})

  return Object.keys(commandsAndHandlers).map(function (k) {
    return commandsAndHandlers[k]
  })
}
{'fraction_non_alphanumeric': 0.14963355048859936, 'fraction_numerical': 0.0013232899022801302, 'mean_word_length': 3.630065975494816, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '2049208', 'n_tokens_mistral': 3169, 'n_tokens_neox': 3115, 'n_words': 633}