1 | /*
|
2 | Kettle Resolvers
|
3 |
|
4 | Copyright 2017 Raising the Floor - International
|
5 |
|
6 | Licensed under the New BSD license. You may not use this file except in
|
7 | compliance with this License.
|
8 |
|
9 | You may obtain a copy of the License at
|
10 | https://github.com/fluid-project/kettle/blob/master/LICENSE.txt
|
11 | */
|
12 |
|
13 | ;
|
14 |
|
15 | var fluid = require("infusion"),
|
16 | fs = require("fs"),
|
17 | kettle = fluid.registerNamespace("kettle");
|
18 |
|
19 | fluid.registerNamespace("kettle.resolvers");
|
20 |
|
21 | /** Returns an environment variable with the given name.
|
22 | * @param name {String} The name of the environment variable to return
|
23 | * @return {String|Undefined} The value of the required environment variable,
|
24 | * or `undefined` if there is no value.
|
25 | */
|
26 |
|
27 | kettle.resolvers.env = function (name) {
|
28 | return process.env[name];
|
29 | };
|
30 |
|
31 | /** Returns the contents of the given file as a string, interpreting the
|
32 | * file contents as UTF-8. If the file does not exist, an exception will be thrown.
|
33 | * @param fileName {String} The name of the file to be resolved, which may begin
|
34 | * with an Infusion module reference of the form `%module-name` as described in
|
35 | * <a href="http://docs.fluidproject.org/infusion/development/NodeAPI.html#fluid-module-resolvepath-path-">fluid.module.resolvePath</a>.
|
36 | * @return {String} The contents of the file as a string.
|
37 | */
|
38 |
|
39 | kettle.resolvers.file = function (fileName) {
|
40 | if (fileName.charAt(0) === "%") {
|
41 | fileName = fluid.module.resolvePath(fileName);
|
42 | }
|
43 | return fs.readFileSync(fileName, "utf8");
|
44 | };
|
45 |
|
46 | /** Returns the process argument at the specified index, or the entire argument
|
47 | * list if no index is supplied.
|
48 | * @param index {Integer} [optional] The index of the required process argument.
|
49 | * @return {String|Undefined|Array of String} The required process argument if it
|
50 | * exists, undefined if it does not, or the full array of process arguments if
|
51 | * no value was supplied for `index`.
|
52 | */
|
53 |
|
54 | kettle.resolvers.args = function (index) {
|
55 | return index === undefined ? process.argv : process.argv[index];
|
56 | };
|
57 |
|
58 | // We plan for these to be mockable via a FLUID-6157 approach
|