tailwindcss-formatter 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b19aef1c5b1d66878b4958539acd3bdbbb750b6aa88ca7badedcb951003483a6
4
+ data.tar.gz: 91497c6398ec7faef985b192c36912425faa135c155f511f42f4c49410a33dc5
5
+ SHA512:
6
+ metadata.gz: 84bd6491ab8ad09dc25fab0c7eb97c30799d3d7fe90fa94cb7ffa132d9bc930be3a9c7c195dc6f8d927b6ab6951f099a6ffa99519c31109374307e551b8590c4
7
+ data.tar.gz: 538d2bc1081fb42109afcbdef1551d9d3a261fe53b219c2eb262db9dc4530534f79962a59c87263e5aba809ce11c981bd41c326f68548d00e13260572362297b
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # Tailwindcss::Formatter
2
+
3
+ TODO: Delete this and the text below, and describe your gem
4
+
5
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/tailwindcss/formatter`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ ## Installation
8
+
9
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ ```bash
14
+ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
15
+ ```
16
+
17
+ If bundler is not being used to manage dependencies, install the gem by executing:
18
+
19
+ ```bash
20
+ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
21
+ ```
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/tailwindcss-formatter.
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "tailwindcss/formatter"
5
+
6
+ # Call the TailwindFormat CLI with arguments from the command line
7
+ Tailwindcss::Formatter::CLI.start(ARGV)
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "constants"
4
+
5
+ module Tailwindcss
6
+ module Formatter
7
+ # The Tailwindcss::Formatter::ClassSorter class is responsible for sorting
8
+ # Tailwind CSS classes in a consistent order according to best practices.
9
+ # This helps ensure that Tailwind CSS classes in HTML or other templates are
10
+ # ordered logically, improving readability and maintainability of the code.
11
+ #
12
+ # The sorting is based on predefined patterns for class categories such as
13
+ # layout, typography, colors, etc.
14
+ class ClassSorter
15
+ def initialize(check_formatted: false, write: false)
16
+ @check_formatted = check_formatted
17
+ @write = write
18
+ end
19
+
20
+ def process_file(file)
21
+ original_content = File.read(file)
22
+ formatted_content = format_content(original_content)
23
+
24
+ # Check if the file was formatted
25
+ if formatted_content == original_content
26
+ # File was allready formatted correctly
27
+ puts "\e[90m#{file}\e[0m"
28
+ else
29
+ puts file
30
+ fail_format_check if @check_formatted
31
+ File.write(file, formatted_content) if @write
32
+ end
33
+ end
34
+
35
+ private
36
+
37
+ def format_content(content)
38
+ CLASS_REGEX_PATTERNS.each do |_regex|
39
+ content = content.gsub(_regex[:pattern]) do |_match|
40
+ "#{_regex[:prefix]}\"#{sorted_class_string(Regexp.last_match(1))}\""
41
+ end
42
+ end
43
+
44
+ content
45
+ end
46
+
47
+ def sorted_class_string(class_string)
48
+ class_list = class_string.split(/\s+/)
49
+ sorted_list = class_list.sort_by do |class_name|
50
+ order_index = CLASS_ORDER.index(class_name)
51
+ order_index.nil? ? [1, class_name] : [0, order_index]
52
+ end
53
+
54
+ sorted_list.join(" ")
55
+ end
56
+
57
+ def fail_format_check
58
+ puts "Check format failure - exited with error code 1"
59
+ exit(1)
60
+ end
61
+ end
62
+ end
63
+ end