@scaleway/configuration-loader 0.1.0-beta.3 → 0.1.0-beta.4

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/CHANGELOG.md CHANGED
@@ -3,13 +3,13 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
- ## 0.1.0-beta.3 (2022-08-08)
6
+ ## 0.1.0-beta.4 (2022-11-23)
7
7
 
8
8
  **Note:** Version bump only for package @scaleway/configuration-loader
9
9
 
10
+ ## 0.1.0-beta.3 (2022-08-08)
10
11
 
11
-
12
-
12
+ **Note:** Version bump only for package @scaleway/configuration-loader
13
13
 
14
14
  ## [0.1.0-beta.2](https://github.com/scaleway/scaleway-sdk-js/compare/@scaleway/configuration-loader@0.1.0-beta.1...@scaleway/configuration-loader@0.1.0-beta.2) (2022-07-13)
15
15
 
@@ -10,7 +10,6 @@ import { loadConfigurationFromFile } from './yml-loader.js';
10
10
  *
11
11
  * @public
12
12
  */
13
-
14
13
  const loadProfileFromEnvironmentValues = () => ({
15
14
  accessKey: env[EnvironmentKey.ScwAccessKey],
16
15
  apiURL: env[EnvironmentKey.ScwAPIURL],
@@ -20,6 +19,7 @@ const loadProfileFromEnvironmentValues = () => ({
20
19
  defaultZone: env[EnvironmentKey.ScwDefaultZone],
21
20
  secretKey: env[EnvironmentKey.ScwSecretKey]
22
21
  });
22
+
23
23
  /**
24
24
  * Loads profile from configuration file.
25
25
  *
@@ -32,22 +32,17 @@ const loadProfileFromEnvironmentValues = () => ({
32
32
  *
33
33
  * @public
34
34
  */
35
-
36
35
  const loadProfileFromConfigurationFile = params => {
37
36
  const filePath = (params == null ? void 0 : params.filepath) ?? resolveConfigurationFilePath();
38
-
39
37
  if (typeof filePath !== 'string' || filePath.length === 0) {
40
38
  throw new Error('Could not find the path to the configuration file.');
41
39
  }
42
-
43
40
  const configs = loadConfigurationFromFile(filePath);
44
41
  const profileName = (params == null ? void 0 : params.profileName) ?? 'default';
45
42
  const profileMap = configs[profileName];
46
-
47
43
  if (typeof profileMap !== 'object') {
48
44
  throw new Error(`Could not find the desired profile '${profileName}' in the configuration file.`);
49
45
  }
50
-
51
46
  return {
52
47
  accessKey: profileMap.access_key,
53
48
  apiURL: profileMap.api_url,
package/dist/env.js CHANGED
@@ -2,7 +2,6 @@
2
2
  * Environment Key.
3
3
  */
4
4
  let EnvironmentKey;
5
-
6
5
  (function (EnvironmentKey) {
7
6
  EnvironmentKey["ScwConfigPath"] = "SCW_CONFIG_PATH";
8
7
  EnvironmentKey["ScwAccessKey"] = "SCW_ACCESS_KEY";
package/dist/index.cjs CHANGED
@@ -1,14 +1,11 @@
1
1
  'use strict';
2
2
 
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
3
  var process = require('process');
6
4
  var os = require('os');
7
5
  var path = require('path');
8
6
  var fs = require('fs');
9
7
 
10
- function _interopNamespace(e) {
11
- if (e && e.__esModule) return e;
8
+ function _interopNamespaceDefault(e) {
12
9
  var n = Object.create(null);
13
10
  if (e) {
14
11
  Object.keys(e).forEach(function (k) {
@@ -21,17 +18,16 @@ function _interopNamespace(e) {
21
18
  }
22
19
  });
23
20
  }
24
- n["default"] = e;
21
+ n.default = e;
25
22
  return Object.freeze(n);
26
23
  }
27
24
 
28
- var path__namespace = /*#__PURE__*/_interopNamespace(path);
25
+ var path__namespace = /*#__PURE__*/_interopNamespaceDefault(path);
29
26
 
30
27
  /**
31
28
  * Environment Key.
32
29
  */
33
30
  let EnvironmentKey;
34
-
35
31
  (function (EnvironmentKey) {
36
32
  EnvironmentKey["ScwConfigPath"] = "SCW_CONFIG_PATH";
37
33
  EnvironmentKey["ScwAccessKey"] = "SCW_ACCESS_KEY";
@@ -50,12 +46,14 @@ let EnvironmentKey;
50
46
  *
51
47
  * @internal
52
48
  */
53
-
54
49
  const getScwConfigurationDirectory = () => {
55
50
  const xdgConfigPath = process.env.XDG_CONFIG_HOME;
56
- if (typeof xdgConfigPath === 'string' && xdgConfigPath.length > 0) return path__namespace.join(xdgConfigPath, 'scw');
51
+ if (typeof xdgConfigPath === 'string' && xdgConfigPath.length > 0) {
52
+ return path__namespace.join(xdgConfigPath, 'scw');
53
+ }
57
54
  return path__namespace.join(os.homedir(), '.config', 'scw');
58
55
  };
56
+
59
57
  /**
60
58
  * Gets the configuration file path.
61
59
  *
@@ -63,18 +61,21 @@ const getScwConfigurationDirectory = () => {
63
61
  *
64
62
  * @internal
65
63
  */
66
-
67
64
  const resolveConfigurationFilePath = () => {
68
65
  // Try path defined by user in env variables
69
66
  const envFilePath = process.env[EnvironmentKey.ScwConfigPath];
70
- if (typeof envFilePath === 'string' && envFilePath.length > 0) return envFilePath; // or fallback on the default path
67
+ if (typeof envFilePath === 'string' && envFilePath.length > 0) {
68
+ return envFilePath;
69
+ }
71
70
 
71
+ // or fallback on the default path
72
72
  return path__namespace.join(getScwConfigurationDirectory(), 'config.yaml');
73
73
  };
74
74
 
75
75
  const STRIP_COMMENT_REGEX = /(^|\s)[;#]/;
76
76
  const DETECT_SECTION_REGEX = /^\s*([^]+):\s*$/;
77
77
  const DETECT_ITEM_REGEX = /^\s*(.+?)\s*:\s*(.+?)\s*$/;
78
+
78
79
  /**
79
80
  * Converts Yaml to configuration map.
80
81
  *
@@ -83,34 +84,29 @@ const DETECT_ITEM_REGEX = /^\s*(.+?)\s*:\s*(.+?)\s*$/;
83
84
  *
84
85
  * @internal
85
86
  */
86
-
87
87
  const convertYamlToConfiguration = input => {
88
88
  let foundProfilesKey = false;
89
89
  let currentSection = 'default';
90
90
  const map = {};
91
-
92
91
  if (typeof input !== 'string') {
93
92
  return map;
94
93
  }
95
-
96
94
  input.split(/\r?\n/).forEach(rawLine => {
97
95
  // remove comments
98
- const line = rawLine.split(STRIP_COMMENT_REGEX)[0]; // parse sections
99
-
96
+ const line = rawLine.split(STRIP_COMMENT_REGEX)[0];
97
+ // parse sections
100
98
  const newSection = DETECT_SECTION_REGEX.exec(line);
101
-
102
99
  if (newSection) {
103
100
  currentSection = undefined;
104
-
105
101
  if (newSection[1] === 'profiles') {
106
102
  foundProfilesKey = true;
107
103
  } else if (foundProfilesKey === true) {
108
104
  [, currentSection] = newSection;
109
105
  }
110
- } // parse items
106
+ }
107
+ // parse items
111
108
  else if (currentSection) {
112
109
  const item = DETECT_ITEM_REGEX.exec(line);
113
-
114
110
  if (item) {
115
111
  if (typeof map[currentSection] !== 'object') {
116
112
  map[currentSection] = {};
@@ -121,6 +117,7 @@ const convertYamlToConfiguration = input => {
121
117
  });
122
118
  return map;
123
119
  };
120
+
124
121
  /**
125
122
  * Loads configuration from a file.
126
123
  *
@@ -132,7 +129,6 @@ const convertYamlToConfiguration = input => {
132
129
  *
133
130
  * @internal
134
131
  */
135
-
136
132
  const loadConfigurationFromFile = filePath => {
137
133
  // `readFileSync` returns a string when encoding option is specified.
138
134
  // {@link https://nodejs.org/api/fs.html#fs_fs_readfilesync_path_options}
@@ -147,7 +143,6 @@ const loadConfigurationFromFile = filePath => {
147
143
  *
148
144
  * @public
149
145
  */
150
-
151
146
  const loadProfileFromEnvironmentValues = () => ({
152
147
  accessKey: process.env[EnvironmentKey.ScwAccessKey],
153
148
  apiURL: process.env[EnvironmentKey.ScwAPIURL],
@@ -157,6 +152,7 @@ const loadProfileFromEnvironmentValues = () => ({
157
152
  defaultZone: process.env[EnvironmentKey.ScwDefaultZone],
158
153
  secretKey: process.env[EnvironmentKey.ScwSecretKey]
159
154
  });
155
+
160
156
  /**
161
157
  * Loads profile from configuration file.
162
158
  *
@@ -169,22 +165,17 @@ const loadProfileFromEnvironmentValues = () => ({
169
165
  *
170
166
  * @public
171
167
  */
172
-
173
168
  const loadProfileFromConfigurationFile = params => {
174
169
  const filePath = (params == null ? void 0 : params.filepath) ?? resolveConfigurationFilePath();
175
-
176
170
  if (typeof filePath !== 'string' || filePath.length === 0) {
177
171
  throw new Error('Could not find the path to the configuration file.');
178
172
  }
179
-
180
173
  const configs = loadConfigurationFromFile(filePath);
181
174
  const profileName = (params == null ? void 0 : params.profileName) ?? 'default';
182
175
  const profileMap = configs[profileName];
183
-
184
176
  if (typeof profileMap !== 'object') {
185
177
  throw new Error(`Could not find the desired profile '${profileName}' in the configuration file.`);
186
178
  }
187
-
188
179
  return {
189
180
  accessKey: profileMap.access_key,
190
181
  apiURL: profileMap.api_url,
@@ -10,12 +10,14 @@ import { EnvironmentKey } from './env.js';
10
10
  *
11
11
  * @internal
12
12
  */
13
-
14
13
  const getScwConfigurationDirectory = () => {
15
14
  const xdgConfigPath = env.XDG_CONFIG_HOME;
16
- if (typeof xdgConfigPath === 'string' && xdgConfigPath.length > 0) return path.join(xdgConfigPath, 'scw');
15
+ if (typeof xdgConfigPath === 'string' && xdgConfigPath.length > 0) {
16
+ return path.join(xdgConfigPath, 'scw');
17
+ }
17
18
  return path.join(homedir(), '.config', 'scw');
18
19
  };
20
+
19
21
  /**
20
22
  * Gets the configuration file path.
21
23
  *
@@ -23,12 +25,14 @@ const getScwConfigurationDirectory = () => {
23
25
  *
24
26
  * @internal
25
27
  */
26
-
27
28
  const resolveConfigurationFilePath = () => {
28
29
  // Try path defined by user in env variables
29
30
  const envFilePath = env[EnvironmentKey.ScwConfigPath];
30
- if (typeof envFilePath === 'string' && envFilePath.length > 0) return envFilePath; // or fallback on the default path
31
+ if (typeof envFilePath === 'string' && envFilePath.length > 0) {
32
+ return envFilePath;
33
+ }
31
34
 
35
+ // or fallback on the default path
32
36
  return path.join(getScwConfigurationDirectory(), 'config.yaml');
33
37
  };
34
38
 
@@ -3,6 +3,7 @@ import { readFileSync } from 'fs';
3
3
  const STRIP_COMMENT_REGEX = /(^|\s)[;#]/;
4
4
  const DETECT_SECTION_REGEX = /^\s*([^]+):\s*$/;
5
5
  const DETECT_ITEM_REGEX = /^\s*(.+?)\s*:\s*(.+?)\s*$/;
6
+
6
7
  /**
7
8
  * Converts Yaml to configuration map.
8
9
  *
@@ -11,34 +12,29 @@ const DETECT_ITEM_REGEX = /^\s*(.+?)\s*:\s*(.+?)\s*$/;
11
12
  *
12
13
  * @internal
13
14
  */
14
-
15
15
  const convertYamlToConfiguration = input => {
16
16
  let foundProfilesKey = false;
17
17
  let currentSection = 'default';
18
18
  const map = {};
19
-
20
19
  if (typeof input !== 'string') {
21
20
  return map;
22
21
  }
23
-
24
22
  input.split(/\r?\n/).forEach(rawLine => {
25
23
  // remove comments
26
- const line = rawLine.split(STRIP_COMMENT_REGEX)[0]; // parse sections
27
-
24
+ const line = rawLine.split(STRIP_COMMENT_REGEX)[0];
25
+ // parse sections
28
26
  const newSection = DETECT_SECTION_REGEX.exec(line);
29
-
30
27
  if (newSection) {
31
28
  currentSection = undefined;
32
-
33
29
  if (newSection[1] === 'profiles') {
34
30
  foundProfilesKey = true;
35
31
  } else if (foundProfilesKey === true) {
36
32
  [, currentSection] = newSection;
37
33
  }
38
- } // parse items
34
+ }
35
+ // parse items
39
36
  else if (currentSection) {
40
37
  const item = DETECT_ITEM_REGEX.exec(line);
41
-
42
38
  if (item) {
43
39
  if (typeof map[currentSection] !== 'object') {
44
40
  map[currentSection] = {};
@@ -49,6 +45,7 @@ const convertYamlToConfiguration = input => {
49
45
  });
50
46
  return map;
51
47
  };
48
+
52
49
  /**
53
50
  * Loads configuration from a file.
54
51
  *
@@ -60,7 +57,6 @@ const convertYamlToConfiguration = input => {
60
57
  *
61
58
  * @internal
62
59
  */
63
-
64
60
  const loadConfigurationFromFile = filePath => {
65
61
  // `readFileSync` returns a string when encoding option is specified.
66
62
  // {@link https://nodejs.org/api/fs.html#fs_fs_readfilesync_path_options}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@scaleway/configuration-loader",
3
- "version": "0.1.0-beta.3",
3
+ "version": "0.1.0-beta.4",
4
4
  "license": "Apache-2.0",
5
5
  "description": "Load configuration via file or environment for NodeJS.",
6
6
  "main": "dist/index.cjs",
@@ -21,5 +21,5 @@
21
21
  "devDependencies": {
22
22
  "@types/node": "^17.0.45"
23
23
  },
24
- "gitHead": "d0dccb15478b4b9580c5696920fc210b3ab18e98"
24
+ "gitHead": "5484de96723e18038b6a8781ebb941a1f270f49a"
25
25
  }