{"action":"opened","issue":{"url":"https://api.github.com/repos/klei/gulp-angular-filesort/issues/27","labels_url":"https://api.github.com/repos/klei/gulp-angular-filesort/issues/27/labels{/name}","comments_url":"https://api.github.com/repos/klei/gulp-angular-filesort/issues/27/comments","events_url":"https://api.github.com/repos/klei/gulp-angular-filesort/issues/27/events","html_url":"https://github.com/klei/gulp-angular-filesort/issues/27","id":65648686,"number":27,"title":"Non-reversed option and simple debug options","user":{"login":"lborupj","id":7460210,"avatar_url":"https://avatars.githubusercontent.com/u/7460210?v=3","gravatar_id":"","url":"https://api.github.com/users/lborupj","html_url":"https://github.com/lborupj","followers_url":"https://api.github.com/users/lborupj/followers","following_url":"https://api.github.com/users/lborupj/following{/other_user}","gists_url":"https://api.github.com/users/lborupj/gists{/gist_id}","starred_url":"https://api.github.com/users/lborupj/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lborupj/subscriptions","organizations_url":"https://api.github.com/users/lborupj/orgs","repos_url":"https://api.github.com/users/lborupj/repos","events_url":"https://api.github.com/users/lborupj/events{/privacy}","received_events_url":"https://api.github.com/users/lborupj/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-04-01T09:57:02Z","updated_at":"2015-04-01T09:57:02Z","closed_at":null,"body":"Hi,\r\n\r\nWhen used to sort bower components, the non-reversed order is needed, hence reverse should be an option. Perhaps something like (index.js):\r\n\r\n```javascript\r\n\r\nvar es = require('event-stream');\r\nvar ngDep = require('ng-dependencies');\r\nvar toposort = require('toposort');\r\nvar gutil = require('gulp-util');\r\nvar PluginError = gutil.PluginError;\r\n\r\nvar PLUGIN_NAME = 'gulp-angular-filesort';\r\nvar ANGULAR_MODULE = 'ng';\r\n\r\nmodule.exports = function angularFilesort (opts) {\r\n  var files = [];\r\n  var angmods = {};\r\n  var toSort = [];\r\n  var dbgTitle = 'angularFilesort: ';\r\n  var lOpts = opts || {debug: false, reverse: true};\r\n\r\n  return es.through(function collectFilesToSort (file) {\r\n      if(!file.contents) {\r\n        return this.emit('error', new PluginError(PLUGIN_NAME, 'File: \"' + file.relative + '\" without content. You have to read it with gulp.src(..)'));\r\n      }\r\n      \r\n      var deps;\r\n      try {\r\n        deps = ngDep(file.contents);\r\n      } catch (err) {\r\n        return this.emit('error', new PluginError(PLUGIN_NAME, 'Error in parsing: \"' + file.relative + '\", ' + err.message));\r\n      }\r\n\r\n      if (lOpts.debug) {\r\n        \tgutil.log(dbgTitle + file.path + ' Modules={' + Object.keys(deps.modules) + '}, dependencies={' + deps.dependencies +'}');\r\n      }\r\n      \r\n      if (deps.modules) {\r\n        // Store references to each file with a declaration:\r\n        Object.keys(deps.modules).forEach(function (name) {\r\n          angmods[name] = file;\r\n        });\r\n      }\r\n      \r\n      if (deps.dependencies) {\r\n        // Add each file with dependencies to the array to sort:\r\n        deps.dependencies.forEach(function (dep) {\r\n          if (isDependecyUsedInAnyDeclaration(dep, deps)) {\r\n            return;\r\n          }\r\n          if (dep === ANGULAR_MODULE) {\r\n            return;\r\n          }\r\n          toSort.push([file, dep]);\r\n        });\r\n      }\r\n\r\n      // Collect all files:\r\n      files.push(file);\r\n\r\n    }, function afterFileCollection () {\r\n      // Convert all module names to actual files with declarations:\r\n      for (var i = 0; i < toSort.length; i++) {\r\n        var moduleName = toSort[i][1];\r\n        var declarationFile = angmods[moduleName];\r\n        if (declarationFile) {\r\n          toSort[i][1] = declarationFile;\r\n        } else {\r\n          // Depending on module outside stream (possibly a 3rd party one),\r\n          // don't care when sorting:\r\n          toSort.splice(i--, 1);\r\n        }\r\n      }\r\n\r\n      // Sort `files` with `toSort` as dependency tree:\r\n      var sorted = toposort.array(files, toSort);\r\n      if (lOpts.reverse) {\r\n    \t  sorted = sorted.reverse();\r\n      }\r\n      sorted.forEach(function (file) {\r\n          this.emit('data', file);\r\n      }.bind(this));\r\n\r\n      this.emit('end');\r\n    });\r\n};\r\n\r\nfunction isDependecyUsedInAnyDeclaration (dependency, ngDeps) {\r\n  if (!ngDeps.modules) {\r\n    return false;\r\n  }\r\n  if (dependency in ngDeps.modules) {\r\n    return true;\r\n  }\r\n  return Object.keys(ngDeps.modules).some(function (module) {\r\n    return ngDeps.modules[module].indexOf(dependency) > -1;\r\n  });\r\n}\r\n\r\n```\r\n\r\nand then use\r\n\r\n```javascript\r\n\treturn gulp.src(bowerFiles(), {base : 'bower_components/'})\r\n\t\t.pipe(filterScriptsOnly)\r\n\t\t.pipe(plugins.angularFilesort({debug: false, reverse: false}))\r\n\t\t.pipe(plugins.order(['**/jquery.js', '**/angular.js']))\r\n\t\t.pipe(debug({title: 'sorted-vendor-scripts:'}))\r\n\t\t.pipe(filterScriptsOnly.restore());\t\r\n```\r\n\r\nto sort correctly (and prefer jquery)"}}