class Wordless::WordlessCLI

Constants

DEFAULT_PATHS
GLOBAL_NODE_MODULES

Attributes

options[R]
thor[R]

Public Class Methods

new(thor, options = {}) click to toggle source
# File lib/wordless/wordless_cli.rb, line 23
def initialize(thor, options = {})
  @options = options
  @thor = thor
end

Public Instance Methods

install_global_node_modules() click to toggle source
# File lib/wordless/wordless_cli.rb, line 75
def install_global_node_modules
  info("Check for necessary global NPM packages")
  which('npm') ||
    error("Node isn't installed. Head to https://nodejs.org/en/download/package-manager")

  global_node_modules = GLOBAL_NODE_MODULES.dup

  global_node_modules.reject! do |m|
    run_command("npm list -g #{m}")
  end

  if global_node_modules.empty?
    success("Global NPM packages needed by Wordless already installed. Good job!")
    return true
  end

  global_node_modules.each do |m|
    run_command("npm install -g #{m}") && success("Installed NPM package #{m} globally")
  end
  success("Done!")
end
install_wordless() click to toggle source
# File lib/wordless/wordless_cli.rb, line 59
def install_wordless
  ensure_wp_cli_installed!

  info("Installing and activating plugin...")

  at_wordpress_root do
    github_url = 'https://github.com/welaika/wordless/archive/master.zip'
    error("Directory '#{plugins_path}' not found.") unless File.directory?(plugins_path)
    if run_command("wp plugin install #{github_url} --activate")
      success("Done!")
    else
      error("There was an error installing and/or activating the Wordless plugin.")
    end
  end
end
start(name) click to toggle source
# File lib/wordless/wordless_cli.rb, line 28
def start(name) # rubocop:disable Metrics/MethodLength
  install_global_node_modules
  install_wordpress_and_wp_cli(name)

  Dir.chdir(name)

  install_wordless
  create_and_activate_theme(name)
  set_permalinks

  Dir.chdir("wp-content/themes/#{name}")

  info('Activate the right node version using NVM')
  if run_command('$SHELL -l -c "nvm use"')
    info("Installing theme's node modules...")
    run_command('$SHELL -l -c "nvm use && yarn setup"') ||
      error("Problem installing theme's node modules")
  else
    warning(
      'NVM or the required node version is not installed. ' \
      'Will try to continue using global node.'
    )
    info("Installing theme's node modules...")
    run_command("yarn setup") || error("Problem installing theme's node modules")
  end

  success("All done! Now yor're ready to use Wordless with following commands:")
  info("`cd #{name}/wp-content/themes/#{name}`")
  info("`yarn run server`")
end

Private Instance Methods

at_wordpress_root() { || ... } click to toggle source
# File lib/wordless/wordless_cli.rb, line 141
def at_wordpress_root
  pwd = Dir.pwd
  Dir.chdir(wordpress_dir)
  begin
    yield
  ensure
    Dir.chdir(pwd)
  end
end
config() click to toggle source
# File lib/wordless/wordless_cli.rb, line 131
def config
  @config ||= begin
    if File.exist?(wordfile_path)
      YAML.safe_load(File.open(wordfile_path)).symbolize_keys
    else
      {}
    end
  end
end
create_and_activate_theme(name) click to toggle source
# File lib/wordless/wordless_cli.rb, line 155
def create_and_activate_theme(name)
  at_wordpress_root do
    info("Activating theme...")
    run_command("wp wordless theme create #{name}") || error("Cannot activate theme '#{name}'")
    success("Done!")
  end
end
install_wordpress_and_wp_cli(name) click to toggle source
# File lib/wordless/wordless_cli.rb, line 151
def install_wordpress_and_wp_cli(name)
  WordPressTools::CLI.new.invoke('new', [name], options)
end
last_dir?(directory) click to toggle source
# File lib/wordless/wordless_cli.rb, line 123
def last_dir?(directory)
  directory == "/"
end
lib_dir() click to toggle source
# File lib/wordless/wordless_cli.rb, line 105
def lib_dir
  @lib_dir ||= __dir__
end
upper_dir(directory) click to toggle source
# File lib/wordless/wordless_cli.rb, line 127
def upper_dir(directory)
  File.join(directory, '..')
end
which(cmd) click to toggle source
# File lib/wordless/wordless_cli.rb, line 171
def which(cmd)
  exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']

  ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
    exts.each do |ext|
      exe = File.join(path, "#{cmd}#{ext}")
      return exe if File.executable?(exe) && !File.directory?(exe)
    end
  end

  nil
end
wordpress_dir(current_dir = Dir.pwd) click to toggle source
# File lib/wordless/wordless_cli.rb, line 109
def wordpress_dir(current_dir = Dir.pwd)
  @wordpress_dir ||= begin
    current_dir = File.expand_path(current_dir)

    if File.exist?(File.join(current_dir, wp_content_path))
      current_dir
    elsif last_dir?(current_dir)
      error("Could not find a valid Wordpress directory")
    else
      wordpress_dir(upper_dir(current_dir))
    end
  end
end