Class: NationSyncThor

Inherits:
Thor
  • Object
show all
Defined in:
lib/nationsync/cli.rb

Constant Summary collapse

HL =
HighLine.new

Instance Method Summary collapse

Instance Method Details

#clearObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/nationsync/cli.rb', line 110

def clear
  setup_config
  setup_api
  unless @config["asset_keys"]
    puts "Please run `nationsync fetch` to get an up-to-date asset list."
    return
  end
  print "Removing #{@config["asset_keys"].length.to_s} files..."
  @config["asset_keys"].each do |fn|
    File.delete(File.join(cwd, fn))
  end
  @config.delete "asset_keys"
  save_config!
  puts " Done"
end

#fetchObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/nationsync/cli.rb', line 127

def fetch
  setup_config
  setup_api
  unless @config["theme_id"]
    self.pick_theme
  end
  print "Fetching assets for theme #{@config["theme_id"]}..."
  assets = @api.theme_assets(@config["theme_id"])
  puts " Done"
  # if (window.is_binary(file_path)) {
  #   attachment = Ti.Codec.encodeBase64(content);
  # } else {
  #   value = content.toString();
  # }
  print "Writing files..."
  assets.each do |a|
    # puts "#{a["key"]}: #{a.keys.inspect}"
    fn = a["key"]
    data = a["value"]
    if a["attachment"]
      data = Base64.decode64(a["attachment"])
    end
    File.open(File.join(cwd, fn), 'w') {|f| f.write data }
  end
  puts " Done. (#{assets.length.to_s} Assets)"
  @config["asset_keys"] = assets.map {|a| a["key"] }
  save_config!
end

#initObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/nationsync/cli.rb', line 35

def init
  puts "Creating .nbconfig1"
  domain   = HL.ask("Domain: ").to_s
  email    = HL.ask("Email: ").to_s
  password = HL.ask("Password: ") {|q| q.echo = false }.to_s
  
  print "Fetching credentials..."
  auth = NationSync::Auth.new(domain, email, password)
  auth.authenticate!
  puts " Done"
  
  @config = {
    "domain" => domain,
    "email"  => email,
    "access_token" => auth.access_token,
    "session_id" => auth.session_id
  }
  save_config!
  puts "Created: #{fn}"
end

#pick_themeObject



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/nationsync/cli.rb', line 157

def pick_theme
  setup_config
  setup_api
  print "Fetching themes for selection..."
  themes = @api.themes
  puts " Done"
  # pp themes
  
  id = HL.choose do |m|
    m.prompt = "Select a theme: "
    
    themes.each do |t|
      # m.choice(t["id"])
      s = t["name"] + HL.color(" (#{t["id"]})", :gray)
      unless t["sites"].to_s.empty?
        s += "\n     (Site: " + t["sites"].to_s + ")"
      end
      m.choice(s) { t["id"] }
    end
  end
  @config["theme_id"] = id
  save_config!
  puts "Using theme #{id}"
end

#watchObject



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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/nationsync/cli.rb', line 57

def watch
  setup_config
  setup_api
  unless @config["asset_keys"]
    puts "Please run `nationsync fetch` to get an up-to-date asset list."
    return
  end
  listener = Listen.to(Dir.getwd) do |modified, added, removed|
    modified.each do |path|
      fn = File.basename(path)
      if !@config["asset_keys"].include?(fn)
        puts "- Skipping #{fn}"
      end
      type = MIME::Types.type_for(fn).first
      is_binary = type && type.binary?
      if is_binary
        resp = @api.theme_asset_put_attachment(@config["theme_id"], fn, File.read(path))
      else
        resp = @api.theme_asset_put(@config["theme_id"], fn, File.read(path))
      end
      
      if resp.status == 200
        if resp.body["success"].to_s == "true"
          puts "- Updated #{fn}" + (is_binary ? " (binary)" : "")
        else
          puts "- #{HL.color("Error", :red) } updating #{fn}:"
          errors = resp.body["errors"]
          if errors.is_a? Array
            errors.each {|e| puts "    #{e}"}
          else
            puts "    #{errors.to_s}"
          end
        end
      else
        puts "- #{HL.color("Error", :red) } updating #{fn}:"
        puts "    #{resp.body.to_s}"
      end
    end
    added.each do |path|
      fn = File.basename(path)
      puts "- #{HL.color("Warning:", :red)} File addition not supported yet (#{fn})"
    end
    removed.each do |path|
      fn = File.basename(path)
      puts "- #{HL.color("Warning:", :red)} File deletion not supported yet (#{fn})"
    end
  end#listener
  listener.start
  puts "Listening for changes in #{Dir.getwd}"
  sleep
end