UNPKG

declapract

Version:

A tool to declaratively define best practices, maintainable evolve them, and scalably enforce them.

58 lines 2.39 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.serviceClientStage = exports.stage = exports.isOfStage = exports.Stage = void 0; const error_fns_1 = require("@ehmpathy/error-fns"); const type_fns_1 = require("type-fns"); var Stage; (function (Stage) { Stage["PRODUCTION"] = "prod"; Stage["DEVELOPMENT"] = "dev"; Stage["TEST"] = "test"; })(Stage || (exports.Stage = Stage = {})); exports.isOfStage = (0, type_fns_1.createIsOfEnum)(Stage); /** * verify that the server is on UTC timezone * * why? * - non UTC timezone usage causes problems and takes a while to track down * - by failing fast if the server our code runs in is not in UTC, we avoid these issues * => * - create a pit of success */ const TIMEZONE = process.env.TZ; if (TIMEZONE !== 'UTC') throw new error_fns_1.UnexpectedCodePathError('env.TZ is not set to UTC. this can cause issues. please set the env var', { found: TIMEZONE, desire: 'UTC' }); /** * this allows us to infer what the stage should be in environments that do not have STAGE specified * - e.g., when running locally * - e.g., when running tests */ const inferStageFromNodeEnv = () => { const nodeEnv = process.env.NODE_ENV; // default to test if not defined if (!nodeEnv) throw new Error('process.env.NODE_ENV must be defined'); if (nodeEnv === 'production') return Stage.PRODUCTION; if (nodeEnv === 'development') return Stage.DEVELOPMENT; if (nodeEnv === 'test') return Stage.TEST; throw new Error(`unexpected nodeEnv '${nodeEnv}'`); }; /** * a method that exposes relevant environmental variables in a standard way */ const getEnvironment = () => { var _a; const stage = (_a = process.env.STAGE) !== null && _a !== void 0 ? _a : inferStageFromNodeEnv(); // figure it out from NODE_ENV if not explicitly defined if (!stage) throw new Error('process.env.STAGE must be defined'); if (!(0, exports.isOfStage)(stage)) throw new Error(`invalid stage defined '${stage}'`); return { stage }; }; // export stage immediately, since it does not change exports.stage = getEnvironment().stage; // export service client stage exports.serviceClientStage = exports.stage === Stage.PRODUCTION ? Stage.PRODUCTION : Stage.DEVELOPMENT; // i.e., if its prod, hit prod. otherwise, dev //# sourceMappingURL=environment.js.map