<commit_before>
url = require 'url'

_ = require 'underscore'
nconf = require 'nconf'

Config = (require 'config').Config
pkgman = require 'pkgman'

exports.$config = (req) ->
	
	baseUrl: "//#{req.headers.host}"
	testMode: if (req.nconf.get 'E2E')? then 'e2e' else false
	debugging: 'production' isnt req.nconf.get 'NODE_ENV'
	packageList: req.nconf.get 'packageList'

exports.$httpMiddleware = (http) ->
	
	label: 'Serve package configuration'
	middleware: [

		(req, res, next) ->
			
			req.nconf = nconf
			
			next()
		
		(req, res, next) ->
			return next() unless req.url is '/js/config.js'
			
			config = {}
			pkgman.invoke 'config', (path, fn) -> _.extend config, fn req
			
			prettyPrintConfig = ->
				[first, rest...] = (JSON.stringify config, null, '  ').split '\n'
				([first].concat rest.map (line) -> '  ' + line).join '\n'
				
			res.setHeader 'Content-Type', 'text/javascript'
			
			res.send """
angular.module('shrub.config', []).provider('config', function() {

  var __slice = [].slice;
  
  return new ((#{Config.toString()})())(#{prettyPrintConfig()});

});
"""
			
	]<commit_msg>hook $config can now return promises<commit_after>
url = require 'url'

_ = require 'underscore'
nconf = require 'nconf'
Promise = require 'bluebird'

Config = (require 'config').Config
pkgman = require 'pkgman'

exports.$config = (req) ->
	
	baseUrl: "//#{req.headers.host}"
	testMode: if (req.nconf.get 'E2E')? then 'e2e' else false
	debugging: 'production' isnt req.nconf.get 'NODE_ENV'
	packageList: req.nconf.get 'packageList'

exports.$httpMiddleware = (http) ->
	
	label: 'Serve package configuration'
	middleware: [

		(req, res, next) ->
			
			req.nconf = nconf
			
			next()
		
		(req, res, next) ->
			return next() unless req.url is '/js/config.js'
			
			promises = []
			pkgman.invoke 'config', (path, fn) -> promises.push fn req
			
			config = {}
			Promise.all(promises).then((subconfigs) ->
				_.extend config, subconfig for subconfig in subconfigs
			).then(->
				
					prettyPrintConfig = ->
						[first, rest...] = (JSON.stringify config, null, '  ').split '\n'
						([first].concat rest.map (line) -> '  ' + line).join '\n'
						
					res.setHeader 'Content-Type', 'text/javascript'
					
					res.send """
angular.module('shrub.config', []).provider('config', function() {

  var __slice = [].slice;
  
  return new ((#{Config.toString()})())(#{prettyPrintConfig()});

});
"""
				
				(error) -> next error
			)
			
	]
{'fraction_non_alphanumeric': 0.18111518111518113, 'fraction_numerical': 0.001628001628001628, 'mean_word_length': 3.0695364238410594, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '347844', 'n_tokens_mistral': 989, 'n_tokens_neox': 840, 'n_words': 204}