UNPKG

1.62 kBJavaScriptView Raw
1'use strict';
2
3var fs = require('fs'),
4 path = require('path'),
5 CachingWriter = require('broccoli-caching-writer');
6
7function ConfigLoader(inputNode, options) {
8 options = options || {};
9 CachingWriter.call(this, [inputNode], { annotation: options.annotation });
10 this.options = options;
11}
12
13ConfigLoader.prototype = Object.create(CachingWriter.prototype);
14ConfigLoader.prototype.constructor = ConfigLoader;
15
16/*
17 * @private
18 *
19 * On windows, when residing on a UNC share (lib or app/addon code), exact
20 * match here is not possible. Although we could be more precise, there is
21 * little pain in evicting all fuzzy matches
22 *
23 * @method fuzzyPurgeRequireEntry
24 */
25
26function 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
34ConfigLoader.prototype.clearConfigGeneratorCache = function() {
35 fuzzyPurgeRequireEntry(this.options.project.configPath() + '.js');
36};
37
38ConfigLoader.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
60module.exports = ConfigLoader;