Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated glob sorting to sort files in parent directories ahead of files in sub-directories. #195

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion lib/jammit/packager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,25 @@ def package_for(package, extension)
# Print a warning if no files were found that match the glob.
def glob_files(glob)
absolute = Pathname.new(glob).absolute?
paths = Dir[absolute ? glob : File.join(ASSET_ROOT, glob)].sort
paths = Dir[absolute ? glob : File.join(ASSET_ROOT, glob)].sort(&method(:glob_comparator))
Jammit.warn("No assets match '#{glob}'") if paths.empty?
paths
end

# Compares two paths, sorting files in parent directories before
# files in their subdirectories.
# a/a.js
# a/b.js
# a/a/a.js
def glob_comparator(path1, path2)
dir1 = File.dirname(path1)
dir2 = File.dirname(path2)

return path1 <=> path2 if dir1 == dir2
return 1 if dir1.start_with?(dir2)
return -1 if dir2.start_with?(dir1)
dir1 <=> dir2
end

# Get the latest mtime of a list of files (plus the config path).
def latest_mtime(paths)
Expand Down
2 changes: 2 additions & 0 deletions test/config/assets.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ javascripts:
js_test_nested:
- *js_test
- fixtures/src/nested/*.js
js_test_nested2:
- fixtures/src/**/*.js
jst_test: &jst_test
- fixtures/src/*.jst
jst_test_nested:
Expand Down
2 changes: 2 additions & 0 deletions test/unit/test_packager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ def test_fetching_lists_of_nested_urls
assert urls == ['/fixtures/src/test1.css', '/fixtures/src/test2.css', '/fixtures/src/test_fonts.css', '/fixtures/src/nested/nested1.css', '/fixtures/src/nested/nested2.css']
urls = Jammit.packager.individual_urls(:js_test_nested, :js)
assert urls == ['/fixtures/src/test1.js', '/fixtures/src/test2.js', '/fixtures/src/nested/nested1.js', '/fixtures/src/nested/nested2.js']
urls = Jammit.packager.individual_urls(:js_test_nested2, :js)
assert urls == ['/fixtures/src/test1.js', '/fixtures/src/test2.js', '/fixtures/src/nested/nested1.js', '/fixtures/src/nested/nested2.js']
urls = Jammit.packager.individual_urls(:jst_test_nested, :js)
assert urls == ['/assets/jst_test_nested.jst']
end
Expand Down