Brak opisu

index.js 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. 'use strict';
  2. var path = require('path');
  3. var findIndex = require('find-index');
  4. var flattenGlob = function(arr){
  5. var out = [];
  6. var flat = true;
  7. for(var i = 0; i < arr.length; i++) {
  8. if (typeof arr[i] !== 'string') {
  9. flat = false;
  10. break;
  11. }
  12. out.push(arr[i]);
  13. }
  14. // last one is a file or specific dir
  15. // so we pop it off
  16. if (flat) {
  17. out.pop();
  18. }
  19. return out;
  20. };
  21. var flattenExpansion = function(set) {
  22. var first = set[0];
  23. var toCompare = set.slice(1);
  24. // find index where the diff is
  25. var idx = findIndex(first, function(v, idx){
  26. if (typeof v !== 'string') {
  27. return true;
  28. }
  29. var matched = toCompare.every(function(arr){
  30. return v === arr[idx];
  31. });
  32. return !matched;
  33. });
  34. return first.slice(0, idx);
  35. };
  36. var setToBase = function(set) {
  37. // normal something/*.js
  38. if (set.length <= 1) {
  39. return flattenGlob(set[0]);
  40. }
  41. // has expansion
  42. return flattenExpansion(set);
  43. };
  44. module.exports = function(glob) {
  45. var set = glob.minimatch.set;
  46. var baseParts = setToBase(set);
  47. var basePath = path.normalize(baseParts.join(path.sep))+path.sep;
  48. return basePath;
  49. };