├── env-load-tests.js ├── package.js ├── LICENSE ├── env-load.es6.js └── README.md /env-load-tests.js: -------------------------------------------------------------------------------- 1 | // Write your tests here! 2 | // Here is an example. 3 | Tinytest.add('example', function (test) { 4 | test.equal(true, true); 5 | }); 6 | -------------------------------------------------------------------------------- /package.js: -------------------------------------------------------------------------------- 1 | Package.describe({ 2 | name: 'poetic:env-load', 3 | version: '0.0.1', 4 | summary: 'Stop a Meteor app on startup if any environment variables are missing as per environment.json.', 5 | git: 'https://github.com/poetic/meteor-env-load', 6 | documentation: 'README.md' 7 | }); 8 | 9 | Package.onUse(function(api) { 10 | api.versionsFrom('1.2.0.1'); 11 | api.use('ecmascript'); 12 | api.addFiles('env-load.es6.js'); 13 | }); 14 | 15 | Package.onTest(function(api) { 16 | api.use('ecmascript'); 17 | api.use('tinytest'); 18 | api.use('poetic:env-load'); 19 | api.addFiles('env-load-tests.js'); 20 | }); 21 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Poetic 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /env-load.es6.js: -------------------------------------------------------------------------------- 1 | if (Meteor.isServer) { 2 | EnvLoad = { 3 | loadSettings() { 4 | var envFile = 'environment.json'; 5 | 6 | return new Promise((resolve, reject) => { 7 | return GlobalAssets.getText(envFile, (err, data) => { 8 | if (err) { 9 | reject(err); 10 | } else { 11 | resolve(JSON.parse(data)); 12 | } 13 | }); 14 | }); 15 | }, 16 | 17 | status() { 18 | return new Promise(resolve => { 19 | return this.loadSettings().then(settings => { 20 | var keys = Object.keys(settings); 21 | 22 | var missingKeys = keys.filter(key => { 23 | if (settings[key] && !process.env[key]) { 24 | return key; 25 | } 26 | }); 27 | 28 | if (missingKeys.length > 0) { resolve(false); } 29 | 30 | resolve(true); 31 | }).catch(this.error.bind(this, '#loadSettings')); 32 | }); 33 | }, 34 | 35 | error(method, err) { 36 | console.log(`${method}: ${err}`); 37 | process.exit(1); 38 | } 39 | } 40 | 41 | Meteor.startup(() => { 42 | return EnvLoad.status().then(status => { 43 | if (!status) { 44 | console.log('Missing one or more environment variables.'); 45 | process.exit(1); 46 | } 47 | }); 48 | }); 49 | } 50 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # meteor-env-load 2 | 3 | `meteor add poetic:env-load` 4 | 5 | This package will prevent your app from starting when environment variables are not defined, this way you don't have to worry about all of those `process.env.VAR_NAME` references in your Meteor app. 6 | 7 | ### Quick Start 8 | 9 | - Install the env-load package itself into your Meteor application 10 | - Place the following snippet into any file in `/server` such as `/server/env-load.js` or within a `Meteor.isServer()` block: 11 | ```Javascript 12 | GlobalAssets = Assets; 13 | ``` 14 | 15 | - Add `private/environment.json`, containing something like: 16 | ```JSON 17 | { 18 | "SECRET_KEY": true, 19 | "OPTIONAL_KEY": false 20 | } 21 | ``` 22 | 23 | ### `private/environment.json` 24 | 25 | The keys in this file represent the environment variables used in various places within your Meteor app. 26 | 27 | For example, an `environment.json` with the contents of: 28 | 29 | ```JSON 30 | { 31 | "SECRET_KEY": true 32 | } 33 | ``` 34 | 35 | means that you are using `process.env.SECRET_KEY` within your app and it is required, so if it is not set, your app will not start. You can use a value of `false` to indicate that an environment variable is optional so that your app will still start up without it. This is useful for documenting variables that may only be relevant to development, testing, or a different deployment environment. 36 | 37 | ### Other Notes 38 | 39 | Using `GlobalAssets = Assets` is neccessary due to the fact that Assets.getText within a Meteor package is limited to that package's assets. By assigning an app's Assets to GlobalAssets, we then have access from within our Meteor package. 40 | 41 | ### Running Tests 42 | 43 | TODO 44 | --------------------------------------------------------------------------------