You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

153 lines
5.9 KiB

module.exports = (function() {
var __MODS__ = {};
var __DEFINE__ = function(modId, func, req) { var m = { exports: {}, _tempexports: {} }; __MODS__[modId] = { status: 0, func: func, req: req, m: m }; };
var __REQUIRE__ = function(modId, source) { if(!__MODS__[modId]) return require(source); if(!__MODS__[modId].status) { var m = __MODS__[modId].m; m._exports = m._tempexports; var desp = Object.getOwnPropertyDescriptor(m, "exports"); if (desp && desp.configurable) Object.defineProperty(m, "exports", { set: function (val) { if(typeof val === "object" && val !== m._exports) { m._exports.__proto__ = val.__proto__; Object.keys(val).forEach(function (k) { m._exports[k] = val[k]; }); } m._tempexports = val }, get: function () { return m._tempexports; } }); __MODS__[modId].status = 1; __MODS__[modId].func(__MODS__[modId].req, m, m.exports); } return __MODS__[modId].m.exports; };
var __REQUIRE_WILDCARD__ = function(obj) { if(obj && obj.__esModule) { return obj; } else { var newObj = {}; if(obj != null) { for(var k in obj) { if (Object.prototype.hasOwnProperty.call(obj, k)) newObj[k] = obj[k]; } } newObj.default = obj; return newObj; } };
var __REQUIRE_DEFAULT__ = function(obj) { return obj && obj.__esModule ? obj.default : obj; };
__DEFINE__(1730294775026, function(require, module, exports) {
var _ = require('lodash');
var fs = require('fs');
var paths = require('./paths');
var env = module.exports = {
getConfigFromFile: getConfigFromFile
};
var converters = {
string: { // Any object that implements `toString`
parse: function (x) { return x != null && x.toString(); },
validate: function (x) { return _.isString(x); }
},
number: { // Numbers and strings that can be parsed into numbers
parse: parseFloat,
validate: function (x) { return !isNaN(x); }
},
boolean: { // `true`, `false`, `'true'` and `'false'`
parse: function (x) {
if (typeof x === 'boolean') return x;
if (x === 'true') return true;
if (x === 'false') return false;
},
validate: _.isBoolean
},
date: { // Any string for which `new Date()` will return a valid date
parse: function (x) { return new Date(x); },
validate: _.isDate
},
object: { // Parse JSON objects
parse: JSON.parse,
validate: _.isObject
},
function: { // Any valid JavaScript expression; `this` refers to `env`
parse: function (x) { return eval(x); } // jshint ignore: line
}
};
// Add environment variables from `process.env`
_.extend(env, process.env);
// Parse environment variables from `env.json`
var config = getConfigFromFile(paths.CONFIG);
_.each(config, parseAndSetEnvironmentVariableOptions);
function getConfigFromFile(fileName) {
var configFilePath = paths.get(fileName);
var configFileContent;
try {
configFileContent = fs.readFileSync(configFilePath, 'utf8');
}
catch (error) {
if (error.code === 'ENOENT') {
console.error('File not found: \'' + configFilePath + '\'! Continuing...');
}
else {
throw error;
}
}
var parsedConfig;
if (configFileContent) {
try {
parsedConfig = JSON.parse(configFileContent);
}
catch (error) {
console.error(error && error.message || error);
}
}
if (!parsedConfig) {
parsedConfig = {};
}
return parsedConfig;
}
function parseAndSetEnvironmentVariableOptions(options, key) {
if (!_.isObject(options)) options = { default: options }; // Shorthand notation
var processValueIsDefined = process.env[key] !== undefined;
var defaultValueIsDefined = options.default !== undefined;
var environmentValueIsDefined = options[env.NODE_ENV] !== undefined;
if (processValueIsDefined || defaultValueIsDefined || environmentValueIsDefined) {
var value;
var defaultValueKey = environmentValueIsDefined ? env.NODE_ENV : 'default'; // Use corresponding key to get default value
if (processValueIsDefined) { // Values from `process.env` take precedence over `env.json`
if (options.type === undefined) {
var defaultValue = options[defaultValueKey];
if (defaultValue !== undefined) { // Infer type from default value
options.type = typeof defaultValue;
}
else { // `process.env` can only contain strings anyway
options.type = 'string';
}
}
value = process.env[key];
}
else if (defaultValueIsDefined || environmentValueIsDefined) { // The condition is just for clarity, it's always true
value = options[defaultValueKey];
if (value === undefined && !options.required) return; // Don't enforce convertion
_.defaults(options, { type: typeof value }); // Infer type from default value if necessary
process.env[key] = value; // Put the value into `process.env`
}
var converter = converters[options.type];
if (!converter) {
console.error('Unsupported type for environment variable `' + key + '`: ' + options.type);
process.exit(1);
}
var parsedValue = _.isString(value) ?
converter.parse.call(env, value) : // Call with `env` to allow for variable interdependency
value; // Only parse the value if it's a string that could've come from `process.env`
if (converter.validate && !converter.validate(parsedValue)) {
console.error('Invalid value for environment variable `' + key + '`: ' + value);
process.exit(1);
}
env[key] = parsedValue;
}
else if (options.required) {
console.error('Required environment variable `' + key + '`');
process.exit(1);
}
}
}, function(modId) {var map = {"./paths":1730294775027}; return __REQUIRE__(map[modId], modId); })
__DEFINE__(1730294775027, function(require, module, exports) {
var path = require('path');
exports.CONFIG = process.env.ENV_VAR_CONFIG_FILE || 'env.json';
exports.get = (fileName) => path.resolve(process.cwd(), fileName);
}, function(modId) { var map = {}; return __REQUIRE__(map[modId], modId); })
return __REQUIRE__(1730294775026);
})()
//miniprogram-npm-outsideDeps=["lodash","fs","path"]
//# sourceMappingURL=index.js.map