GithubHelp home page GithubHelp logo

markoivanetic / env.sh Goto Github PK

View Code? Open in Web Editor NEW

This project forked from sparanoid/env.sh

0.0 1.0 0.0 26 KB

Dead-simple .env file reader and generator

License: Apache License 2.0

Shell 97.21% JavaScript 2.79%

env.sh's Introduction

env.sh

Dead-simple .env file reader and generator

Features

  • Designed to be used for Next.js app inside a Docker container (General React app should also work)
  • Read env file and generate __env.js file for runtime client use
  • Merge current environment variables passing to it (Useful for Docker images)
  • No dependencies (More like a lite version of react-env). But does not require you to build your project inside the container. The benefit of this method can help you avoid multistage Dockerfile. You can build your Next.js project in GitHub Actions. And only COPY built files required for production. With this method your image can be much smaller.

Usage

Simply copy env.sh to the root of your project. And follow the steps.

General usage:

$ ./env.sh

Replacing varaible:

$ NEXT_PUBLIC_API_BASE=xxx ./env.sh

Enviroment variable not in whitelist will be discarded:

$ BAD_ENV=zzz ./env.sh

Change script options:

$ ENVSH_ENV="./.env.staging" ENVSH_OUTPUT="./public/config.js" ./env.sh

Use it with your existing project. Modify your scripts in package.json:

"scripts": {
  "dev": "bash env.sh next dev",
  "build": "CI=true bash env.sh next build",
}

Create a utils.js to use it in runtime client:

// Simplified from:
// https://github.com/andrewmclagan/react-env/blob/master/packages/node/src/index.js
export function env(key = '') {
  if (!key.length) {
    throw new Error('No env key provided');
  }

  if (isBrowser() && window.__env) {
    return window.__env[key] === "''" ? '' : window.__env[key];
  }

  return process.env[key] === "''" ? '' : process.env[key];
}

In _document.js:

<Head>
  <script src="/__env.js" />
</Head>

In MyComponent.js:

import { env } from 'utils';
const API_BASE = env('CUSTOM_API_BASE');

Now you can run yarn dev and see API_BASE dynamically injected to your app for client-side rendering.

Use it inside Dockerfile:

Install bash for your image first (Here operator used in this script does not work with sh at the moment. This can be improved in the furture):

RUN apk add --no-cache bash

# ...other build steps

RUN chmod +x ./env.sh
ENTRYPOINT ["./env.sh"]

# ...custom next.js server if required
CMD ["node", "server.js"]

Options

ENVSH_ENV

Specify env file to read.

Default: ./.env

ENVSH_PREFIX

Only environment variables with this prefix will be matched.

Default: NEXT_PUBLIC_

ENVSH_PREFIX_STRIP

If set to true, ENVSH_PREFIX will be stripped from the variable name:

window.__env = {
API: "https://openbayes.com/",
DEBUG: "true",
}

When set to false:

window.__env = {
NEXT_PUBLIC_API: "https://openbayes.com/",
NEXT_PUBLIC_DEBUG: "true",
}

Default: true

ENVSH_PREPEND

String to be prepended to the output config name. If you change this, you should also change the way to access it in utils.js.

Default: window.__env = {

ENVSH_APPEND

String to be appended to the output config name.

Default: }

ENVSH_OUTPUT

The filename of the output config file.

Default: ./public/__env.js

ENVSH_VERBOSE

Debug level output.

Default: false

Security Alert

This script is used to read environment variables from a file and inject matched variables for client-side use. So It's not safe to prepend your private API keys or tokens with ENVSH_PREFIX. If you do that these variables will be available in ENVSH_OUTPUT file and exposed to the clients.

License

Apache-2.0

env.sh's People

Contributors

sparanoid avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.