Class: Jscompiler::Cli

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.source_rootObject



32
33
34
# File 'lib/jscompiler/cli.rb', line 32

def source_root
  File.expand_path('../../',__FILE__)
end

Instance Method Details

#compileObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/jscompiler/cli.rb', line 90

def compile
  unless Jscompiler::Config.configured?
    say("This folder has not yet been configured to run jsc commands. Please run 'jsc init'.")
    return
  end

  if @options[:group]
    unless Jscompiler::Config.groups.keys.include?(@options[:group])
      puts("Error: invalid group name")
      return
    end

    Jscompiler::Commands::Base.compile_group(@options[:group], @options)
    return
  end

  Jscompiler::Config.groups.keys.each do |group|  
    Jscompiler::Commands::Base.compile_group(group, @options)
  end
end

#groupObject



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
# File 'lib/jscompiler/cli.rb', line 57

def group
  unless Jscompiler::Config.configured?
    say("This folder has not yet been configured to run jsc commands. Please run 'jsc init'.")
    return
  end

  @group_name = ask("What would you like to name this group?")
  @root = Jscompiler::Config.source_root
  ask_for_compiler(:default => true)
  ask_for_file_order
  ask_for_output_path

  Jscompiler::Config.config['groups'][@group_name] = {
    "files" => @files,
    "output" => {"path" => @output}, 
  }

  if @compiler != "default"
    Jscompiler::Config.config['groups'][@group_name]["compiler"] = {"name" => @compiler}
  end

  Jscompiler::Config.update_config

  say("Configuration has been saved in .jscompiler file. You now can run the compile commands.")
end

#initObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/jscompiler/cli.rb', line 39

def init
  say("Initializing your project...")

  ask_for_compiler

  @root = ask("\r\nWhat is your files source folder (a relative path from the current directory)?")
  @root = '.' if @root == ''

  ask_for_file_order
  ask_for_output_path

  template 'templates/jscompiler.yml.erb', "./#{Jscompiler::Config.path}"

  say("Configuration has been saved in .jscompiler file. You now can run the compile commands.")      
end

#watchObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/jscompiler/cli.rb', line 113

def watch
  unless Jscompiler::Config.configured?
    say("This folder has not yet been configured to run jsc commands. Please run 'jsc init'.")
    return
  end

  say("Started monitoring #{Jscompiler::Config.source_root} folder. To stop use Ctrl+C.")

  monitor = FSSM::Monitor.new
  monitor.path("./#{Jscompiler::Config.source_root}/", '**/*.js') do
    update do |base, relative|
      puts("#{relative} file changed")
      compile(relative)
    end

    delete do |base, relative|
      puts("#{relative} deleted")
      compile(relative)
    end

    def compile(relative)
      Jscompiler::Config.groups.keys.each do |group|  
        full_path = Jscompiler::Config.source_root == '.' ? relative : "#{Jscompiler::Config.source_root}/#{relative}"
        next unless Jscompiler::Config.files(group).include?(full_path)
        Jscompiler::Commands::Base.compile_group(group)
      end
    end        
  end

  monitor.run
end