-
Notifications
You must be signed in to change notification settings - Fork 185
/
Copy pathupgrade_tailwindcss.rb
63 lines (55 loc) · 2.55 KB
/
upgrade_tailwindcss.rb
1
2
3
4
5
6
7
8
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
TAILWIND_CONFIG_PATH = Rails.root.join("config/tailwind.config.js")
APPLICATION_LAYOUT_PATH = Rails.root.join("app/views/layouts/application.html.erb")
POSTCSS_CONFIG_PATH = Rails.root.join("config/postcss.config.js")
OLD_TAILWIND_ASSET_PATH = Rails.root.join("app/assets/stylesheets/application.tailwind.css")
TAILWIND_ASSET_PATH = Rails.root.join("app/assets/tailwind/application.css")
unless TAILWIND_CONFIG_PATH.exist?
say "Default tailwind.config.js is missing!", :red
abort
end
if File.read(TAILWIND_CONFIG_PATH).match?(/defaultTheme/)
say "Removing references to 'defaultTheme' from #{TAILWIND_CONFIG_PATH}"
gsub_file TAILWIND_CONFIG_PATH.to_s, /^(.*defaultTheme)/, "// \\1"
end
if POSTCSS_CONFIG_PATH.exist?
say "Moving PostCSS configuration to application root directory"
copy_file POSTCSS_CONFIG_PATH, Rails.root.join("postcss.config.js")
remove_file POSTCSS_CONFIG_PATH
end
if APPLICATION_LAYOUT_PATH.exist?
if File.read(APPLICATION_LAYOUT_PATH).match?(/"inter-font"/)
say "Strip Inter font CSS from application layout"
gsub_file APPLICATION_LAYOUT_PATH.to_s, %r{, "inter-font"}, ""
else
say "Inter font CSS not detected.", :green
end
if File.read(APPLICATION_LAYOUT_PATH).match?(/stylesheet_link_tag :app/) &&
File.read(APPLICATION_LAYOUT_PATH).match?(/stylesheet_link_tag "tailwind"/)
say "Remove unnecessary stylesheet_link_tag from application layout"
gsub_file APPLICATION_LAYOUT_PATH.to_s, %r{^\s*<%= stylesheet_link_tag "tailwind".*%>$}, ""
end
else
say "Default application.html.erb is missing!", :red
say %( Please check your layouts and remove any "inter-font" stylesheet links.)
end
if OLD_TAILWIND_ASSET_PATH.exist?
say "Moving #{OLD_TAILWIND_ASSET_PATH} to #{TAILWIND_ASSET_PATH}"
copy_file OLD_TAILWIND_ASSET_PATH, TAILWIND_ASSET_PATH
remove_file OLD_TAILWIND_ASSET_PATH
end
if system("npx --version")
say "Running the upstream Tailwind CSS upgrader"
command = Shellwords.join(["npx", "@tailwindcss/upgrade@next", "--force", "--config", TAILWIND_CONFIG_PATH.to_s])
success = run(command, abort_on_failure: false)
unless success
say "The upgrade tool failed!", :red
say %( You probably need to update your configuration. Please read the error messages,)
say %( and check the Tailwind CSS upgrade guide at https://tailwindcss.com/docs/upgrade-guide.)
abort
end
else
say "Could not run the Tailwind upgrade tool. Please see https://tailwindcss.com/docs/upgrade-guide for manual instructions.", :red
abort
end
say "Compile initial Tailwind build"
run "rails tailwindcss:build"