Class: ZendeskAppsTools::Command

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions, CommandHelpers
Defined in:
lib/zendesk_apps_tools/command.rb

Constant Summary collapse

DEFAULT_SERVER_IP =
'0.0.0.0'
DEFAULT_SERVER_PORT =
'4567'
DEFAULT_SERVER_PATH =
'./'
DEFAULT_CONFIG_PATH =
'./settings.yml'
DEFAULT_APP_ID =
0

Constants included from APIConnection

APIConnection::DEFAULT_URL_TEMPLATE, APIConnection::EMAIL_ERROR_MSG, APIConnection::EMAIL_REGEX, APIConnection::PROMPT_FOR_URL, APIConnection::SUBDOMAIN_VALIDATION_PATTERN, APIConnection::URL_ERROR_MSG, APIConnection::ZENDESK_URL_VALIDATION_PATTERN

Instance Method Summary collapse

Methods included from CommandHelpers

#cache, included, #setup_path

Methods included from PackageHelper

#app_package, #manifest, #zip

Methods included from Directory

#app_dir, #prompt_new_app_dir, #tmp_dir

Methods included from Deploy

#app_exists?, #check_job, #check_status, #deploy_app, #find_app_id, #install_app, #upload

Methods included from APIConnection

#get_connection, #prepare_api_auth

Methods included from Common

#get_password_from_stdin, #get_value_from_stdin, included, #json_or_die, #say_error, #say_error_and_exit

Instance Method Details

#cleanObject



143
144
145
146
147
148
149
150
151
# File 'lib/zendesk_apps_tools/command.rb', line 143

def clean
  require 'fileutils'

  setup_path(options[:path])

  return unless File.exist?(Pathname.new(File.join(app_dir, 'tmp')).to_s)

  FileUtils.rm(Dir["#{tmp_dir}/app-*.zip"])
end

#createObject



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/zendesk_apps_tools/command.rb', line 196

def create
  cache.clear
  setup_path(options[:path])
  @command = 'Create'

  unless options[:zipfile]
    app_name = manifest.name
  end
  app_name ||= get_value_from_stdin('Enter app name:')
  deploy_app(:post, '/api/apps.json', name: app_name)
  has_requirements = File.exist?(File.join(options[:path], 'requirements.json'))
  return unless options[:install]
  product_names(manifest).each do |product_name|
    say_status 'Install', "installing in #{product_name}"
    install_app(has_requirements, product_name, app_id: cache.fetch('app_id'), settings: settings.merge(name: app_name))
  end
end

#newObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/zendesk_apps_tools/command.rb', line 41

def new
  run_deprecation_checks('error', '1.0') if options[:v1]

  enter = ->(variable) { "Enter this app author's #{variable}:\n" }
  invalid = ->(variable) { "Invalid #{variable}, try again:" }
  @author_name  = get_value_from_stdin(enter.call('name'),
                                       error_msg: invalid.call('name'))
  @author_email = get_value_from_stdin(enter.call('email'),
                                       valid_regex: /^.+@.+\..+$/,
                                       error_msg: invalid.call('email'))
  @author_url   = get_value_from_stdin(enter.call('url'),
                                       valid_regex: %r{^https?://.+$},
                                       error_msg: invalid.call('url'),
                                       allow_empty: true)
  @app_name     = get_value_from_stdin("Enter a name for this new app:\n",
                                       error_msg: invalid.call('app name'))

  @iframe_location =
    if options[:scaffold]
      'assets/iframe.html'
    elsif options[:v1]
      '_legacy'
    else
      iframe_uri_text = 'Enter your iFrame URI or leave it blank to use'\
                        " a default local template page:\n"
      get_value_from_stdin(iframe_uri_text, allow_empty: true, default: 'assets/iframe.html')
    end

  prompt_new_app_dir

  directory_options =
  if options[:scaffold]
    # excludes everything but manifest.json
    { exclude_pattern: /^((?!manifest.json).)*$/ }
  elsif @iframe_location != 'assets/iframe.html'
    { exclude_pattern: /iframe.html/ }
  else
    {}
  end

  directory('app_template_iframe', @app_dir, directory_options)

  download_scaffold(@app_dir) if options[:scaffold]
end

#packageObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/zendesk_apps_tools/command.rb', line 121

def package
  return false unless validate

  setup_path(options[:path])

  if app_package.manifest.name
    warning = 'Please note that the name key of manifest.json is currently only used in development.'
    say_status 'warning', warning, :yellow
  end

  archive_path = File.join(tmp_dir, "app-#{Time.now.strftime('%Y%m%d%H%M%S')}.zip")

  archive_rel_path = relative_to_original_destination_root(archive_path)

  zip archive_path

  say_status 'package', "created at #{archive_rel_path}"
  true
end

#serverObject



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/zendesk_apps_tools/command.rb', line 164

def server
  setup_path(options[:path])
  if app_package.has_file?('assets/app.js')
    warning = 'Warning: creating assets/app.js causes zat server to behave badly.'
    say_status 'warning', warning, :yellow
  end

  require 'zendesk_apps_tools/server'
  ZendeskAppsTools::Server.tap do |server|
    server.set :settings_helper, settings_helper
    server.set :bind, options[:bind] if options[:bind]
    server.set :port, options[:port]
    server.set :root, options[:path]
    server.set :public_folder, File.join(options[:path], 'assets')
    server.set :parameters, settings
    server.set :app_id, options[:app_id]
    server.set :plan, [options[:plan], cache.fetch('plan')].reject(&:nil?).first

    host = options[:bind] || DEFAULT_SERVER_IP
    say "If apps fail to load, your browser may be blocking requests to http://#{host}:#{options[:port]}", :yellow
    say 'Make sure Local Network Access (LNA) is allowed to access your local apps server.', :yellow
    say 'Learn more: https://developer.zendesk.com/documentation/apps/zendesk-app-tools-zat/installing-and-using-zat/#testing-your-app-locally-in-a-browser', :yellow

    server.run!
  end
end

#updateObject



217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/zendesk_apps_tools/command.rb', line 217

def update
  cache.clear
  setup_path(options[:path])
  @command = 'Update'

  product_name = product_names(manifest).first
  app_id = cache.fetch('app_id') || find_app_id(product_name)
  app_url = "/api/v2/apps/#{app_id}.json"
  unless /\d+/ =~ app_id.to_s && app_exists?(app_id)
    say_error_and_exit "App ID not found. Please try running command with --clean or check your internet connection."
  end

  deploy_app(:put, app_url, {})
end

#validateObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/zendesk_apps_tools/command.rb', line 88

def validate
  require 'execjs'
  check_for_update
  setup_path(options[:path])
  begin
    errors = app_package.validate(marketplace: false)
  rescue ExecJS::RuntimeError
    error = "There was an error trying to validate this app.\n"
    if ExecJS.runtime.nil?
      error += 'Validation relies on a JavaScript runtime. See https://github.com/rails/execjs for a list of available runtimes.'
    elsif ExecJS.runtime.name == 'JScript'
      error += 'To validate on Windows, please install node from https://nodejs.org/'
    end
    say_error_and_exit error
  end
  valid = errors.none?

  if valid
    app_package.warnings.each { |w| say_status 'warning', w.to_s, :yellow }
    # clean when all apps are upgraded
    run_deprecation_checks unless options[:'unattended']
    say_status 'validate', 'OK'
  else
    errors.each { |e| say_status 'validate', e.to_s, :red }
  end

  @destination_stack.pop if options[:path]
  exit 1 unless valid
  true
end

#versionObject



233
234
235
# File 'lib/zendesk_apps_tools/command.rb', line 233

def version
  say ZendeskAppsTools::VERSION
end