Class: Til::Initializer

Inherits:
Object
  • Object
show all
Includes:
Thor::Actions, Thor::Base
Defined in:
lib/til/services/initializer.rb

Instance Method Summary collapse

Instance Method Details

#create_notes_directoryObject



22
23
24
25
# File 'lib/til/services/initializer.rb', line 22

def create_notes_directory
  d = Settings.load.directory
  FileUtils.mkdir_p(File.expand_path(d))
end

#create_settings_file(settings_file_path = File.expand_path("~/.til.config")) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/til/services/initializer.rb', line 8

def create_settings_file(settings_file_path=File.expand_path("~/.til.config"))
  begin
    notes_directory = ask("What directory do you want your TIL notes to live in? (If you have no preference, press return to default to `~/til`.)\n>")
    github_repo = ask("Do you have a GitHub repo for your TIL notes? Paste the URL here if so; otherwise, press return.\n>")

    File.open(settings_file_path, "w") do |f|
      f.write(settings_hash(notes_directory, github_repo).to_json)
    end
  rescue Interrupt
    puts "Aborted!"
    abort
  end
end

#settings_hash(notes_directory, github_repo) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/til/services/initializer.rb', line 27

def settings_hash(notes_directory, github_repo)
  notes_directory = "~/til" if notes_directory.empty?
  settings_hash = {
    "directory" => notes_directory,
  }
  if !github_repo.empty?
    settings_hash["github_repo"] = github_repo.scan(/[^\/]*\/[^\/]*$/).first
  end
  settings_hash
end