build.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /**
  2. * http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types
  3. * https://github.com/broofa/node-mime/blob/master/types/node.types
  4. *
  5. * Convert these text files to JSON for browser usage.
  6. */
  7. var co = require('co')
  8. var fs = require('fs')
  9. var path = require('path')
  10. var cogent = require('cogent')
  11. function* get(url) {
  12. var res = yield* cogent(url, {
  13. string: true
  14. })
  15. if (res.statusCode !== 200)
  16. throw new Error('got status code ' + res.statusCode + ' from ' + url)
  17. var text = res.text
  18. var json = {}
  19. // http://en.wikipedia.org/wiki/Internet_media_type#Naming
  20. /**
  21. * Mime types and associated extensions are stored in the form:
  22. *
  23. * <type> <ext> <ext> <ext>
  24. *
  25. * And some are commented out with a leading `#` because they have no associated extensions.
  26. * This regexp checks whether a single line matches this format, ignoring lines that are just comments.
  27. * We could also just remove all lines that start with `#` if we want to make the JSON files smaller
  28. * and ignore all mime types without associated extensions.
  29. */
  30. var re = /^(?:# )?([\w-]+\/[\w\+\.-]+)(?:\s+\w+)*$/
  31. text = text.split('\n')
  32. .filter(Boolean)
  33. .forEach(function (line) {
  34. line = line.trim()
  35. if (!line) return
  36. var match = re.exec(line)
  37. if (!match) return
  38. // remove the leading # and <type> and return all the <ext>s
  39. json[match[1]] = line.replace(/^(?:# )?([\w-]+\/[\w\+\.-]+)/, '')
  40. .split(/\s+/)
  41. .filter(Boolean)
  42. })
  43. fs.writeFileSync('lib/' + path.basename(url).split('.')[0] + '.json',
  44. JSON.stringify(json, null, 2) + '\n')
  45. }
  46. co(function* () {
  47. yield [
  48. get('http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types'),
  49. get('https://raw.githubusercontent.com/broofa/node-mime/master/types/node.types')
  50. ]
  51. })()