1 | 'use strict';
|
2 |
|
3 | var fs = require('fs'),
|
4 | path = require('path'),
|
5 | CachingWriter = require('broccoli-caching-writer');
|
6 |
|
7 | function ConfigLoader(inputNode, options) {
|
8 | options = options || {};
|
9 | CachingWriter.call(this, [inputNode], { annotation: options.annotation });
|
10 | this.options = options;
|
11 | }
|
12 |
|
13 | ConfigLoader.prototype = Object.create(CachingWriter.prototype);
|
14 | ConfigLoader.prototype.constructor = ConfigLoader;
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | function fuzzyPurgeRequireEntry(entry) {
|
27 | return Object.keys(require.cache).filter(function(path) {
|
28 | return path.indexOf(entry) > -1;
|
29 | }).forEach(function(entry) {
|
30 | delete require.cache[entry];
|
31 | });
|
32 | }
|
33 |
|
34 | ConfigLoader.prototype.clearConfigGeneratorCache = function() {
|
35 | fuzzyPurgeRequireEntry(this.options.project.configPath() + '.js');
|
36 | };
|
37 |
|
38 | ConfigLoader.prototype.build = function() {
|
39 | this.clearConfigGeneratorCache();
|
40 |
|
41 | var outputDir = path.join(this.outputPath, 'environments');
|
42 | fs.mkdirSync(outputDir);
|
43 |
|
44 | var environments = [this.options.env];
|
45 | if (this.options.tests) {
|
46 | environments.push('test');
|
47 | }
|
48 |
|
49 | environments.forEach(function(env) {
|
50 | var config = this.options.project.config(env),
|
51 | jsonString = JSON.stringify(config),
|
52 | outputPath = path.join(outputDir, env);
|
53 |
|
54 | fs.writeFileSync(outputPath + '.json', jsonString, {
|
55 | encoding: 'utf8'
|
56 | });
|
57 | }, this);
|
58 | };
|
59 |
|
60 | module.exports = ConfigLoader;
|