Skip to content

Commit ca019ee

Browse files
authored
Merge pull request #17 from platformsh/allow-custom-prefix
Allow custom prefix
2 parents 4cde147 + 6f1288a commit ca019ee

File tree

8 files changed

+150
-74
lines changed

8 files changed

+150
-74
lines changed

.github/workflows/npm-publish.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
name: Publish (npm)
3+
on:
4+
push: ~
5+
6+
jobs:
7+
deploy:
8+
name: Publish to npm
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v2
12+
- uses: actions/setup-node@v2
13+
with:
14+
node-version: '10.x'
15+
registry-url: 'https://registry.npmjs.org'
16+
- run: npm install
17+
- if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags')
18+
env:
19+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
20+
run: npm publish
21+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
name: Quality Assurance
3+
on:
4+
push: ~
5+
pull_request: ~
6+
7+
jobs:
8+
build:
9+
name: '[Build/test] Node.js ${{ matrix.nodejs }}'
10+
runs-on: ubuntu-latest
11+
strategy:
12+
matrix:
13+
nodejs: [ '10', '12', '14' ]
14+
steps:
15+
- uses: actions/checkout@v2
16+
- uses: actions/setup-node@v1
17+
with:
18+
node-version: ${{ matrix.nodejs }}
19+
- run: npm install
20+
- run: npm run build --if-present
21+
- run: npm test

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
# Changelog
22

3+
## [2.4.0] - 2021-02-03
4+
5+
### Added
6+
7+
* GitHub actions for tests (`quality-assurance.yaml`) and publishing to npm (`npm-publish.yaml`).
8+
9+
### Changed
10+
11+
* `config` method can now get an object `{ varPrefix: string }` to specify a different environment variables prefix.
12+
13+
### Removed
14+
15+
* CircleCI action config.
16+
317
## [2.3.1] - 2019-11-04
418

519
### Added

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Platform.sh Config Reader (Node.js)
22

3+
![Quality Assurance](https://github.com/platformsh/config-reader-nodejs/workflows/Quality%20Assurance/badge.svg)
4+
35
This library provides a streamlined and easy to use way to interact with a Platform.sh environment. It offers utility methods to access routes and relationships more cleanly than reading the raw environment variables yourself.
46

57
This library requires Node.js 10 or later.
@@ -106,6 +108,13 @@ config.socket;
106108
config.port;
107109
```
108110

111+
By default, Platform.sh environment variables are prefixed with `PLATFORM_`. In some cases, you might need to change this default in order to have access to environment variables at build time (like with [create-react-app](https://create-react-app.dev/docs/adding-custom-environment-variables/)).
112+
113+
You can do this like so:
114+
```js
115+
const config = require("platformsh-config").config({ varPrefix: "MY_PREFIX_" });
116+
```
117+
109118
### Reading service credentials
110119

111120
[Platform.sh services](https://docs.platform.sh/configuration/services.html) are defined in a `services.yaml` file, and exposed to an application by listing a `relationship` to that service in the application's `.platform.app.yaml` file. User, password, host, etc. information is then exposed to the running application in the `PLATFORM_RELATIONSHIPS` environment variable, which is a base64-encoded JSON string. The following method allows easier access to credential information than decoding the environment variable yourself.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "platformsh-config",
3-
"version": "2.3.1",
3+
"version": "2.4.0",
44
"description": "Helper for running nodejs applications on Platform.sh",
55
"main": "lib/platformsh.js",
66
"keywords": [

src/platformsh.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ function decode(value) {
2424
*/
2525
class Config {
2626

27-
constructor(env = null, prefix = 'PLATFORM_') {
27+
constructor(env = null, varPrefix = 'PLATFORM_') {
2828
this.environmentVariables = env || process.env;
29-
this.envPrefix = prefix;
29+
this.varPrefix = varPrefix;
3030

3131
// Node doesn't support pre-defined object properties in classes, so
3232
// this is mostly for documentation but also to ensure there's always
@@ -567,7 +567,7 @@ class Config {
567567
* @return {string|null}
568568
*/
569569
_getValue(name) {
570-
let checkName = this.envPrefix + name.toUpperCase();
570+
let checkName = this.varPrefix + name.toUpperCase();
571571

572572
return this.environmentVariables[checkName] || null;
573573
}
@@ -619,9 +619,9 @@ function puppeteerFormatter(credentials) {
619619
*
620620
* @returns {Config}
621621
*/
622-
function config() {
622+
function config({ varPrefix } = {}) {
623623

624-
return new Config();
624+
return new Config(null, varPrefix);
625625
}
626626

627627
module.exports = {

test/testdata/ENV.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@
55
"PLATFORM_TREE_ID": "abc123",
66
"PLATFORM_PROJECT_ENTROPY": "def789",
77

8-
"SOME_VARIABLE": "some value"
8+
"SOME_VARIABLE": "some value",
9+
10+
"CUSTOM_PREFIX_VARIABLE_WITH_CUSTOM_PREFIX": "with custom prefix"
911
}

0 commit comments

Comments
 (0)