utils.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. 'use strict';
  2. var fs = require('fs')
  3. var ini = require('ini')
  4. var path = require('path')
  5. var stripJsonComments = require('strip-json-comments')
  6. var parse = exports.parse = function (content) {
  7. //if it ends in .json or starts with { then it must be json.
  8. //must be done this way, because ini accepts everything.
  9. //can't just try and parse it and let it throw if it's not ini.
  10. //everything is ini. even json with a syntax error.
  11. if(/^\s*{/.test(content))
  12. return JSON.parse(stripJsonComments(content))
  13. return ini.parse(content)
  14. }
  15. var file = exports.file = function () {
  16. var args = [].slice.call(arguments).filter(function (arg) { return arg != null })
  17. //path.join breaks if it's a not a string, so just skip this.
  18. for(var i in args)
  19. if('string' !== typeof args[i])
  20. return
  21. var file = path.join.apply(null, args)
  22. var content
  23. try {
  24. return fs.readFileSync(file,'utf-8')
  25. } catch (err) {
  26. return
  27. }
  28. }
  29. var json = exports.json = function () {
  30. var content = file.apply(null, arguments)
  31. return content ? parse(content) : null
  32. }
  33. var env = exports.env = function (prefix, env) {
  34. env = env || process.env
  35. var obj = {}
  36. var l = prefix.length
  37. for(var k in env) {
  38. if(k.toLowerCase().indexOf(prefix.toLowerCase()) === 0) {
  39. var keypath = k.substring(l).split('__')
  40. // Trim empty strings from keypath array
  41. var _emptyStringIndex
  42. while ((_emptyStringIndex=keypath.indexOf('')) > -1) {
  43. keypath.splice(_emptyStringIndex, 1)
  44. }
  45. var cursor = obj
  46. keypath.forEach(function _buildSubObj(_subkey,i){
  47. // (check for _subkey first so we ignore empty strings)
  48. // (check for cursor to avoid assignment to primitive objects)
  49. if (!_subkey || typeof cursor !== 'object')
  50. return
  51. // If this is the last key, just stuff the value in there
  52. // Assigns actual value from env variable to final key
  53. // (unless it's just an empty string- in that case use the last valid key)
  54. if (i === keypath.length-1)
  55. cursor[_subkey] = env[k]
  56. // Build sub-object if nothing already exists at the keypath
  57. if (cursor[_subkey] === undefined)
  58. cursor[_subkey] = {}
  59. // Increment cursor used to track the object at the current depth
  60. cursor = cursor[_subkey]
  61. })
  62. }
  63. }
  64. return obj
  65. }
  66. var find = exports.find = function () {
  67. var rel = path.join.apply(null, [].slice.call(arguments))
  68. function find(start, rel) {
  69. var file = path.join(start, rel)
  70. try {
  71. fs.statSync(file)
  72. return file
  73. } catch (err) {
  74. if(path.dirname(start) !== start) // root
  75. return find(path.dirname(start), rel)
  76. }
  77. }
  78. return find(process.cwd(), rel)
  79. }