1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | "use strict";
|
15 |
|
16 | var fluid = require("infusion"),
|
17 | path = require("path"),
|
18 | resolve = require("fluid-resolve"),
|
19 | kettle = fluid.registerNamespace("kettle");
|
20 |
|
21 | fluid.defaults("kettle.config", {
|
22 | gradeNames: ["fluid.component"]
|
23 | });
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | kettle.config.getConfigName = function (outerDefault) {
|
31 | var nodeEnv = process.argv[3] || process.env.NODE_ENV || outerDefault;
|
32 | if (nodeEnv) {
|
33 | fluid.log("Loader running configuration name " + nodeEnv);
|
34 | } else {
|
35 | fluid.fail("No configuration specified in either 1st command line argument, NODE_ENV or internal default");
|
36 | }
|
37 | return nodeEnv;
|
38 | };
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | kettle.config.getConfigPath = function (outerDefault) {
|
45 | var arg2 = process.argv[2];
|
46 | if (arg2 === "-") {
|
47 | arg2 = null;
|
48 | }
|
49 | var configPath = arg2 || outerDefault;
|
50 | if (!configPath) {
|
51 | fluid.fail("Config path must be specified as 1st command line argument");
|
52 | }
|
53 | return configPath;
|
54 | };
|
55 |
|
56 | kettle.config.initCLI = function (defaultConfigPath, defaultConfigName) {
|
57 | return kettle.config.loadConfig({
|
58 | configPath: kettle.config.getConfigPath(defaultConfigPath),
|
59 | configName: kettle.config.getConfigName(defaultConfigName)
|
60 | });
|
61 | };
|
62 |
|
63 | kettle.config.expectedTopLevel = fluid.arrayToHash(["type", "options", "loadConfigs", "mergeConfigs", "require"]);
|
64 |
|
65 | kettle.config.checkConfig = function (config, fullPath) {
|
66 | fluid.each(config, function (value, key) {
|
67 | if (!kettle.config.expectedTopLevel[key]) {
|
68 | fluid.fail("Error in config file at path " + fullPath + " - key \"" + key +
|
69 | "\" found, where the only legal options are " +
|
70 | fluid.keys(kettle.config.expectedTopLevel).join(", "));
|
71 | }
|
72 | });
|
73 | };
|
74 |
|
75 | kettle.config.loadSubConfigs = function (prefix, fullPath, configPaths, gradeNames) {
|
76 | configPaths = fluid.makeArray(configPaths);
|
77 | fluid.each(configPaths, function loadConfigFromPath(configPath) {
|
78 | var loadedDefaults;
|
79 | try {
|
80 | loadedDefaults = kettle.config.createDefaultsImpl(prefix, configPath);
|
81 | } catch (e) {
|
82 | e.message += " while loading included configs for config at path " + fullPath;
|
83 | throw e;
|
84 | }
|
85 | if (gradeNames) {
|
86 | gradeNames.push(loadedDefaults);
|
87 | }
|
88 | });
|
89 | };
|
90 |
|
91 | kettle.config.createDefaultsImpl = function (prefix, filePath) {
|
92 | var fullPath;
|
93 | if (filePath.charAt(0) === "%") {
|
94 | fullPath = fluid.module.resolvePath(filePath);
|
95 | prefix = path.dirname(fullPath);
|
96 | } else {
|
97 | var fileName = path.basename(filePath),
|
98 | dirName = path.dirname(filePath);
|
99 | prefix = path.resolve(prefix, dirName);
|
100 | fullPath = path.resolve(prefix, fileName);
|
101 | }
|
102 | var configFile;
|
103 | var testFiles = [fullPath, fullPath + ".json", fullPath + ".json5"];
|
104 | var firstExisting = kettle.firstExistingFile(testFiles);
|
105 | if (!firstExisting) {
|
106 | fluid.fail("Could not find a config file at any of the paths ", testFiles.join(", "));
|
107 | }
|
108 | var parser = kettle.JSON.readFileSync(firstExisting, "reading config file at " + firstExisting);
|
109 | parser.then(function (parsed) {
|
110 | configFile = parsed;
|
111 | }, function (rejection) {
|
112 | fluid.fail(rejection.message);
|
113 | });
|
114 |
|
115 | kettle.config.checkConfig(configFile);
|
116 | var gradeNames = ["kettle.config"];
|
117 |
|
118 | kettle.config.loadSubConfigs(prefix, firstExisting, configFile.mergeConfigs, gradeNames);
|
119 | kettle.config.loadSubConfigs(prefix, firstExisting, configFile.loadConfigs);
|
120 |
|
121 | var requires = fluid.makeArray(configFile.require);
|
122 | fluid.each(requires, function loadRequire(requireId) {
|
123 | if (requireId.charAt(0) === "%") {
|
124 | requireId = fluid.module.resolvePath(requireId);
|
125 | }
|
126 | try {
|
127 | var resolved = resolve.sync(requireId, {
|
128 | basedir: prefix
|
129 | });
|
130 | require(resolved);
|
131 | } catch (e) {
|
132 | e.message += " while trying to resolve require directive for " + requireId + " in config at path " + firstExisting;
|
133 | throw (e);
|
134 | }
|
135 | });
|
136 | configFile.type = configFile.type || "kettle.config." + fluid.allocateGuid();
|
137 | configFile.options = configFile.options || {};
|
138 | configFile.options.gradeNames = gradeNames.concat(fluid.makeArray(
|
139 | configFile.options.gradeNames));
|
140 | fluid.defaults(configFile.type, configFile.options);
|
141 | fluid.log(fluid.logLevel.TRACE, "Created defaults for config type " + configFile.type + ": " + fluid.prettyPrintJSON(configFile.options));
|
142 | return configFile.type;
|
143 | };
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 | kettle.config.createDefaults = function (options) {
|
155 | return kettle.config.createDefaultsImpl(fluid.module.resolvePath(options.configPath), options.configName);
|
156 | };
|
157 |
|
158 |
|
159 |
|
160 | kettle.config.loadConfig = function (options) {
|
161 | var componentName = kettle.config.createDefaults(options);
|
162 | return fluid.invokeGlobalFunction(componentName);
|
163 | };
|