@smg-automotive/configuration 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 CAR FOR YOU
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,64 @@
1
+ # SMG Automotive Configuration
2
+
3
+ [![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
4
+
5
+ It loads configuration for a specific stage via `dotenv` from `.env/<CONFIG_ENV>`. If no environment is provided it defaults to `NODE_ENV`.
6
+
7
+ You can add local overrides in `.env/<CONFIG_ENV>.local`. This is useful for temporary or local changes.
8
+
9
+ ## Usage
10
+ ```
11
+ npm install @smg-automotive/configuration
12
+ ```
13
+
14
+ Add the following line to your `.gitignore`
15
+ ```
16
+ /.env/*.local
17
+ ```
18
+
19
+ The configuration environment can be passed via `CONFIG_ENV` environment variable:
20
+
21
+ ```
22
+ $ CONFIG_ENV=stage-prod npm run dev
23
+ ```
24
+
25
+ In a nextjs project, you can call `loadConfiguration()` in `next.config.js` and pass the result to next as `env`, see https://nextjs.org/docs/api-reference/next.config.js/environment-variables - configuration values will be available on `process.env` both client- and server-side
26
+
27
+ ```
28
+ const configuration = require("@smg-automotive/configuration")
29
+ module.exports = {
30
+ env: configuration
31
+ }
32
+ ```
33
+
34
+ In any node process, simply require the package in your entry point and access variables on `process.env`. Do this as early in the file as possible, ie. before requiring any files that are accessing config variables
35
+
36
+ ```
37
+ require("@smg-automotive/configuration")
38
+ ```
39
+
40
+ ## Development
41
+ ```
42
+ npm run build
43
+ ```
44
+
45
+ You can link your local npm package to integrate it with any local project:
46
+ ```
47
+ cd configuration-pkg
48
+ npm run build
49
+
50
+ cd project
51
+ npm link ../configuration-pkg/dist
52
+ ```
53
+
54
+ ## Release a new version
55
+
56
+ New versions are released on the ci using semantic-release as soon as you merge into master. Please
57
+ make sure your merge commit message adheres to the corresponding conventions.
58
+
59
+
60
+ ## Circle CI
61
+
62
+ You will need to enable the repository in circle CI ui to be able to build it.
63
+
64
+ For slack notifications to work you will need to provide the token in circle settings.
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "@smg-automotive/configuration",
3
+ "version": "1.0.0",
4
+ "description": "SMG Automotive configuration conventions",
5
+ "scripts": {
6
+ "version": "npm run build",
7
+ "build": "rimraf pkg && tsc --outDir pkg",
8
+ "lint": "eslint --ext js,json .",
9
+ "format": "npm run lint -- --fix",
10
+ "typecheck": "tsc --noEmit",
11
+ "test": "jest",
12
+ "test:debug": "node --inspect-brk --inspect=127.0.0.1:9229 ./node_modules/jest/bin/jest.js --runInBand"
13
+ },
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://github.com/smg-automotive/configuration-pkg.git"
17
+ },
18
+ "author": "CAR FOR YOU",
19
+ "license": "MIT",
20
+ "homepage": "https://github.com/smg-automotive/configuration-pkg#readme",
21
+ "devDependencies": {
22
+ "@carforyou/eslint-config": "4.0.23",
23
+ "@types/jest": "27.5.2",
24
+ "jest": "27.5.1",
25
+ "rimraf": "3.0.2",
26
+ "semantic-release": "19.0.3",
27
+ "ts-jest": "27.1.5"
28
+ },
29
+ "dependencies": {
30
+ "dotenv": "16.0.1",
31
+ "es-abstract": "1.20.1",
32
+ "typescript": "4.5.5"
33
+ },
34
+ "main": "pkg/index.js",
35
+ "files": [
36
+ "pkg/**/*"
37
+ ],
38
+ "bugs": {
39
+ "url": "https://github.com/smg-automotive/configuration-pkg/issues"
40
+ }
41
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ /**
7
+ * @jest-environment node
8
+ */
9
+ /* eslint-disable @typescript-eslint/no-var-requires */
10
+ var loadConfiguration_1 = __importDefault(require("../loadConfiguration"));
11
+ beforeEach(function () {
12
+ process.env.CONFIG_ENV = null;
13
+ process.env.NODE_ENV = null;
14
+ });
15
+ it("throws error when CONFIG_ENV is undefined", function () {
16
+ expect(function () {
17
+ (0, loadConfiguration_1.default)();
18
+ }).toThrow("Invalid environment");
19
+ });
20
+ it("throws error when CONFIG_ENV has no respective config", function () {
21
+ process.env.CONFIG_ENV = "invalid";
22
+ expect(function () {
23
+ (0, loadConfiguration_1.default)();
24
+ }).toThrow("Invalid environment");
25
+ });
26
+ it("returns configuration", function () {
27
+ process.env.CONFIG_ENV = "test";
28
+ var configuration = (0, loadConfiguration_1.default)();
29
+ expect(configuration.VIDEO_URL).toEqual("https://www.youtube.com/watch?v=dQw4w9WgXcQ");
30
+ });
31
+ it("allows local overrides", function () {
32
+ process.env.CONFIG_ENV = "overrides";
33
+ var configuration = (0, loadConfiguration_1.default)();
34
+ expect(configuration.VIDEO_URL).toEqual("https://www.youtube.com/watch?v=XSqi5s3rfqk");
35
+ });
36
+ //# sourceMappingURL=index.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.test.js","sourceRoot":"","sources":["../../src/__tests__/index.test.ts"],"names":[],"mappings":";;;;;AAAA;;GAEG;AACH,uDAAuD;AACvD,2EAAoD;AAEpD,UAAU,CAAC;IACT,OAAO,CAAC,GAAG,CAAC,UAAU,GAAG,IAAI,CAAA;IAC7B,OAAO,CAAC,GAAG,CAAC,QAAQ,GAAG,IAAI,CAAA;AAC7B,CAAC,CAAC,CAAA;AAEF,EAAE,CAAC,2CAA2C,EAAE;IAC9C,MAAM,CAAC;QACL,IAAA,2BAAiB,GAAE,CAAA;IACrB,CAAC,CAAC,CAAC,OAAO,CAAC,qBAAqB,CAAC,CAAA;AACnC,CAAC,CAAC,CAAA;AAEF,EAAE,CAAC,uDAAuD,EAAE;IAC1D,OAAO,CAAC,GAAG,CAAC,UAAU,GAAG,SAAS,CAAA;IAClC,MAAM,CAAC;QACL,IAAA,2BAAiB,GAAE,CAAA;IACrB,CAAC,CAAC,CAAC,OAAO,CAAC,qBAAqB,CAAC,CAAA;AACnC,CAAC,CAAC,CAAA;AAEF,EAAE,CAAC,uBAAuB,EAAE;IAC1B,OAAO,CAAC,GAAG,CAAC,UAAU,GAAG,MAAM,CAAA;IAC/B,IAAM,aAAa,GAAG,IAAA,2BAAiB,GAAE,CAAA;IACzC,MAAM,CAAC,aAAa,CAAC,SAAS,CAAC,CAAC,OAAO,CACrC,6CAA6C,CAC9C,CAAA;AACH,CAAC,CAAC,CAAA;AAEF,EAAE,CAAC,wBAAwB,EAAE;IAC3B,OAAO,CAAC,GAAG,CAAC,UAAU,GAAG,WAAW,CAAA;IACpC,IAAM,aAAa,GAAG,IAAA,2BAAiB,GAAE,CAAA;IACzC,MAAM,CAAC,aAAa,CAAC,SAAS,CAAC,CAAC,OAAO,CACrC,6CAA6C,CAC9C,CAAA;AACH,CAAC,CAAC,CAAA"}
package/pkg/index.d.ts ADDED
@@ -0,0 +1 @@
1
+ export {};
package/pkg/index.js ADDED
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ var loadConfiguration_1 = __importDefault(require("./loadConfiguration"));
7
+ var configuration = (0, loadConfiguration_1.default)();
8
+ module.exports = configuration;
9
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;AAAA,0EAAmD;AACnD,IAAM,aAAa,GAAG,IAAA,2BAAiB,GAAE,CAAA;AAEzC,MAAM,CAAC,OAAO,GAAG,aAAa,CAAA"}
@@ -0,0 +1,5 @@
1
+ interface Configuration {
2
+ [key: string]: string | number | boolean;
3
+ }
4
+ declare const loadConfiguration: () => Configuration;
5
+ export default loadConfiguration;
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __importDefault = (this && this.__importDefault) || function (mod) {
14
+ return (mod && mod.__esModule) ? mod : { "default": mod };
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ /* eslint-disable @typescript-eslint/naming-convention */
18
+ var path_1 = __importDefault(require("path"));
19
+ var fs_1 = __importDefault(require("fs"));
20
+ var dotenv_1 = __importDefault(require("dotenv"));
21
+ if (typeof window !== "undefined") {
22
+ throw new Error("It looks like you're loading the configuration in the browser. Use process.env.VARIABLE instead");
23
+ }
24
+ var loadEnvFile = function (configPath) {
25
+ var configuration = dotenv_1.default.config({ path: configPath });
26
+ // eslint-disable-next-line no-console
27
+ console.info("> Loading configuration from ".concat(configPath));
28
+ if (configuration.error) {
29
+ throw configuration.error;
30
+ }
31
+ return configuration.parsed;
32
+ };
33
+ var loadConfiguration = function () {
34
+ var envDir = path_1.default.join(process.cwd(), ".env");
35
+ var environments = fs_1.default.readdirSync(envDir);
36
+ var nodeEnv = process.env.NODE_ENV || "development";
37
+ var env = process.env.CONFIG_ENV || nodeEnv;
38
+ var localEnv = "".concat(env, ".local");
39
+ var version = process.env.VERSION;
40
+ process.env.CONFIG_ENV = env;
41
+ if (!environments.includes(env)) {
42
+ throw new Error("Invalid environment: ".concat(env));
43
+ }
44
+ var configPath = path_1.default.resolve(envDir, env);
45
+ var configuration = __assign({ CONFIG_ENV: env, VERSION: version }, loadEnvFile(configPath));
46
+ if (environments.includes(localEnv)) {
47
+ var localConfiguration = loadEnvFile(path_1.default.resolve(envDir, localEnv));
48
+ return __assign(__assign({}, configuration), localConfiguration);
49
+ }
50
+ return configuration;
51
+ };
52
+ exports.default = loadConfiguration;
53
+ //# sourceMappingURL=loadConfiguration.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"loadConfiguration.js","sourceRoot":"","sources":["../src/loadConfiguration.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,yDAAyD;AACzD,8CAAuB;AACvB,0CAAmB;AACnB,kDAA2B;AAM3B,IAAI,OAAO,MAAM,KAAK,WAAW,EAAE;IACjC,MAAM,IAAI,KAAK,CACb,iGAAiG,CAClG,CAAA;CACF;AAED,IAAM,WAAW,GAAG,UAAC,UAAkB;IACrC,IAAM,aAAa,GAAG,gBAAM,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,UAAU,EAAE,CAAC,CAAA;IAEzD,sCAAsC;IACtC,OAAO,CAAC,IAAI,CAAC,uCAAgC,UAAU,CAAE,CAAC,CAAA;IAE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