Skip to content

Commit

Permalink
load env vars from .env file
Browse files Browse the repository at this point in the history
  • Loading branch information
hipstersmoothie committed Jan 20, 2019
1 parent 7c73925 commit db495bb
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 0 deletions.
7 changes: 7 additions & 0 deletions docs/pages/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ You must configure some environment variables for publishing and releasing to wo
- `GH_TOKEN` - Used for updating the changelog and publishing the GitHub release
- `NPM_TOKEN` - Used to publish to npm.

You can also store these values in a local file at the root of your project named `.env`. You should make sure to add this file to your `.gitignore` so you don't commit any keys!

```bash
GH_TOKEN=YOUR_TOKEN
NPM_TOKEN=PUBLISH_TOKEN
```

If you are publishing from the CI you must inject the `NPM_TOKEN` into your `.npmrc`.

```sh
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"command-line-args": "^5.0.2",
"command-line-usage": "^5.0.5",
"cosmiconfig": "5.0.7",
"dotenv": "^6.2.0",
"enquirer": "^2.3.0",
"env-ci": "^3.2.0",
"get-monorepo-packages": "^1.1.0",
Expand All @@ -60,6 +61,7 @@
"@types/command-line-args": "^5.0.0",
"@types/command-line-usage": "^5.0.1",
"@types/cosmiconfig": "5.0.3",
"@types/dotenv": "^6.1.0",
"@types/env-ci": "^3.1.0",
"@types/jest": "~23.3.12",
"@types/node": "~10.12.18",
Expand Down
22 changes: 22 additions & 0 deletions src/__tests__/main-env.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { AutoRelease } from '../main';

jest.mock('fs', () => ({
readFileSync: () => 'FOO="test value"',
closeSync: () => undefined,
existsSync: () => undefined,
readFile: () => undefined,
ReadStream: () => undefined,
WriteStream: () => undefined,
writeFile: () => undefined
}));

test('should load .env file', async () => {
const auto = new AutoRelease({
command: 'init',
owner: 'foo',
repo: 'bar'
});

expect(auto).toBeDefined();
expect(process.env.FOO).toBe('test value');
});
3 changes: 3 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env node

import cosmiconfig from 'cosmiconfig';
import env from 'dotenv';
import envCi from 'env-ci';
import { gt, inc, ReleaseType } from 'semver';

Expand Down Expand Up @@ -73,6 +74,8 @@ export class AutoRelease {
args.veryVerbose ? 'veryVerbose' : args.verbose ? 'verbose' : undefined
);
this.hooks = makeHooks();

env.config();
}

public async loadConfig() {
Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1117,6 +1117,13 @@
dependencies:
"@types/node" "*"

"@types/dotenv@^6.1.0":
version "6.1.0"
resolved "https://registry.yarnpkg.com/@types/dotenv/-/dotenv-6.1.0.tgz#fba2bfd1f28a46eadaa049f3313ebb89bdedfc53"
integrity sha512-gmbNb7V1LbJQA4MmH0hVFgqY1cyKsa6RvKC1Xrq0WBnZ0JuuvXKciXx/s8dN0LVXCJd8xO6wIaSFSyUIoGph9g==
dependencies:
"@types/node" "*"

"@types/env-ci@^3.1.0":
version "3.1.0"
resolved "https://registry.yarnpkg.com/@types/env-ci/-/env-ci-3.1.0.tgz#8b8b388641c641fa4eb51862cbb018db013aeb68"
Expand Down Expand Up @@ -3775,6 +3782,11 @@ dot-prop@^4.1.0, dot-prop@^4.1.1:
dependencies:
is-obj "^1.0.0"

dotenv@^6.2.0:
version "6.2.0"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-6.2.0.tgz#941c0410535d942c8becf28d3f357dbd9d476064"
integrity sha512-HygQCKUBSFl8wKQZBSemMywRWcEDNidvNbjGVyZu3nbZ8qq9ubiPoGLMdRDpfSrpkkm9BXYFkpKxxFX38o/76w==

download@^6.2.2:
version "6.2.5"
resolved "https://registry.yarnpkg.com/download/-/download-6.2.5.tgz#acd6a542e4cd0bb42ca70cfc98c9e43b07039714"
Expand Down

0 comments on commit db495bb

Please sign in to comment.