Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .github/workflows/npm-publish.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
name: Publish (npm)
on:
push: ~

jobs:
deploy:
name: Publish to npm
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: '10.x'
registry-url: 'https://registry.npmjs.org'
- run: npm install
- if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags')
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
run: npm publish

21 changes: 21 additions & 0 deletions .github/workflows/quality-assurance.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
name: Quality Assurance
on:
push: ~
pull_request: ~

jobs:
build:
name: '[Build/test] Node.js ${{ matrix.nodejs }}'
runs-on: ubuntu-latest
strategy:
matrix:
nodejs: [ '10', '12', '14' ]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: ${{ matrix.nodejs }}
- run: npm install
- run: npm run build --if-present
- run: npm test
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
# Changelog

## [2.4.0] - 2021-02-03

### Added

* GitHub actions for tests (`quality-assurance.yaml`) and publishing to npm (`npm-publish.yaml`).

### Changed

* `config` method can now get an object `{ varPrefix: string }` to specify a different environment variables prefix.

### Removed

* CircleCI action config.

## [2.3.1] - 2019-11-04

### Added
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Platform.sh Config Reader (Node.js)

![Quality Assurance](https://github.com/platformsh/config-reader-nodejs/workflows/Quality%20Assurance/badge.svg)

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.

This library requires Node.js 10 or later.
Expand Down Expand Up @@ -106,6 +108,13 @@ config.socket;
config.port;
```

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/)).

You can do this like so:
Comment thread
RudyWeber marked this conversation as resolved.
```js
const config = require("platformsh-config").config({ varPrefix: "MY_PREFIX_" });
```

### Reading service credentials

[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.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "platformsh-config",
"version": "2.3.1",
"version": "2.4.0",
"description": "Helper for running nodejs applications on Platform.sh",
"main": "lib/platformsh.js",
"keywords": [
Expand Down
10 changes: 5 additions & 5 deletions src/platformsh.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ function decode(value) {
*/
class Config {

constructor(env = null, prefix = 'PLATFORM_') {
constructor(env = null, varPrefix = 'PLATFORM_') {
this.environmentVariables = env || process.env;
this.envPrefix = prefix;
this.varPrefix = varPrefix;

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

return this.environmentVariables[checkName] || null;
}
Expand Down Expand Up @@ -619,9 +619,9 @@ function puppeteerFormatter(credentials) {
*
* @returns {Config}
*/
function config() {
function config({ varPrefix } = {}) {

return new Config();
return new Config(null, varPrefix);
}

module.exports = {
Expand Down
4 changes: 3 additions & 1 deletion test/testdata/ENV.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@
"PLATFORM_TREE_ID": "abc123",
"PLATFORM_PROJECT_ENTROPY": "def789",

"SOME_VARIABLE": "some value"
"SOME_VARIABLE": "some value",

"CUSTOM_PREFIX_VARIABLE_WITH_CUSTOM_PREFIX": "with custom prefix"
}
Loading