Skip to content

Commit

Permalink
add trims and udpate REAMDE
Browse files Browse the repository at this point in the history
  • Loading branch information
tuchk4 committed Dec 22, 2016
1 parent 1edb34b commit 2cc4453
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 15 deletions.
7 changes: 6 additions & 1 deletion README.md
Expand Up @@ -25,8 +25,13 @@ API_SECURITY_TOKEN = access_token
NODE_PATH = src/scripts
PORT = 9001
```
*react-app-env* will automatically add `REACT_APP` prefix to each env variable expect:

* PORT - dev server port
* NODE_PATH - directory name to be resolved to the current directory as well as its ancestors, and searched for modules. It is [resolve.modulesDirectories](https://webpack.github.io/docs/configuration.html#resolve-modulesdirectories) for webpack. More details at node official doc ["Loading from the global folders"](https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders)

With this environment file defined above:

With this environment file
```
react-app-env start
```
Expand Down
2 changes: 1 addition & 1 deletion __tests__/index.js
Expand Up @@ -16,7 +16,7 @@ describe('parse env variables', () => {
"REACT_APP_API_SECURITY_TOKEN": "access_token",

"NODE_PATH": "/src",
"PORT": "9001",
"PORT": "9001"
});
});
});
14 changes: 7 additions & 7 deletions __tests__/variables.env
@@ -1,9 +1,9 @@
GOOGLE_CLIENT_ID=XXX_YYY_ZZZ
GOOGLE_CLIENT_ID = XXX_YYY_ZZZ

API_PROTOCOL=http:
API_HOST=localhost:9876
API_PREFIX=api
API_SECURITY_TOKEN=access_token
API_PROTOCOL = http:
API_HOST = localhost:9876
API_PREFIX = api
API_SECURITY_TOKEN = access_token

NODE_PATH=/src
PORT=9001
NODE_PATH = /src
PORT = 9001
15 changes: 10 additions & 5 deletions lib/parse-env-file.js
Expand Up @@ -17,14 +17,19 @@ const parseEnvFile = (file) => {
const parts = row.split('=');

if (parts[0] && parts[1]) {
let varName = null;
if (EXPECT_VARIABLES.indexOf(parts[0]) === -1) {
varName = `REACT_APP_${parts[0]}`.replace(/\s/g, '');
let varName = parts[0].trim();

if (varName.indexOf(' ') !== -1) {
throw new Error(`spaces are not allowed at evn var names. "${varName}"`);
}

if (EXPECT_VARIABLES.indexOf(varName) === -1) {
varName = `REACT_APP_${varName}`;
} else {
varName = parts[0].replace(/\s/g, '');
varName = varName;
}

variables[varName] = parts[1];
variables[varName] = parts[1].trim();
}

return variables;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -4,7 +4,7 @@
"description": "Run create-react-app application with env variables",
"main": "lib/index.js",
"scripts": {
"test": "babel -d ./ ./lib",
"test": "jest --coverage",
"test-dev": "jest --coverage --watch"
},
"bin": {
Expand Down

0 comments on commit 2cc4453

Please sign in to comment.