Sha256: e85ffc4a825236834a3c2ef6f010bb92b03efeccfd8536a3c00c1c658596b632

Contents?: true

Size: 1.56 KB

Versions: 4

Compression:

Stored size: 1.56 KB

Contents

module RubyLambda
  class Init

    TEMPLATE_DIR = "#{__dir__}/templates"

    def initialize(current_directory)
      @current_directory  = current_directory
      @shell = Thor::Base.shell.new
    end

    def run(mute: false)
      @mute = mute

      unless File.writable?(@current_directory)
        @shell.say "Can not create files as the current directory is not writable: #{@current_directory}", :red
        return
      end

      move_template_files
      rename_env_file
      update_function_name
    end

    private

    def move_template_files
      Dir.foreach(TEMPLATE_DIR) do |template_file_name|
        next if template_file_name == '.' or template_file_name == '..'

        init_file = File.join(@current_directory, template_file_name)

        template_file_path = File.join(TEMPLATE_DIR, template_file_name)

        if File.exist?(init_file)
          @shell.say_status("Skipped:", "#{template_file_name} file already exists at #{File.expand_path(init_file)}", :yellow) unless @mute
        else
          FileUtils.cp(template_file_path, init_file)

          @shell.say_status("Created:", "#{template_file_name}", :green) unless @mute
        end
      end
    end

    def rename_env_file
      File.rename("#{@current_directory}/env", "#{@current_directory}/.env")
    end

    def update_function_name
      config_file = "#{@current_directory}/config.yml"

      config_data = YAML.load_file config_file
      config_data['function_name'] = @current_directory.split('/').last

      File.open(config_file, 'w') { |f| YAML.dump(config_data, f) }
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ruby_lambda-0.5.4 lib/ruby_lambda/init.rb
ruby_lambda-0.5.2 lib/ruby_lambda/init.rb
ruby_lambda-0.5.0 lib/ruby_lambda/init.rb
ruby_lambda-0.4.0 lib/ruby_lambda/init.rb