<commit_before>paths = require "./paths"
path = require "path"
sbawn = require("./sbawn")
chalk = require "chalk"

Help = require "./Help"

os = require "os"

class SafariDebug
  SafariDebugError: class SafariDebugError extends steroidsCli.SteroidsError

  constructor: (@callBackOnExit) ->  # @callBackOnExit is invoked when this class' methods exit - typically used to redisplay the interactive prompt.

  run: (options={}) =>
    new Promise (resolve, reject) =>
      unless steroidsCli.host.os.isOSX()
        reject new steroidsCli.PlatformError
        return

      if options.path
        @open(options.path)
      else
        console.log "Fetching location paths from Safari:\n"
        @callBackOnExit = ->
          console.log "Use path with --location=<part of the location path>"

        @listViews()

      resolve()

  listViews: ()=>
    new Promise (resolve, reject) =>
      getViews = if steroidsCli.host.os.osx.isYosemite()
        @runJavaScript "yosemite-safari.js", ["safari", "listviews"]
      else
        @runAppleScript "SafariDebugWebViewLister.scpt"

      getViews.then (viewList) =>
        resolve viewList
      .catch (error) =>
        reject error

  open: (argument) =>
    new Promise (resolve, reject) =>
      @runAppleScript("openSafariDevMenu.scpt", argument).then ->
        resolve()
      .catch (error) ->
        reject error

  runJavaScript: (scriptFileName, argument) =>
    new Promise (resolve, reject) =>
      views = []
      @ensureAssistiveAccess().then( =>
        scriptPath = path.join paths.scriptsDir, scriptFileName

        args = if argument?
          [scriptPath].concat argument
        else
          [scriptPath]

        session = sbawn
          cmd: "osascript"
          args: args

        session.on "exit", () =>
          steroidsCli.debug "SafariDebug started and killed."
          steroidsCli.debug "stderr: " + session.stderr
          steroidsCli.debug "stdout: " + session.stdout<commit_msg>(fix) safari debug only for OS X<commit_after>paths = require "./paths"
path = require "path"
sbawn = require("./sbawn")
chalk = require "chalk"

Help = require "./Help"

os = require "os"

class SafariDebug
  SafariDebugError: class SafariDebugError extends steroidsCli.SteroidsError

  constructor: (@callBackOnExit) ->  # @callBackOnExit is invoked when this class' methods exit - typically used to redisplay the interactive prompt.

  run: (options={}) =>
    new Promise (resolve, reject) =>
      unless steroidsCli.host.os.isOSX()
        reject new steroidsCli.PlatformError
        return

      if options.path
        @open(options.path)
      else
        console.log "Fetching location paths from Safari:\n"
        @callBackOnExit = ->
          console.log "Use path with --location=<part of the location path>"

        @listViews()

      resolve()

  listViews: ()=>
    new Promise (resolve, reject) =>
      unless steroidsCli.host.os.isOSX()
        reject new steroidsCli.PlatformError
        return

      getViews = if steroidsCli.host.os.osx.isYosemite()
        @runJavaScript "yosemite-safari.js", ["safari", "listviews"]
      else
        @runAppleScript "SafariDebugWebViewLister.scpt"

      getViews.then (viewList) =>
        resolve viewList
      .catch (error) =>
        reject error

  open: (argument) =>
    new Promise (resolve, reject) =>
      unless steroidsCli.host.os.isOSX()
        reject new steroidsCli.PlatformError
        return

      @runAppleScript("openSafariDevMenu.scpt", argument).then ->
        resolve()
      .catch (error) ->
        reject error

  runJavaScript: (scriptFileName, argument) =>
    new Promise (resolve, reject) =>
      views = []
      @ensureAssistiveAccess().then( =>
        scriptPath = path.join paths.scriptsDir, scriptFileName

        args = if argument?
          [scriptPath].concat argument
        else
          [scriptPath]

        session = sbawn
          cmd: "osascript"
          args: args

        session.on "exit", () =>
          steroidsCli.debug "SafariDebug started and killed."
          steroidsCli.debug "stderr: " + session.stderr
          steroidsCli.debug "stdout: " + session.stdout
{'fraction_non_alphanumeric': 0.10853454458522592, 'fraction_numerical': 0.0, 'mean_word_length': 2.4837635303913403, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 37, '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': '4205307', 'n_tokens_mistral': 1247, 'n_tokens_neox': 1186, 'n_words': 317}