Skip to content

Commit

Permalink
docs: add README.md and comments in cra example
Browse files Browse the repository at this point in the history
  • Loading branch information
JoseVSeb committed Feb 14, 2024
1 parent b5a7669 commit 41573c7
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
*.html
*.ico
Dockerfile
*.json
*.json
*.md
89 changes: 89 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# runtime-env-cli

[![npm](https://img.shields.io/npm/v/runtime-env-cli.svg)](https://www.npmjs.org/package/runtime-env-cli)
[![npm](https://img.shields.io/npm/dm/runtime-env-cli.svg)](https://www.npmjs.org/package/runtime-env-cli)
[![GitHub](https://img.shields.io/github/issues/JoseVSeb/runtime-env.svg)](https://github.com/JoseVSeb/runtime-env/issues)
[![release workflow](https://github.com/JoseVSeb/runtime-env/actions/workflows/release.yaml/badge.svg)](https://github.com/JoseVSeb/runtime-env/actions/workflows/release.yaml)
[![npm](https://img.shields.io/npm/l/runtime-env-cli.svg)](https://www.npmjs.org/package/runtime-env-cli)
[![semantic-release: conventional commit](https://img.shields.io/badge/semantic--release-conventionalcommit-e10079?logo=semantic-release)](https://github.com/semantic-release/semantic-release)

## Overview

`runtime-env-cli` is an NPM package that helps manage environment variables in web applications. It allows environment-agnostic build for frameworks like create-react-app and inject environment variables as `window.env` in html files during server startup.

## Usage

### Development Mode

1. Install `runtime-env-cli` as development dependency.

pnpm:

```bash
pnpm i -D runtime-env-cli
```

npm:

```bash
npm i -D runtime-env-cli
```

Yarn:

```bash
pnpm add -D runtime-env-cli
```

2. Import `runtime-env-cli` in top-level javascript/typescript file for aliasing `process.env` as `windows.env` in development mode.

```ts
import "runtime-env-cli";
```

It also provides type-safety for `windows.env`.

Note: You can make it further type-safe by extending interfaces `ProcessEnv` or `WindowEnv`.

Use environment variables from `window.env`:

```ts
window.env.SAMPLE_ENV;
```

### Production Mode

1. Generate static files for your application.

```bash
npm run build
```

2. Install `runtime-env-cli` in hosting environment (if using docker, do this in the last stage).

```bash
npm i -g runtime-env-cli
```

3. Run the cli to inject env into html files before serving files (in docker, do this in entrypoint following by serving files, refer [example](https://github.com/JoseVSeb/runtime-env/tree/main/examples/cra)).

```bash
runtime-env [options]
```

Note: CLI uses `process.env` to get environment while injecting. Any matching environment variables already configured in shell will be read. To use .env files, first load it before running this (refer [dotenv-cli](https://www.npmjs.com/package/dotenv-cli)).

### CLI Options

- **-v, --version**: output the version number.
- **-p, --path [paths...]** (optional): glob patterns to match path of html files (default: ["./build/**/*.html"]).
- **-m, --match \<regex\>**: regular expression to match environment variable names to be injected.
- **-h, --help**: display help for command.

#### example

```bash
runtime-env --path "./**/*.html" --match "^APP_ENV_"
```

This will inject all environment variables prefixed with `APP_ENV_` as `window.env` object into all html files under working directory.
1 change: 1 addition & 0 deletions examples/cra/.env
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# environment variable to be used for development server
REACT_APP_SAMPLE_ENV = "This is for development server."
2 changes: 2 additions & 0 deletions examples/cra/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ RUN pnpm build

FROM base as runner
WORKDIR /app
# install runtime-env-cli for production server
RUN npm i -g serve runtime-env-cli
COPY --from=builder /workspace/build .
COPY scripts/start_server.sh .
RUN chmod +x start_server.sh
# server startup shell script as entrypoint
ENTRYPOINT [ "./start_server.sh" ]
1 change: 1 addition & 0 deletions examples/cra/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ services:
ports:
- "3000:3000"
environment:
# environment variable provided at runtime
- REACT_APP_SAMPLE_ENV=injected from docker compose
2 changes: 1 addition & 1 deletion examples/cra/scripts/start_server.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/sh

# inject matching env variables in built html
# inject matching env variables in built html on server startup
runtime-env -m ^REACT_APP_ -p ./*.html

# serve the static files
Expand Down
1 change: 1 addition & 0 deletions examples/cra/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from "react";

export default function App() {
// Access environment variable from `window.env` object
return <div>REACT_APP_SAMPLE_ENV: {window.env.REACT_APP_SAMPLE_ENV}</div>;
}
2 changes: 2 additions & 0 deletions examples/cra/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App";

// `window.env` is now type-safe and loads `process.env` in development mode.
import "runtime-env-cli";

const root = ReactDOM.createRoot(
Expand Down
1 change: 1 addition & 0 deletions examples/cra/src/react-app-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

declare namespace NodeJS {
interface ProcessEnv {
// provide type safety for environment variable
REACT_APP_SAMPLE_ENV: string;
}
}

0 comments on commit 41573c7

Please sign in to comment.