Class: ShopifyTheme::Cli

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/shopify_theme/cli.rb

Constant Summary collapse

IGNORE =
%w(config.yml)
DEFAULT_WHITELIST =
%w(layout/ assets/ config/ snippets/ templates/ locales/)
TIMEFORMAT =
"%H:%M:%S"

Instance Method Summary collapse

Instance Method Details

#bootstrap(api_key = nil, password = nil, store = nil, theme_name = nil) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/shopify_theme/cli.rb', line 75

def bootstrap(api_key=nil, password=nil, store=nil, theme_name=nil)
  ShopifyTheme.config = {:api_key => api_key, :password => password, :store => store, :whitelist_files => Filters::Whitelist::DEFAULT_WHITELIST}
  check(true)

  theme_name ||= 'Timber'
  say("Registering #{theme_name} theme on #{store}", :green)
  theme = ShopifyTheme.upload_timber(theme_name, options[:version])

  say("Creating directory named #{theme_name}", :green)
  empty_directory(theme_name)

  say("Saving configuration to #{theme_name}", :green)
  ShopifyTheme.config.merge!(theme_id: theme['id'])
  create_file("#{theme_name}/config.yml", ShopifyTheme.config.to_yaml)

  say("Downloading #{theme_name} assets from Shopify")
  Dir.chdir(theme_name)
  download()
rescue Releases::VersionError => e
  say(e.message, :red)
end

#check(exit_on_failure = false) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/shopify_theme/cli.rb', line 40

def check(exit_on_failure=false)
  result = APIChecker.new(ShopifyTheme).test_connectivity

  if result.api_down?
    say("Cannot connect to Shopify. API appears to be down", :red)
    say("Visit http://status.shopify.com for more details", :yello)
  elsif result.invalid_config?
    say("Cannot connect to Shopify. Configuration is invalid.", :red)
    say("Verify that your API key, password and domain are correct.", :yellow)
    say("Visit https://github.com/shopify/shopify_theme#configuration for more details", :yellow)
    say("If your shop domain is correct, the following URL should take you to the Private Apps page for the shop:", :yellow)
    say("  https://#{config[:store]}/admin/apps/private", :yellow)
  else
    say("Shopify API is accessible and configuration is valid", :green) unless exit_on_failure
  end

  exit(1) if result.cannot_access_api? && exit_on_failure
end

#configure(api_key = nil, password = nil, store = nil, theme_id = nil) ⇒ Object



60
61
62
63
64
# File 'lib/shopify_theme/cli.rb', line 60

def configure(api_key=nil, password=nil, store=nil, theme_id=nil)
  config = {:api_key => api_key, :password => password, :store => store, :theme_id => theme_id, :whitelist_files => Filters::Whitelist::DEFAULT_WHITELIST}
  create_file('config.yml', config.to_yaml)
  check(true)
end

#configure_oauth(access_token = nil, store = nil, theme_id = nil) ⇒ Object



67
68
69
70
# File 'lib/shopify_theme/cli.rb', line 67

def configure_oauth(access_token=nil, store=nil, theme_id=nil)
  config = {:access_token => access_token, :store => store, :theme_id => theme_id}
  create_file('config.yml', config.to_yaml)
end

#download(*keys) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/shopify_theme/cli.rb', line 100

def download(*keys)
  check(true)
  assets = assets_for(keys, ShopifyTheme.asset_list)

  if options['exclude']
    assets = assets.delete_if { |asset| asset =~ Regexp.new(options['exclude']) }
  end

  assets.each do |asset|
    download_asset(asset)
    say("#{ShopifyTheme.api_usage} Downloaded: #{asset}", :green) unless options['quiet']
  end
  say("Done.", :green) unless options['quiet']
end

#open(*keys) ⇒ Object



116
117
118
119
120
# File 'lib/shopify_theme/cli.rb', line 116

def open(*keys)
  if Launchy.open shop_theme_url
    say("Done.", :green)
  end
end

#remove(*keys) ⇒ Object



156
157
158
159
160
161
162
# File 'lib/shopify_theme/cli.rb', line 156

def remove(*keys)
  check(true)
  keys.each do |key|
    delete_asset(key, options['quiet'])
  end
  say("Done.", :green) unless options['quiet']
end

#replace(*keys) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/shopify_theme/cli.rb', line 136

def replace(*keys)
  check(true)
  say("Are you sure you want to completely replace your shop theme assets? This is not undoable.", :yellow)
  if ask("Continue? (Y/N): ") == "Y"
    # only delete files on remote that are not present locally
    # files present on remote and present locally get overridden anyway
    remote_assets = keys.empty? ? (ShopifyTheme.asset_list - local_assets_list) : keys
    remote_assets.each do |asset|
      delete_asset(asset, options['quiet']) unless ShopifyTheme.ignore_files.any? { |regex| regex =~ asset }
    end
    local_assets = keys.empty? ? local_assets_list : keys
    local_assets.each do |asset|
      send_asset(asset, options['quiet'])
    end
    say("Done.", :green) unless options['quiet']
  end
end

#systeminfoObject



188
189
190
191
192
193
194
195
196
197
# File 'lib/shopify_theme/cli.rb', line 188

def systeminfo
  ruby_version = "#{RUBY_VERSION}"
  ruby_version += "-p#{RUBY_PATCHLEVEL}" if RUBY_PATCHLEVEL
  puts "Ruby: v#{ruby_version}"
  puts "Operating System: #{RUBY_PLATFORM}"
  %w(Thor Listen HTTParty Launchy).each do |lib|
    require "#{lib.downcase}/version"
    puts "#{lib}: v" +  Kernel.const_get("#{lib}::VERSION")
  end
end

#upload(*keys) ⇒ Object



124
125
126
127
128
129
130
131
132
# File 'lib/shopify_theme/cli.rb', line 124

def upload(*keys)
  check(true)
  assets = assets_for(keys, local_assets_list)
  assets = keys.empty? ? local_assets_list : keys
  assets.each do |asset|
    send_asset(asset, options['quiet'])
  end
  say("Done.", :green) unless options['quiet']
end

#watchObject



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/shopify_theme/cli.rb', line 167

def watch
  check(true)
  puts "Watching current folder: #{Dir.pwd}"
  watcher do |filename, event|
    filename = filename.gsub("#{Dir.pwd}/", '')
    
    next unless local_assets_list.include?(filename)
    
    action = if [:changed, :new].include?(event)
      :send_asset
    elsif event == :delete && !options['keep_files']
      :delete_asset
    else
      raise NotImplementedError, "Unknown event -- #{event} -- #{filename}"
    end

    send(action, filename, options['quiet'])
  end
end