Class: ApiMaker::MoveComponentsToRoutes

Inherits:
ApplicationService show all
Defined in:
app/services/api_maker/move_components_to_routes.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationService

#api_maker_json

Constructor Details

#initialize(prepend_path:, routes_path:) ⇒ MoveComponentsToRoutes

Returns a new instance of MoveComponentsToRoutes.



4
5
6
7
# File 'app/services/api_maker/move_components_to_routes.rb', line 4

def initialize(prepend_path:, routes_path:)
  @prepend_path = prepend_path
  @routes_path = routes_path
end

Instance Attribute Details

#prepend_pathObject (readonly)

Returns the value of attribute prepend_path.



2
3
4
# File 'app/services/api_maker/move_components_to_routes.rb', line 2

def prepend_path
  @prepend_path
end

#routes_pathObject (readonly)

Returns the value of attribute routes_path.



2
3
4
# File 'app/services/api_maker/move_components_to_routes.rb', line 2

def routes_path
  @routes_path
end

Instance Method Details

#dir_empty?(path) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
49
# File 'app/services/api_maker/move_components_to_routes.rb', line 41

def dir_empty?(path)
  Dir.foreach(path) do |file|
    next if file == "." || file == ".."

    return false
  end

  true
end

#performObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/services/api_maker/move_components_to_routes.rb', line 9

def perform
  routes.each do |route|
    path = "#{prepend_path}#{route.fetch("component")}"
    full_path = "app/javascript/components/#{path}"
    new_path = "app/javascript/routes/#{path}"

    full_path_with_jsx = "#{full_path}.jsx"
    new_path_with_jsx = "#{new_path}.jsx"

    if !File.exist?(full_path_with_jsx) && File.exist?("#{full_path}/index.jsx")
      full_path_with_jsx = "#{full_path}/index.jsx"
      new_path_with_jsx = "#{new_path}/index.jsx"
    end

    if File.exist?(full_path_with_jsx)
      new_dir = File.dirname(new_path_with_jsx)
      FileUtils.mkdir_p(new_dir) unless File.exist?(new_dir)
      File.rename(full_path_with_jsx, new_path_with_jsx)
    end

    old_dir = File.dirname(full_path_with_jsx)

    Dir.unlink(old_dir) if File.exist?(old_dir) && dir_empty?(old_dir)
  end

  succeed!
end

#routesObject



37
38
39
# File 'app/services/api_maker/move_components_to_routes.rb', line 37

def routes
  @routes = JSON.parse(File.read(routes_path)).fetch("routes")
end