pulsar/build/tasks/output-module-counts.coffee

67 lines
2.0 KiB
CoffeeScript
Raw Normal View History

2015-02-12 02:19:29 +03:00
fs = require 'fs'
path = require 'path'
module.exports = (grunt) ->
grunt.registerTask 'output-module-counts', 'Log modules where more than one copy exists in node_modules', ->
nodeModulesDir = path.resolve(__dirname, '..', '..', 'node_modules')
2015-02-11 06:10:23 +03:00
otherModules = {}
atomModules = {}
sortModuleNames = (modules) ->
Object.keys(modules).sort (name1, name2) ->
diff = modules[name2].count - modules[name1].count
diff = name1.localeCompare(name2) if diff is 0
diff
getAtomTotal = ->
Object.keys(atomModules).length
getOtherTotal = ->
Object.keys(otherModules).length
2015-02-12 02:19:29 +03:00
recurseHandler = (absolutePath, rootPath, relativePath, fileName) ->
return if fileName isnt 'package.json'
2015-02-11 06:10:23 +03:00
{name, version, repository} = grunt.file.readJSON(absolutePath)
2015-02-11 05:54:29 +03:00
return unless name and version
2015-02-11 06:10:23 +03:00
repository = repository.url if repository?.url
if /.+\/atom\/.+/.test(repository)
modules = atomModules
else
modules = otherModules
2015-02-11 04:34:43 +03:00
modules[name] ?= {versions: {}, count: 0}
modules[name].count++
modules[name].versions[version] = true
2015-02-12 02:19:29 +03:00
walkNodeModuleDir = ->
grunt.file.recurse(nodeModulesDir, recurseHandler)
# Handle broken symlinks that grunt.file.recurse fails to handle
loop
try
walkNodeModuleDir()
break
catch error
if error.code is 'ENOENT'
fs.unlinkSync(error.path)
otherModules = {}
atomModules = {}
else
break
2015-02-11 06:10:23 +03:00
if getAtomTotal() > 0
console.log "Atom Modules: #{getAtomTotal()}"
sortModuleNames(atomModules).forEach (name) ->
{count, versions, atom} = atomModules[name]
grunt.log.error "#{name}: #{count} (#{Object.keys(versions).join(', ')})" if count > 1
console.log()
2015-02-11 06:10:23 +03:00
console.log "Other Modules: #{getOtherTotal()}"
sortModuleNames(otherModules).forEach (name) ->
{count, versions, atom} = otherModules[name]
2015-02-11 04:34:43 +03:00
grunt.log.error "#{name}: #{count} (#{Object.keys(versions).join(', ')})" if count > 1