Sha256: 194920631673742f79360c331aa6bc3e64fffd64b8df2388fd34091a31ad9e30

Contents?: true

Size: 1.48 KB

Versions: 1

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: false

require 'fileutils'
require 'thor'

module Frontman
  class CLI < Thor
    option :template
    option :force, type: :boolean
    desc 'init', 'Bootstrap a new Frontman project'
    def init(path)
      template = options[:template] || 'default'
      unless template_exists?(template)
        raise "Template #{template} does not exist!"
      end

      target_dir = File.join(Dir.pwd, path == '.' ? '' : path)

      unless allowed_to_modify_dir?(target_dir)
        say 'Not bootstrapping new Frontman project'
        return
      end

      copy_template(template, target_dir)

      command = path == '.' ? '' : "cd #{path} && "
      command += 'bundle exec frontman serve'

      say "Your project is ready. Run `#{command}` and start developing!"
    end

    private

    def copy_template(template, dest)
      FileUtils.cp_r(
        "#{path_to_template(template)}/.",
        dest
      )
    end

    def allowed_to_modify_dir?(dir)
      return true if options[:force]
      return true if !Dir.exist?(dir) || Dir.empty?(dir)

      say 'This folder already contains files. '
      say 'Initializing a new Frontman project here may override these files.'
      answer = ask('Are you sure you want to continue? [y/N]')

      answer.to_s.downcase == 'y'
    end

    def template_exists?(template)
      Dir.exist?(path_to_template(template))
    end

    def path_to_template(template)
      File.join(__dir__, '../../../project-templates', template)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
frontman-ssg-0.0.4 lib/frontman/commands/init.rb