class Bridgetown::Commands::New

Constants

DOCSURL

Attributes

created_site_dir[RW]

Public Class Methods

banner() click to toggle source
exit_on_failure?() click to toggle source
# File lib/bridgetown-core/commands/new.rb, line 42
def self.exit_on_failure?
  false
end
source_root() click to toggle source
# File lib/bridgetown-core/commands/new.rb, line 46
def self.source_root
  File.expand_path("../../site_template", __dir__)
end

Public Instance Methods

new_site() click to toggle source
# File lib/bridgetown-core/commands/new.rb, line 54
def new_site
  raise ArgumentError, "You must specify a path." if args.empty?

  new_site_path = File.expand_path(args.join(" "), Dir.pwd)
  @site_name = new_site_path.split(File::SEPARATOR).last

  if preserve_source_location?(new_site_path, options)
    say_status :conflict, "#{new_site_path} exists and is not empty.", :red
    Bridgetown.logger.abort_with "Ensure #{new_site_path} is empty or else " \
              "try again with `--force` to proceed and overwrite any files."
  end

  self.destination_root = self.class.created_site_dir = new_site_path

  say_status :create, new_site_path
  create_site new_site_path
  after_install new_site_path, args.join(" "), options
end

Protected Instance Methods

after_install(path, cli_path, options = {}) click to toggle source

After a new site has been created, print a success notification and then automatically execute bundle install from within the new site dir unless the user opts to skip 'bundle install'. rubocop:todo Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity

# File lib/bridgetown-core/commands/new.rb, line 109
def after_install(path, cli_path, options = {})
  git_init path

  @skipped_bundle = true # is set to false if bundle install worked
  bundle_install path unless options["skip-bundle"]

  @skipped_yarn = true
  yarn_install path unless options["skip-yarn"]

  invoke(Apply, [], options) if options[:apply]
  invoke(Configure, options[:configure].split(","), {}) if options[:configure]

  logger = Bridgetown.logger
  yarn_start = "yarn start"
  logger.info ""
  logger.info "Success!".green, "🎉 Your new Bridgetown site was" \
              " generated in #{cli_path.cyan}."
  if options["skip-yarn"]
    logger.info "You can now #{"cd".cyan} #{cli_path.cyan} to get started."
    logger.info "You'll probably also want to #{"yarn install".cyan}" \
                " to load in your frontend assets."
  else
    logger.info "You can now #{"cd".cyan} and run #{yarn_start.cyan} to get started."
  end
  logger.info "Then check out our online documentation for" \
              " next steps: #{DOCSURL.cyan}"

  logger.info "Bundle install skipped.".yellow if @skipped_bundle
  logger.info "Yarn install skipped.".yellow if @skipped_yarn
end
bundle_install(path) click to toggle source

rubocop:enable Metrics/CyclomaticComplexity rubocop:enable Metrics/PerceivedComplexity

# File lib/bridgetown-core/commands/new.rb, line 142
def bundle_install(path)
  unless Bridgetown.environment.test?
    require "bundler"
    Bridgetown.with_unbundled_env do
      inside(path) do
        run "bundle install", abort_on_failure: true
      end
    end
  end
  @skipped_bundle = false
rescue LoadError
  say_status :run, "Could not load Bundler. Bundle install skipped.", :red
rescue SystemExit
  say_status :run, "Problem occured while running bundle install.", :red
end
configure_postcss() click to toggle source
# File lib/bridgetown-core/commands/new.rb, line 99
def configure_postcss
  template("postcss.config.js.erb", "postcss.config.js")
  copy_file("frontend/styles/index.css")
end
configure_sass() click to toggle source
# File lib/bridgetown-core/commands/new.rb, line 95
def configure_sass
  copy_file("frontend/styles/index.scss")
end
create_site(new_site_path) click to toggle source
# File lib/bridgetown-core/commands/new.rb, line 79
def create_site(new_site_path)
  directory ".", ".", exclude_pattern: %r!\.erb|DS_Store$|\.(s[ac]|c)ss$!
  FileUtils.chmod_R "u+w", new_site_path

  template(
    "src/_posts/0000-00-00-welcome-to-bridgetown.md.erb",
    "src/_posts/#{Time.now.strftime("%Y-%m-%d")}-welcome-to-bridgetown.md"
  )
  template("Gemfile.erb", "Gemfile")
  template("package.json.erb", "package.json")
  template("frontend/javascript/index.js.erb", "frontend/javascript/index.js")

  options["use-postcss"] ? configure_postcss : configure_sass
  invoke(Webpack, ["setup"], {})
end
git_init(path) click to toggle source
# File lib/bridgetown-core/commands/new.rb, line 158
def git_init(path)
  unless Bridgetown.environment.test?
    inside(path) do
      initialize_new_repo
    end
  end
rescue SystemExit
  say_status :run, "Could not load git. git init skipped.", :red
end
preserve_source_location?(path, options) click to toggle source
# File lib/bridgetown-core/commands/new.rb, line 75
def preserve_source_location?(path, options)
  !options["force"] && Dir.exist?(path)
end
yarn_install(path) click to toggle source
# File lib/bridgetown-core/commands/new.rb, line 168
def yarn_install(path)
  unless Bridgetown.environment.test?
    inside(path) do
      run "yarn install", abort_on_failure: true
    end
  end
  @skipped_yarn = false
rescue SystemExit
  say_status :run, "Could not load yarn. yarn install skipped.", :red
end