index.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. config - Read configuration for gulp builds and define polyfills
  3. */
  4. import * as fs from 'fs'
  5. import * as json5 from 'json5'
  6. import * as gulp from 'gulp'
  7. import * as log from 'fancy-log'
  8. import {blend} from './blend'
  9. /*
  10. Polyfills
  11. */
  12. global.print = (...args) => log(...args)
  13. global.dump = (...args) => { for (let item of args) print(JSON.stringify(item, null, 4)) }
  14. /*
  15. Build up config environment which is
  16. ../pak.json + package.json + pak.json + product.json + command line
  17. */
  18. var config = {}
  19. let argv = process.argv
  20. let files = [
  21. '../CONFIG/keys.json',
  22. 'CONFIG/keys.json',
  23. '../pak.json',
  24. 'pak.json',
  25. 'product.json',
  26. ]
  27. for (let file of files) {
  28. file = process.cwd() + '/' + file
  29. if (fs.existsSync(file)) {
  30. let json
  31. try {
  32. json = json5.parse(fs.readFileSync(file))
  33. } catch (e) {
  34. print(`Cannot parse ${file}`)
  35. throw e
  36. }
  37. blend(config, json)
  38. }
  39. }
  40. if (fs.existsSync('../CONFIG/terraform.json')) {
  41. config.terraform = {}
  42. blend(config.terraform, json5.parse(fs.readFileSync('../CONFIG/terraform.json')))
  43. }
  44. /*
  45. Support --profile and other '-' switches. Convert --args into config.NAME = value.
  46. */
  47. let args = {}
  48. for (let i = 2; i < argv.length; i++) {
  49. let arg = argv[i]
  50. if (arg.indexOf('--') == 0) {
  51. arg = arg.slice(2)
  52. args[arg] = argv[++i] || true
  53. } else if (arg.indexOf('-') == 0) {
  54. arg = arg.slice(1)
  55. args[arg] = true
  56. }
  57. }
  58. /*
  59. Set sensible defaults: Default to profile == dev, debug == true if dev.
  60. */
  61. config.profile = args.profile || process.env.PROFILE || 'dev'
  62. if (config.profiles && config.profile && config.profiles[config.profile]) {
  63. blend(config, config.profiles[config.profile])
  64. }
  65. config.debug = args.debug || config.debug
  66. if (config.debug == 'true') {
  67. config.debug = true
  68. }
  69. /*
  70. Get version from ../pak.json or ./pak.json
  71. */
  72. try {
  73. var parent = json5.parse(fs.readFileSync(process.cwd() + '/../pak.json'))
  74. config.version = parent.version
  75. } catch (err) {
  76. try {
  77. var parent = json5.parse(fs.readFileSync(process.cwd() + '/pak.json'))
  78. config.version = parent.version
  79. } catch (err) {
  80. print('Cannot read ../pak.json or ./pak.json')
  81. }
  82. }
  83. global.expand = function(v, ...contexts) {
  84. let json = JSON.stringify(v)
  85. for (let context of contexts) {
  86. json = template(json, context)
  87. }
  88. return JSON.parse(json)
  89. }
  90. function template(v, context) {
  91. let text = v.toString()
  92. let matches = text.match(/\$\{[^}]*}/gm)
  93. if (matches) {
  94. for (let name of matches) {
  95. let word = name.slice(2).slice(0, -1)
  96. if (context[word] == undefined) {
  97. context[word] = '${' + word + '}'
  98. }
  99. }
  100. let fn = Function('_context_', 'with (_context_) { return `' + text + '`}')
  101. return fn(context)
  102. }
  103. return text
  104. }
  105. /*
  106. Map configuration hash into env var format. Uppercase with dots and "-" converted to "_".
  107. */
  108. function mapEnv(obj, prefix = '', vars = {}) {
  109. for (let name of Object.keys(obj)) {
  110. let value = obj[name]
  111. if (name == 'profiles') {
  112. continue
  113. }
  114. if (typeof value == 'object') {
  115. mapEnv(value, prefix + name.toUpperCase() + '_', vars)
  116. } else {
  117. name = (prefix + name).toUpperCase().replace(/\./g, '_').replace(/-/g, '_')
  118. vars[name] = value
  119. }
  120. }
  121. return vars
  122. }
  123. global.getEnv = function(obj, context) {
  124. let vars = mapEnv(obj)
  125. for (let [index, v] of Object.entries(vars)) {
  126. vars[index] = expand(v, context)
  127. }
  128. let env = blend({}, process.env)
  129. return blend(env, vars)
  130. }
  131. export default config