Class: Wordless::WordlessCLI

Inherits:
Object
  • Object
show all
Includes:
CLIHelper
Defined in:
lib/wordless/wordless_cli.rb

Defined Under Namespace

Modules: PATH

Constant Summary collapse

DEFAULT_PATHS =
{
  PATH::WORDFILE => 'Wordfile',
  PATH::WP_CONTENT => 'wp-content',
  PATH::PLUGINS => 'wp-content/plugins',
  PATH::THEMES => 'wp-content/themes'
}.freeze
GLOBAL_NODE_MODULES =
%w[yarn].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(thor, options = {}) ⇒ WordlessCLI

Returns a new instance of WordlessCLI.



23
24
25
26
# File 'lib/wordless/wordless_cli.rb', line 23

def initialize(thor, options = {})
  @options = options
  @thor = thor
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/wordless/wordless_cli.rb', line 5

def options
  @options
end

#thorObject (readonly)

Returns the value of attribute thor.



5
6
7
# File 'lib/wordless/wordless_cli.rb', line 5

def thor
  @thor
end

Instance Method Details

#install_global_node_modulesObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# 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_wordlessObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# 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) ⇒ Object

rubocop:disable Metrics/MethodLength



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# 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