@scaleway/configuration-loader 1.0.3 → 1.0.5
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 +10 -0
- package/dist/config-loader.cjs +52 -0
- package/dist/config-loader.d.ts +34 -0
- package/dist/config-loader.js +26 -52
- package/dist/env.cjs +14 -0
- package/dist/env.d.ts +24 -0
- package/dist/env.js +14 -16
- package/dist/index.cjs +6 -214
- package/dist/index.d.ts +2 -102
- package/dist/index.js +6 -1
- package/dist/path-resolver.cjs +39 -0
- package/dist/path-resolver.d.ts +16 -0
- package/dist/path-resolver.js +13 -30
- package/dist/types.d.ts +67 -0
- package/dist/yml-loader.cjs +41 -0
- package/dist/yml-loader.d.ts +22 -0
- package/dist/yml-loader.js +16 -42
- package/package.json +8 -2
- package/tsconfig.build.json +18 -0
- package/tsconfig.json +4 -0
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,16 @@
|
|
|
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
|
+
## [1.0.5](https://github.com/scaleway/scaleway-sdk-js/compare/@scaleway/configuration-loader@1.0.4...@scaleway/configuration-loader@1.0.5) (2024-05-15)
|
|
7
|
+
|
|
8
|
+
### Bug Fixes
|
|
9
|
+
|
|
10
|
+
- **build:** export all types ([#1261](https://github.com/scaleway/scaleway-sdk-js/issues/1261)) ([2c13c19](https://github.com/scaleway/scaleway-sdk-js/commit/2c13c19d1699d2138d0ae9c4cf2a8a29b50d93ff))
|
|
11
|
+
|
|
12
|
+
## 1.0.4 (2024-05-06)
|
|
13
|
+
|
|
14
|
+
**Note:** Version bump only for package @scaleway/configuration-loader
|
|
15
|
+
|
|
6
16
|
## [1.0.3](https://github.com/scaleway/scaleway-sdk-js/compare/@scaleway/configuration-loader@1.0.2...@scaleway/configuration-loader@1.0.3) (2024-02-19)
|
|
7
17
|
|
|
8
18
|
**Note:** Version bump only for package @scaleway/configuration-loader
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const process = require("process");
|
|
4
|
+
const env = require("./env.cjs");
|
|
5
|
+
const pathResolver = require("./path-resolver.cjs");
|
|
6
|
+
const ymlLoader = require("./yml-loader.cjs");
|
|
7
|
+
const convertFileConfigToSDK = (obj) => ({
|
|
8
|
+
accessKey: obj.access_key,
|
|
9
|
+
apiURL: obj.api_url,
|
|
10
|
+
defaultOrganizationId: obj.default_organization_id,
|
|
11
|
+
defaultProjectId: obj.default_project_id,
|
|
12
|
+
defaultRegion: obj.default_region,
|
|
13
|
+
defaultZone: obj.default_zone,
|
|
14
|
+
secretKey: obj.secret_key
|
|
15
|
+
});
|
|
16
|
+
const loadProfileFromEnvironmentValues = () => ({
|
|
17
|
+
accessKey: process.env[env.EnvironmentKey.ScwAccessKey],
|
|
18
|
+
apiURL: process.env[env.EnvironmentKey.ScwAPIURL],
|
|
19
|
+
defaultOrganizationId: process.env[env.EnvironmentKey.ScwDefaultOrganizationId],
|
|
20
|
+
defaultProjectId: process.env[env.EnvironmentKey.ScwDefaultProjectId],
|
|
21
|
+
defaultRegion: process.env[env.EnvironmentKey.ScwDefaultRegion],
|
|
22
|
+
defaultZone: process.env[env.EnvironmentKey.ScwDefaultZone],
|
|
23
|
+
secretKey: process.env[env.EnvironmentKey.ScwSecretKey]
|
|
24
|
+
});
|
|
25
|
+
const loadAllProfilesFromConfigurationFile = (params) => {
|
|
26
|
+
const filePath = params?.filepath ?? pathResolver.resolveConfigurationFilePath();
|
|
27
|
+
if (typeof filePath !== "string" || filePath.length === 0) {
|
|
28
|
+
throw new Error("Could not find the path to the configuration file.");
|
|
29
|
+
}
|
|
30
|
+
const configs = ymlLoader.loadConfigurationFromFile(filePath);
|
|
31
|
+
return Object.keys(configs).reduce(
|
|
32
|
+
(prev, pKey) => ({
|
|
33
|
+
...prev,
|
|
34
|
+
[pKey]: convertFileConfigToSDK(configs[pKey])
|
|
35
|
+
}),
|
|
36
|
+
{}
|
|
37
|
+
);
|
|
38
|
+
};
|
|
39
|
+
const loadProfileFromConfigurationFile = (params) => {
|
|
40
|
+
const configs = loadAllProfilesFromConfigurationFile(params);
|
|
41
|
+
const profileName = params?.profileName ?? "default";
|
|
42
|
+
const profileMap = configs[profileName];
|
|
43
|
+
if (typeof profileMap !== "object") {
|
|
44
|
+
throw new Error(
|
|
45
|
+
`Could not find the desired profile '${profileName}' in the configuration file.`
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
return profileMap;
|
|
49
|
+
};
|
|
50
|
+
exports.loadAllProfilesFromConfigurationFile = loadAllProfilesFromConfigurationFile;
|
|
51
|
+
exports.loadProfileFromConfigurationFile = loadProfileFromConfigurationFile;
|
|
52
|
+
exports.loadProfileFromEnvironmentValues = loadProfileFromEnvironmentValues;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { AllProfilesFromFileParams, Profile, ProfileFromFileParams } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Loads profile from environment values.
|
|
4
|
+
*
|
|
5
|
+
* @returns The profile filled with values found in the environment
|
|
6
|
+
*
|
|
7
|
+
* @public
|
|
8
|
+
*/
|
|
9
|
+
export declare const loadProfileFromEnvironmentValues: () => Profile;
|
|
10
|
+
/**
|
|
11
|
+
* Loads all the profiles from configuration file.
|
|
12
|
+
*
|
|
13
|
+
* @param params - The parameters to load the profile
|
|
14
|
+
* @returns The profiles filled with values found in the configuration profile
|
|
15
|
+
*
|
|
16
|
+
* @throws Error
|
|
17
|
+
* Thrown if the configuration file couldn't be found.
|
|
18
|
+
*
|
|
19
|
+
* @public
|
|
20
|
+
*/
|
|
21
|
+
export declare const loadAllProfilesFromConfigurationFile: (params?: Readonly<AllProfilesFromFileParams>) => Record<string, Profile>;
|
|
22
|
+
/**
|
|
23
|
+
* Loads profile from configuration file.
|
|
24
|
+
*
|
|
25
|
+
* @param params - The parameters to load the profile
|
|
26
|
+
* @returns The profile filled with values found in the configuration profile
|
|
27
|
+
*
|
|
28
|
+
* @throws Error
|
|
29
|
+
* Thrown if the configuration file couldn't be found,
|
|
30
|
+
* or if the specified profile can't be found.
|
|
31
|
+
*
|
|
32
|
+
* @public
|
|
33
|
+
*/
|
|
34
|
+
export declare const loadProfileFromConfigurationFile: (params?: Readonly<ProfileFromFileParams>) => Profile;
|
package/dist/config-loader.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import { env } from
|
|
2
|
-
import { EnvironmentKey } from
|
|
3
|
-
import { resolveConfigurationFilePath } from
|
|
4
|
-
import { loadConfigurationFromFile } from
|
|
5
|
-
|
|
6
|
-
const convertFileConfigToSDK = obj => ({
|
|
1
|
+
import { env } from "process";
|
|
2
|
+
import { EnvironmentKey } from "./env.js";
|
|
3
|
+
import { resolveConfigurationFilePath } from "./path-resolver.js";
|
|
4
|
+
import { loadConfigurationFromFile } from "./yml-loader.js";
|
|
5
|
+
const convertFileConfigToSDK = (obj) => ({
|
|
7
6
|
accessKey: obj.access_key,
|
|
8
7
|
apiURL: obj.api_url,
|
|
9
8
|
defaultOrganizationId: obj.default_organization_id,
|
|
@@ -12,14 +11,6 @@ const convertFileConfigToSDK = obj => ({
|
|
|
12
11
|
defaultZone: obj.default_zone,
|
|
13
12
|
secretKey: obj.secret_key
|
|
14
13
|
});
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Loads profile from environment values.
|
|
18
|
-
*
|
|
19
|
-
* @returns The profile filled with values found in the environment
|
|
20
|
-
*
|
|
21
|
-
* @public
|
|
22
|
-
*/
|
|
23
14
|
const loadProfileFromEnvironmentValues = () => ({
|
|
24
15
|
accessKey: env[EnvironmentKey.ScwAccessKey],
|
|
25
16
|
apiURL: env[EnvironmentKey.ScwAPIURL],
|
|
@@ -29,50 +20,33 @@ const loadProfileFromEnvironmentValues = () => ({
|
|
|
29
20
|
defaultZone: env[EnvironmentKey.ScwDefaultZone],
|
|
30
21
|
secretKey: env[EnvironmentKey.ScwSecretKey]
|
|
31
22
|
});
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Loads all the profiles from configuration file.
|
|
35
|
-
*
|
|
36
|
-
* @param params - The parameters to load the profile
|
|
37
|
-
* @returns The profiles filled with values found in the configuration profile
|
|
38
|
-
*
|
|
39
|
-
* @throws Error
|
|
40
|
-
* Thrown if the configuration file couldn't be found.
|
|
41
|
-
*
|
|
42
|
-
* @public
|
|
43
|
-
*/
|
|
44
|
-
const loadAllProfilesFromConfigurationFile = params => {
|
|
23
|
+
const loadAllProfilesFromConfigurationFile = (params) => {
|
|
45
24
|
const filePath = params?.filepath ?? resolveConfigurationFilePath();
|
|
46
|
-
if (typeof filePath !==
|
|
47
|
-
throw new Error(
|
|
25
|
+
if (typeof filePath !== "string" || filePath.length === 0) {
|
|
26
|
+
throw new Error("Could not find the path to the configuration file.");
|
|
48
27
|
}
|
|
49
28
|
const configs = loadConfigurationFromFile(filePath);
|
|
50
|
-
return Object.keys(configs).reduce(
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
29
|
+
return Object.keys(configs).reduce(
|
|
30
|
+
(prev, pKey) => ({
|
|
31
|
+
...prev,
|
|
32
|
+
[pKey]: convertFileConfigToSDK(configs[pKey])
|
|
33
|
+
}),
|
|
34
|
+
{}
|
|
35
|
+
);
|
|
54
36
|
};
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Loads profile from configuration file.
|
|
58
|
-
*
|
|
59
|
-
* @param params - The parameters to load the profile
|
|
60
|
-
* @returns The profile filled with values found in the configuration profile
|
|
61
|
-
*
|
|
62
|
-
* @throws Error
|
|
63
|
-
* Thrown if the configuration file couldn't be found,
|
|
64
|
-
* or if the specified profile can't be found.
|
|
65
|
-
*
|
|
66
|
-
* @public
|
|
67
|
-
*/
|
|
68
|
-
const loadProfileFromConfigurationFile = params => {
|
|
37
|
+
const loadProfileFromConfigurationFile = (params) => {
|
|
69
38
|
const configs = loadAllProfilesFromConfigurationFile(params);
|
|
70
|
-
const profileName = params?.profileName ??
|
|
39
|
+
const profileName = params?.profileName ?? "default";
|
|
71
40
|
const profileMap = configs[profileName];
|
|
72
|
-
if (typeof profileMap !==
|
|
73
|
-
throw new Error(
|
|
41
|
+
if (typeof profileMap !== "object") {
|
|
42
|
+
throw new Error(
|
|
43
|
+
`Could not find the desired profile '${profileName}' in the configuration file.`
|
|
44
|
+
);
|
|
74
45
|
}
|
|
75
46
|
return profileMap;
|
|
76
47
|
};
|
|
77
|
-
|
|
78
|
-
|
|
48
|
+
export {
|
|
49
|
+
loadAllProfilesFromConfigurationFile,
|
|
50
|
+
loadProfileFromConfigurationFile,
|
|
51
|
+
loadProfileFromEnvironmentValues
|
|
52
|
+
};
|
package/dist/env.cjs
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
var EnvironmentKey = /* @__PURE__ */ ((EnvironmentKey2) => {
|
|
4
|
+
EnvironmentKey2["ScwConfigPath"] = "SCW_CONFIG_PATH";
|
|
5
|
+
EnvironmentKey2["ScwAccessKey"] = "SCW_ACCESS_KEY";
|
|
6
|
+
EnvironmentKey2["ScwSecretKey"] = "SCW_SECRET_KEY";
|
|
7
|
+
EnvironmentKey2["ScwAPIURL"] = "SCW_API_URL";
|
|
8
|
+
EnvironmentKey2["ScwDefaultOrganizationId"] = "SCW_DEFAULT_ORGANIZATION_ID";
|
|
9
|
+
EnvironmentKey2["ScwDefaultProjectId"] = "SCW_DEFAULT_PROJECT_ID";
|
|
10
|
+
EnvironmentKey2["ScwDefaultRegion"] = "SCW_DEFAULT_REGION";
|
|
11
|
+
EnvironmentKey2["ScwDefaultZone"] = "SCW_DEFAULT_ZONE";
|
|
12
|
+
return EnvironmentKey2;
|
|
13
|
+
})(EnvironmentKey || {});
|
|
14
|
+
exports.EnvironmentKey = EnvironmentKey;
|
package/dist/env.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Environment Key.
|
|
3
|
+
*/
|
|
4
|
+
export declare enum EnvironmentKey {
|
|
5
|
+
/** Path to the Scaleway configuration file */
|
|
6
|
+
ScwConfigPath = "SCW_CONFIG_PATH",
|
|
7
|
+
/** Scaleway access key */
|
|
8
|
+
ScwAccessKey = "SCW_ACCESS_KEY",
|
|
9
|
+
/**
|
|
10
|
+
* Scaleway secret key
|
|
11
|
+
* @remarks #nosec G101
|
|
12
|
+
*/
|
|
13
|
+
ScwSecretKey = "SCW_SECRET_KEY",
|
|
14
|
+
/** Scaleway API URL */
|
|
15
|
+
ScwAPIURL = "SCW_API_URL",
|
|
16
|
+
/** Scaleway default organization ID */
|
|
17
|
+
ScwDefaultOrganizationId = "SCW_DEFAULT_ORGANIZATION_ID",
|
|
18
|
+
/** Scaleway default project ID */
|
|
19
|
+
ScwDefaultProjectId = "SCW_DEFAULT_PROJECT_ID",
|
|
20
|
+
/** Scaleway default region */
|
|
21
|
+
ScwDefaultRegion = "SCW_DEFAULT_REGION",
|
|
22
|
+
/** Scaleway default zone */
|
|
23
|
+
ScwDefaultZone = "SCW_DEFAULT_ZONE"
|
|
24
|
+
}
|
package/dist/env.js
CHANGED
|
@@ -1,16 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export { EnvironmentKey };
|
|
1
|
+
var EnvironmentKey = /* @__PURE__ */ ((EnvironmentKey2) => {
|
|
2
|
+
EnvironmentKey2["ScwConfigPath"] = "SCW_CONFIG_PATH";
|
|
3
|
+
EnvironmentKey2["ScwAccessKey"] = "SCW_ACCESS_KEY";
|
|
4
|
+
EnvironmentKey2["ScwSecretKey"] = "SCW_SECRET_KEY";
|
|
5
|
+
EnvironmentKey2["ScwAPIURL"] = "SCW_API_URL";
|
|
6
|
+
EnvironmentKey2["ScwDefaultOrganizationId"] = "SCW_DEFAULT_ORGANIZATION_ID";
|
|
7
|
+
EnvironmentKey2["ScwDefaultProjectId"] = "SCW_DEFAULT_PROJECT_ID";
|
|
8
|
+
EnvironmentKey2["ScwDefaultRegion"] = "SCW_DEFAULT_REGION";
|
|
9
|
+
EnvironmentKey2["ScwDefaultZone"] = "SCW_DEFAULT_ZONE";
|
|
10
|
+
return EnvironmentKey2;
|
|
11
|
+
})(EnvironmentKey || {});
|
|
12
|
+
export {
|
|
13
|
+
EnvironmentKey
|
|
14
|
+
};
|
package/dist/index.cjs
CHANGED
|
@@ -1,214 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
var fs = require('fs');
|
|
8
|
-
|
|
9
|
-
function _interopNamespaceDefault(e) {
|
|
10
|
-
var n = Object.create(null);
|
|
11
|
-
if (e) {
|
|
12
|
-
Object.keys(e).forEach(function (k) {
|
|
13
|
-
if (k !== 'default') {
|
|
14
|
-
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
15
|
-
Object.defineProperty(n, k, d.get ? d : {
|
|
16
|
-
enumerable: true,
|
|
17
|
-
get: function () { return e[k]; }
|
|
18
|
-
});
|
|
19
|
-
}
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
n.default = e;
|
|
23
|
-
return Object.freeze(n);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
var path__namespace = /*#__PURE__*/_interopNamespaceDefault(path);
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Environment Key.
|
|
30
|
-
*/
|
|
31
|
-
let EnvironmentKey = /*#__PURE__*/function (EnvironmentKey) {
|
|
32
|
-
EnvironmentKey["ScwConfigPath"] = "SCW_CONFIG_PATH";
|
|
33
|
-
EnvironmentKey["ScwAccessKey"] = "SCW_ACCESS_KEY";
|
|
34
|
-
EnvironmentKey["ScwSecretKey"] = "SCW_SECRET_KEY";
|
|
35
|
-
EnvironmentKey["ScwAPIURL"] = "SCW_API_URL";
|
|
36
|
-
EnvironmentKey["ScwDefaultOrganizationId"] = "SCW_DEFAULT_ORGANIZATION_ID";
|
|
37
|
-
EnvironmentKey["ScwDefaultProjectId"] = "SCW_DEFAULT_PROJECT_ID";
|
|
38
|
-
EnvironmentKey["ScwDefaultRegion"] = "SCW_DEFAULT_REGION";
|
|
39
|
-
EnvironmentKey["ScwDefaultZone"] = "SCW_DEFAULT_ZONE";
|
|
40
|
-
return EnvironmentKey;
|
|
41
|
-
}({});
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Gets the Scaleway directory.
|
|
45
|
-
*
|
|
46
|
-
* @returns The path to the Scaleway diretory
|
|
47
|
-
*
|
|
48
|
-
* @internal
|
|
49
|
-
*/
|
|
50
|
-
const getScwConfigurationDirectory = () => {
|
|
51
|
-
const xdgConfigPath = node_process.env.XDG_CONFIG_HOME;
|
|
52
|
-
if (typeof xdgConfigPath === 'string' && xdgConfigPath.length > 0) {
|
|
53
|
-
return path__namespace.join(xdgConfigPath, 'scw');
|
|
54
|
-
}
|
|
55
|
-
return path__namespace.join(node_os.homedir(), '.config', 'scw');
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Gets the configuration file path.
|
|
60
|
-
*
|
|
61
|
-
* @returns The path to the configuration file
|
|
62
|
-
*
|
|
63
|
-
* @internal
|
|
64
|
-
*/
|
|
65
|
-
const resolveConfigurationFilePath = () => {
|
|
66
|
-
// Try path defined by user in env variables
|
|
67
|
-
const envFilePath = node_process.env[EnvironmentKey.ScwConfigPath];
|
|
68
|
-
if (typeof envFilePath === 'string' && envFilePath.length > 0) {
|
|
69
|
-
return envFilePath;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
// or fallback on the default path
|
|
73
|
-
return path__namespace.join(getScwConfigurationDirectory(), 'config.yaml');
|
|
74
|
-
};
|
|
75
|
-
|
|
76
|
-
const STRIP_COMMENT_REGEX = /(^|\s)[;#]/;
|
|
77
|
-
const DETECT_SECTION_REGEX = /^\s*([^]+):\s*$/;
|
|
78
|
-
const DETECT_ITEM_REGEX = /^\s*(.+?)\s*:\s*(.+?)\s*$/;
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Converts YAML to configuration map.
|
|
82
|
-
*
|
|
83
|
-
* @param input - YAML string
|
|
84
|
-
* @returns The configuration map
|
|
85
|
-
*
|
|
86
|
-
* @internal
|
|
87
|
-
*/
|
|
88
|
-
const convertYamlToConfiguration = input => {
|
|
89
|
-
let foundProfilesKey = false;
|
|
90
|
-
let currentSection = 'default';
|
|
91
|
-
const map = {};
|
|
92
|
-
if (typeof input !== 'string') {
|
|
93
|
-
return map;
|
|
94
|
-
}
|
|
95
|
-
input.split(/\r?\n/).forEach(rawLine => {
|
|
96
|
-
// remove comments
|
|
97
|
-
const line = rawLine.split(STRIP_COMMENT_REGEX)[0];
|
|
98
|
-
// parse sections
|
|
99
|
-
const newSection = DETECT_SECTION_REGEX.exec(line);
|
|
100
|
-
if (newSection) {
|
|
101
|
-
currentSection = undefined;
|
|
102
|
-
if (newSection[1] === 'profiles') {
|
|
103
|
-
foundProfilesKey = true;
|
|
104
|
-
} else if (foundProfilesKey) {
|
|
105
|
-
[, currentSection] = newSection;
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
// parse items
|
|
109
|
-
else if (currentSection) {
|
|
110
|
-
const item = DETECT_ITEM_REGEX.exec(line);
|
|
111
|
-
if (item) {
|
|
112
|
-
if (typeof map[currentSection] !== 'object') {
|
|
113
|
-
map[currentSection] = {};
|
|
114
|
-
}
|
|
115
|
-
[,, map[currentSection][item[1]]] = item;
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
});
|
|
119
|
-
return map;
|
|
120
|
-
};
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* Loads configuration from a file.
|
|
124
|
-
*
|
|
125
|
-
* @param filePath - Path to the configuration file
|
|
126
|
-
* @returns The configuration
|
|
127
|
-
*
|
|
128
|
-
* @throws Error
|
|
129
|
-
* Thrown if the file doesn't exist.
|
|
130
|
-
*
|
|
131
|
-
* @internal
|
|
132
|
-
*/
|
|
133
|
-
const loadConfigurationFromFile = filePath => {
|
|
134
|
-
// `readFileSync` returns a string when encoding option is specified.
|
|
135
|
-
// {@link https://nodejs.org/api/fs.html#fs_fs_readfilesync_path_options}
|
|
136
|
-
const fileContent = fs.readFileSync(filePath, 'utf-8');
|
|
137
|
-
return convertYamlToConfiguration(fileContent);
|
|
138
|
-
};
|
|
139
|
-
|
|
140
|
-
const convertFileConfigToSDK = obj => ({
|
|
141
|
-
accessKey: obj.access_key,
|
|
142
|
-
apiURL: obj.api_url,
|
|
143
|
-
defaultOrganizationId: obj.default_organization_id,
|
|
144
|
-
defaultProjectId: obj.default_project_id,
|
|
145
|
-
defaultRegion: obj.default_region,
|
|
146
|
-
defaultZone: obj.default_zone,
|
|
147
|
-
secretKey: obj.secret_key
|
|
148
|
-
});
|
|
149
|
-
|
|
150
|
-
/**
|
|
151
|
-
* Loads profile from environment values.
|
|
152
|
-
*
|
|
153
|
-
* @returns The profile filled with values found in the environment
|
|
154
|
-
*
|
|
155
|
-
* @public
|
|
156
|
-
*/
|
|
157
|
-
const loadProfileFromEnvironmentValues = () => ({
|
|
158
|
-
accessKey: process.env[EnvironmentKey.ScwAccessKey],
|
|
159
|
-
apiURL: process.env[EnvironmentKey.ScwAPIURL],
|
|
160
|
-
defaultOrganizationId: process.env[EnvironmentKey.ScwDefaultOrganizationId],
|
|
161
|
-
defaultProjectId: process.env[EnvironmentKey.ScwDefaultProjectId],
|
|
162
|
-
defaultRegion: process.env[EnvironmentKey.ScwDefaultRegion],
|
|
163
|
-
defaultZone: process.env[EnvironmentKey.ScwDefaultZone],
|
|
164
|
-
secretKey: process.env[EnvironmentKey.ScwSecretKey]
|
|
165
|
-
});
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* Loads all the profiles from configuration file.
|
|
169
|
-
*
|
|
170
|
-
* @param params - The parameters to load the profile
|
|
171
|
-
* @returns The profiles filled with values found in the configuration profile
|
|
172
|
-
*
|
|
173
|
-
* @throws Error
|
|
174
|
-
* Thrown if the configuration file couldn't be found.
|
|
175
|
-
*
|
|
176
|
-
* @public
|
|
177
|
-
*/
|
|
178
|
-
const loadAllProfilesFromConfigurationFile = params => {
|
|
179
|
-
const filePath = params?.filepath ?? resolveConfigurationFilePath();
|
|
180
|
-
if (typeof filePath !== 'string' || filePath.length === 0) {
|
|
181
|
-
throw new Error('Could not find the path to the configuration file.');
|
|
182
|
-
}
|
|
183
|
-
const configs = loadConfigurationFromFile(filePath);
|
|
184
|
-
return Object.keys(configs).reduce((prev, pKey) => ({
|
|
185
|
-
...prev,
|
|
186
|
-
[pKey]: convertFileConfigToSDK(configs[pKey])
|
|
187
|
-
}), {});
|
|
188
|
-
};
|
|
189
|
-
|
|
190
|
-
/**
|
|
191
|
-
* Loads profile from configuration file.
|
|
192
|
-
*
|
|
193
|
-
* @param params - The parameters to load the profile
|
|
194
|
-
* @returns The profile filled with values found in the configuration profile
|
|
195
|
-
*
|
|
196
|
-
* @throws Error
|
|
197
|
-
* Thrown if the configuration file couldn't be found,
|
|
198
|
-
* or if the specified profile can't be found.
|
|
199
|
-
*
|
|
200
|
-
* @public
|
|
201
|
-
*/
|
|
202
|
-
const loadProfileFromConfigurationFile = params => {
|
|
203
|
-
const configs = loadAllProfilesFromConfigurationFile(params);
|
|
204
|
-
const profileName = params?.profileName ?? 'default';
|
|
205
|
-
const profileMap = configs[profileName];
|
|
206
|
-
if (typeof profileMap !== 'object') {
|
|
207
|
-
throw new Error(`Could not find the desired profile '${profileName}' in the configuration file.`);
|
|
208
|
-
}
|
|
209
|
-
return profileMap;
|
|
210
|
-
};
|
|
211
|
-
|
|
212
|
-
exports.loadAllProfilesFromConfigurationFile = loadAllProfilesFromConfigurationFile;
|
|
213
|
-
exports.loadProfileFromConfigurationFile = loadProfileFromConfigurationFile;
|
|
214
|
-
exports.loadProfileFromEnvironmentValues = loadProfileFromEnvironmentValues;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const configLoader = require("./config-loader.cjs");
|
|
4
|
+
exports.loadAllProfilesFromConfigurationFile = configLoader.loadAllProfilesFromConfigurationFile;
|
|
5
|
+
exports.loadProfileFromConfigurationFile = configLoader.loadProfileFromConfigurationFile;
|
|
6
|
+
exports.loadProfileFromEnvironmentValues = configLoader.loadProfileFromEnvironmentValues;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,102 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
*
|
|
4
|
-
* @public
|
|
5
|
-
*/
|
|
6
|
-
type Profile = {
|
|
7
|
-
/**
|
|
8
|
-
* You need an access key and a secret key to connect to Scaleway API.
|
|
9
|
-
* Generate your token at the following address: {@link https://console.scaleway.com/project/credentials}
|
|
10
|
-
*/
|
|
11
|
-
accessKey?: string
|
|
12
|
-
/**
|
|
13
|
-
* APIURL overrides the API URL of the Scaleway API to the given URL.
|
|
14
|
-
* Change that if you want to direct requests to a different endpoint.
|
|
15
|
-
*
|
|
16
|
-
* @defaultValue `https://api.scaleway.com`
|
|
17
|
-
*/
|
|
18
|
-
apiURL?: string
|
|
19
|
-
/**
|
|
20
|
-
* Your organization ID is the identifier of your account inside Scaleway infrastructure.
|
|
21
|
-
*/
|
|
22
|
-
defaultOrganizationId?: string
|
|
23
|
-
/**
|
|
24
|
-
* Your project ID is the identifier of the project your resources are attached to.
|
|
25
|
-
*/
|
|
26
|
-
defaultProjectId?: string
|
|
27
|
-
/**
|
|
28
|
-
* A region is represented as a geographical area such as France (Paris) or the Netherlands (Amsterdam).
|
|
29
|
-
* It can contain multiple availability zones.
|
|
30
|
-
*
|
|
31
|
-
* Examples: fr-par, nl-ams.
|
|
32
|
-
*/
|
|
33
|
-
defaultRegion?: string
|
|
34
|
-
/**
|
|
35
|
-
* A region can be split into many availability zones (AZ).
|
|
36
|
-
* Latency between multiple AZ of the same region are low as they have a common network layer.
|
|
37
|
-
*
|
|
38
|
-
* Examples: fr-par-1, nl-ams-1
|
|
39
|
-
*/
|
|
40
|
-
defaultZone?: string
|
|
41
|
-
/**
|
|
42
|
-
* The secret key is the value that can be used to authenticate against the API (the value used in X-Auth-Token HTTP-header).
|
|
43
|
-
* The secret key MUST remain secret and not given to anyone or published online.
|
|
44
|
-
*/
|
|
45
|
-
secretKey?: string
|
|
46
|
-
}
|
|
47
|
-
/** Parameters to load the all the profiles from the configuration file */
|
|
48
|
-
type AllProfilesFromFileParams = {
|
|
49
|
-
/**
|
|
50
|
-
* The path at which to locate the configuration file.
|
|
51
|
-
*
|
|
52
|
-
* Defaults to the value of the `SCW_CONFIG_PATH` environment variable
|
|
53
|
-
* or `~/.scw/config` otherwise.
|
|
54
|
-
*/
|
|
55
|
-
filepath?: string
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/** Parameters to load the profile from the configuration file */
|
|
59
|
-
type ProfileFromFileParams = AllProfilesFromFileParams & {
|
|
60
|
-
/**
|
|
61
|
-
* Name of the profile to load.
|
|
62
|
-
*
|
|
63
|
-
* @defaultValue `default`
|
|
64
|
-
* */
|
|
65
|
-
profileName?: string
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Loads profile from environment values.
|
|
70
|
-
*
|
|
71
|
-
* @returns The profile filled with values found in the environment
|
|
72
|
-
*
|
|
73
|
-
* @public
|
|
74
|
-
*/
|
|
75
|
-
declare const loadProfileFromEnvironmentValues: () => Profile;
|
|
76
|
-
/**
|
|
77
|
-
* Loads all the profiles from configuration file.
|
|
78
|
-
*
|
|
79
|
-
* @param params - The parameters to load the profile
|
|
80
|
-
* @returns The profiles filled with values found in the configuration profile
|
|
81
|
-
*
|
|
82
|
-
* @throws Error
|
|
83
|
-
* Thrown if the configuration file couldn't be found.
|
|
84
|
-
*
|
|
85
|
-
* @public
|
|
86
|
-
*/
|
|
87
|
-
declare const loadAllProfilesFromConfigurationFile: (params?: Readonly<AllProfilesFromFileParams>) => Record<string, Profile>;
|
|
88
|
-
/**
|
|
89
|
-
* Loads profile from configuration file.
|
|
90
|
-
*
|
|
91
|
-
* @param params - The parameters to load the profile
|
|
92
|
-
* @returns The profile filled with values found in the configuration profile
|
|
93
|
-
*
|
|
94
|
-
* @throws Error
|
|
95
|
-
* Thrown if the configuration file couldn't be found,
|
|
96
|
-
* or if the specified profile can't be found.
|
|
97
|
-
*
|
|
98
|
-
* @public
|
|
99
|
-
*/
|
|
100
|
-
declare const loadProfileFromConfigurationFile: (params?: Readonly<ProfileFromFileParams>) => Profile;
|
|
101
|
-
|
|
102
|
-
export { type AllProfilesFromFileParams, type Profile, type ProfileFromFileParams, loadAllProfilesFromConfigurationFile, loadProfileFromConfigurationFile, loadProfileFromEnvironmentValues };
|
|
1
|
+
export { loadAllProfilesFromConfigurationFile, loadProfileFromConfigurationFile, loadProfileFromEnvironmentValues, } from './config-loader';
|
|
2
|
+
export type { AllProfilesFromFileParams, Profile, ProfileFromFileParams, } from './types';
|
package/dist/index.js
CHANGED
|
@@ -1 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
import { loadAllProfilesFromConfigurationFile, loadProfileFromConfigurationFile, loadProfileFromEnvironmentValues } from "./config-loader.js";
|
|
2
|
+
export {
|
|
3
|
+
loadAllProfilesFromConfigurationFile,
|
|
4
|
+
loadProfileFromConfigurationFile,
|
|
5
|
+
loadProfileFromEnvironmentValues
|
|
6
|
+
};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const node_os = require("node:os");
|
|
4
|
+
const path = require("node:path");
|
|
5
|
+
const node_process = require("node:process");
|
|
6
|
+
const env = require("./env.cjs");
|
|
7
|
+
function _interopNamespaceDefault(e) {
|
|
8
|
+
const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
|
|
9
|
+
if (e) {
|
|
10
|
+
for (const k in e) {
|
|
11
|
+
if (k !== "default") {
|
|
12
|
+
const d = Object.getOwnPropertyDescriptor(e, k);
|
|
13
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
14
|
+
enumerable: true,
|
|
15
|
+
get: () => e[k]
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
n.default = e;
|
|
21
|
+
return Object.freeze(n);
|
|
22
|
+
}
|
|
23
|
+
const path__namespace = /* @__PURE__ */ _interopNamespaceDefault(path);
|
|
24
|
+
const getScwConfigurationDirectory = () => {
|
|
25
|
+
const xdgConfigPath = node_process.env.XDG_CONFIG_HOME;
|
|
26
|
+
if (typeof xdgConfigPath === "string" && xdgConfigPath.length > 0) {
|
|
27
|
+
return path__namespace.join(xdgConfigPath, "scw");
|
|
28
|
+
}
|
|
29
|
+
return path__namespace.join(node_os.homedir(), ".config", "scw");
|
|
30
|
+
};
|
|
31
|
+
const resolveConfigurationFilePath = () => {
|
|
32
|
+
const envFilePath = node_process.env[env.EnvironmentKey.ScwConfigPath];
|
|
33
|
+
if (typeof envFilePath === "string" && envFilePath.length > 0) {
|
|
34
|
+
return envFilePath;
|
|
35
|
+
}
|
|
36
|
+
return path__namespace.join(getScwConfigurationDirectory(), "config.yaml");
|
|
37
|
+
};
|
|
38
|
+
exports.getScwConfigurationDirectory = getScwConfigurationDirectory;
|
|
39
|
+
exports.resolveConfigurationFilePath = resolveConfigurationFilePath;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gets the Scaleway directory.
|
|
3
|
+
*
|
|
4
|
+
* @returns The path to the Scaleway diretory
|
|
5
|
+
*
|
|
6
|
+
* @internal
|
|
7
|
+
*/
|
|
8
|
+
export declare const getScwConfigurationDirectory: () => string;
|
|
9
|
+
/**
|
|
10
|
+
* Gets the configuration file path.
|
|
11
|
+
*
|
|
12
|
+
* @returns The path to the configuration file
|
|
13
|
+
*
|
|
14
|
+
* @internal
|
|
15
|
+
*/
|
|
16
|
+
export declare const resolveConfigurationFilePath: () => string;
|
package/dist/path-resolver.js
CHANGED
|
@@ -1,39 +1,22 @@
|
|
|
1
|
-
import { homedir } from
|
|
2
|
-
import * as path from
|
|
3
|
-
import { env } from
|
|
4
|
-
import { EnvironmentKey } from
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Gets the Scaleway directory.
|
|
8
|
-
*
|
|
9
|
-
* @returns The path to the Scaleway diretory
|
|
10
|
-
*
|
|
11
|
-
* @internal
|
|
12
|
-
*/
|
|
1
|
+
import { homedir } from "node:os";
|
|
2
|
+
import * as path from "node:path";
|
|
3
|
+
import { env } from "node:process";
|
|
4
|
+
import { EnvironmentKey } from "./env.js";
|
|
13
5
|
const getScwConfigurationDirectory = () => {
|
|
14
6
|
const xdgConfigPath = env.XDG_CONFIG_HOME;
|
|
15
|
-
if (typeof xdgConfigPath ===
|
|
16
|
-
return path.join(xdgConfigPath,
|
|
7
|
+
if (typeof xdgConfigPath === "string" && xdgConfigPath.length > 0) {
|
|
8
|
+
return path.join(xdgConfigPath, "scw");
|
|
17
9
|
}
|
|
18
|
-
return path.join(homedir(),
|
|
10
|
+
return path.join(homedir(), ".config", "scw");
|
|
19
11
|
};
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Gets the configuration file path.
|
|
23
|
-
*
|
|
24
|
-
* @returns The path to the configuration file
|
|
25
|
-
*
|
|
26
|
-
* @internal
|
|
27
|
-
*/
|
|
28
12
|
const resolveConfigurationFilePath = () => {
|
|
29
|
-
// Try path defined by user in env variables
|
|
30
13
|
const envFilePath = env[EnvironmentKey.ScwConfigPath];
|
|
31
|
-
if (typeof envFilePath ===
|
|
14
|
+
if (typeof envFilePath === "string" && envFilePath.length > 0) {
|
|
32
15
|
return envFilePath;
|
|
33
16
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
17
|
+
return path.join(getScwConfigurationDirectory(), "config.yaml");
|
|
18
|
+
};
|
|
19
|
+
export {
|
|
20
|
+
getScwConfigurationDirectory,
|
|
21
|
+
resolveConfigurationFilePath
|
|
37
22
|
};
|
|
38
|
-
|
|
39
|
-
export { getScwConfigurationDirectory, resolveConfigurationFilePath };
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Profile contains information to help instanciating the Scaleway client.
|
|
3
|
+
*
|
|
4
|
+
* @public
|
|
5
|
+
*/
|
|
6
|
+
export type Profile = {
|
|
7
|
+
/**
|
|
8
|
+
* You need an access key and a secret key to connect to Scaleway API.
|
|
9
|
+
* Generate your token at the following address: {@link https://console.scaleway.com/project/credentials}
|
|
10
|
+
*/
|
|
11
|
+
accessKey?: string;
|
|
12
|
+
/**
|
|
13
|
+
* APIURL overrides the API URL of the Scaleway API to the given URL.
|
|
14
|
+
* Change that if you want to direct requests to a different endpoint.
|
|
15
|
+
*
|
|
16
|
+
* @defaultValue `https://api.scaleway.com`
|
|
17
|
+
*/
|
|
18
|
+
apiURL?: string;
|
|
19
|
+
/**
|
|
20
|
+
* Your organization ID is the identifier of your account inside Scaleway infrastructure.
|
|
21
|
+
*/
|
|
22
|
+
defaultOrganizationId?: string;
|
|
23
|
+
/**
|
|
24
|
+
* Your project ID is the identifier of the project your resources are attached to.
|
|
25
|
+
*/
|
|
26
|
+
defaultProjectId?: string;
|
|
27
|
+
/**
|
|
28
|
+
* A region is represented as a geographical area such as France (Paris) or the Netherlands (Amsterdam).
|
|
29
|
+
* It can contain multiple availability zones.
|
|
30
|
+
*
|
|
31
|
+
* Examples: fr-par, nl-ams.
|
|
32
|
+
*/
|
|
33
|
+
defaultRegion?: string;
|
|
34
|
+
/**
|
|
35
|
+
* A region can be split into many availability zones (AZ).
|
|
36
|
+
* Latency between multiple AZ of the same region are low as they have a common network layer.
|
|
37
|
+
*
|
|
38
|
+
* Examples: fr-par-1, nl-ams-1
|
|
39
|
+
*/
|
|
40
|
+
defaultZone?: string;
|
|
41
|
+
/**
|
|
42
|
+
* The secret key is the value that can be used to authenticate against the API (the value used in X-Auth-Token HTTP-header).
|
|
43
|
+
* The secret key MUST remain secret and not given to anyone or published online.
|
|
44
|
+
*/
|
|
45
|
+
secretKey?: string;
|
|
46
|
+
};
|
|
47
|
+
/** Configuration type. */
|
|
48
|
+
export type ConfigurationType = Record<string, Record<string, string>>;
|
|
49
|
+
/** Parameters to load the all the profiles from the configuration file */
|
|
50
|
+
export type AllProfilesFromFileParams = {
|
|
51
|
+
/**
|
|
52
|
+
* The path at which to locate the configuration file.
|
|
53
|
+
*
|
|
54
|
+
* Defaults to the value of the `SCW_CONFIG_PATH` environment variable
|
|
55
|
+
* or `~/.scw/config` otherwise.
|
|
56
|
+
*/
|
|
57
|
+
filepath?: string;
|
|
58
|
+
};
|
|
59
|
+
/** Parameters to load the profile from the configuration file */
|
|
60
|
+
export type ProfileFromFileParams = AllProfilesFromFileParams & {
|
|
61
|
+
/**
|
|
62
|
+
* Name of the profile to load.
|
|
63
|
+
*
|
|
64
|
+
* @defaultValue `default`
|
|
65
|
+
* */
|
|
66
|
+
profileName?: string;
|
|
67
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const fs = require("fs");
|
|
4
|
+
const STRIP_COMMENT_REGEX = /(^|\s)[;#]/;
|
|
5
|
+
const DETECT_SECTION_REGEX = /^\s*([^]+):\s*$/;
|
|
6
|
+
const DETECT_ITEM_REGEX = /^\s*(.+?)\s*:\s*(.+?)\s*$/;
|
|
7
|
+
const convertYamlToConfiguration = (input) => {
|
|
8
|
+
let foundProfilesKey = false;
|
|
9
|
+
let currentSection = "default";
|
|
10
|
+
const map = {};
|
|
11
|
+
if (typeof input !== "string") {
|
|
12
|
+
return map;
|
|
13
|
+
}
|
|
14
|
+
input.split(/\r?\n/).forEach((rawLine) => {
|
|
15
|
+
const line = rawLine.split(STRIP_COMMENT_REGEX)[0];
|
|
16
|
+
const newSection = DETECT_SECTION_REGEX.exec(line);
|
|
17
|
+
if (newSection) {
|
|
18
|
+
currentSection = void 0;
|
|
19
|
+
if (newSection[1] === "profiles") {
|
|
20
|
+
foundProfilesKey = true;
|
|
21
|
+
} else if (foundProfilesKey) {
|
|
22
|
+
[, currentSection] = newSection;
|
|
23
|
+
}
|
|
24
|
+
} else if (currentSection) {
|
|
25
|
+
const item = DETECT_ITEM_REGEX.exec(line);
|
|
26
|
+
if (item) {
|
|
27
|
+
if (typeof map[currentSection] !== "object") {
|
|
28
|
+
map[currentSection] = {};
|
|
29
|
+
}
|
|
30
|
+
[, , map[currentSection][item[1]]] = item;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
return map;
|
|
35
|
+
};
|
|
36
|
+
const loadConfigurationFromFile = (filePath) => {
|
|
37
|
+
const fileContent = fs.readFileSync(filePath, "utf-8");
|
|
38
|
+
return convertYamlToConfiguration(fileContent);
|
|
39
|
+
};
|
|
40
|
+
exports.convertYamlToConfiguration = convertYamlToConfiguration;
|
|
41
|
+
exports.loadConfigurationFromFile = loadConfigurationFromFile;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { ConfigurationType } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Converts YAML to configuration map.
|
|
4
|
+
*
|
|
5
|
+
* @param input - YAML string
|
|
6
|
+
* @returns The configuration map
|
|
7
|
+
*
|
|
8
|
+
* @internal
|
|
9
|
+
*/
|
|
10
|
+
export declare const convertYamlToConfiguration: (input: string | null) => ConfigurationType;
|
|
11
|
+
/**
|
|
12
|
+
* Loads configuration from a file.
|
|
13
|
+
*
|
|
14
|
+
* @param filePath - Path to the configuration file
|
|
15
|
+
* @returns The configuration
|
|
16
|
+
*
|
|
17
|
+
* @throws Error
|
|
18
|
+
* Thrown if the file doesn't exist.
|
|
19
|
+
*
|
|
20
|
+
* @internal
|
|
21
|
+
*/
|
|
22
|
+
export declare const loadConfigurationFromFile: (filePath: string) => ConfigurationType;
|
package/dist/yml-loader.js
CHANGED
|
@@ -1,67 +1,41 @@
|
|
|
1
|
-
import { readFileSync } from
|
|
2
|
-
|
|
1
|
+
import { readFileSync } from "fs";
|
|
3
2
|
const STRIP_COMMENT_REGEX = /(^|\s)[;#]/;
|
|
4
3
|
const DETECT_SECTION_REGEX = /^\s*([^]+):\s*$/;
|
|
5
4
|
const DETECT_ITEM_REGEX = /^\s*(.+?)\s*:\s*(.+?)\s*$/;
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Converts YAML to configuration map.
|
|
9
|
-
*
|
|
10
|
-
* @param input - YAML string
|
|
11
|
-
* @returns The configuration map
|
|
12
|
-
*
|
|
13
|
-
* @internal
|
|
14
|
-
*/
|
|
15
|
-
const convertYamlToConfiguration = input => {
|
|
5
|
+
const convertYamlToConfiguration = (input) => {
|
|
16
6
|
let foundProfilesKey = false;
|
|
17
|
-
let currentSection =
|
|
7
|
+
let currentSection = "default";
|
|
18
8
|
const map = {};
|
|
19
|
-
if (typeof input !==
|
|
9
|
+
if (typeof input !== "string") {
|
|
20
10
|
return map;
|
|
21
11
|
}
|
|
22
|
-
input.split(/\r?\n/).forEach(rawLine => {
|
|
23
|
-
// remove comments
|
|
12
|
+
input.split(/\r?\n/).forEach((rawLine) => {
|
|
24
13
|
const line = rawLine.split(STRIP_COMMENT_REGEX)[0];
|
|
25
|
-
// parse sections
|
|
26
14
|
const newSection = DETECT_SECTION_REGEX.exec(line);
|
|
27
15
|
if (newSection) {
|
|
28
|
-
currentSection =
|
|
29
|
-
if (newSection[1] ===
|
|
16
|
+
currentSection = void 0;
|
|
17
|
+
if (newSection[1] === "profiles") {
|
|
30
18
|
foundProfilesKey = true;
|
|
31
19
|
} else if (foundProfilesKey) {
|
|
32
20
|
[, currentSection] = newSection;
|
|
33
21
|
}
|
|
34
|
-
}
|
|
35
|
-
// parse items
|
|
36
|
-
else if (currentSection) {
|
|
22
|
+
} else if (currentSection) {
|
|
37
23
|
const item = DETECT_ITEM_REGEX.exec(line);
|
|
38
24
|
if (item) {
|
|
39
|
-
if (typeof map[currentSection] !==
|
|
25
|
+
if (typeof map[currentSection] !== "object") {
|
|
40
26
|
map[currentSection] = {};
|
|
41
27
|
}
|
|
42
|
-
[
|
|
28
|
+
[, , map[currentSection][item[1]]] = item;
|
|
43
29
|
}
|
|
44
30
|
}
|
|
45
31
|
});
|
|
46
32
|
return map;
|
|
47
33
|
};
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
* Loads configuration from a file.
|
|
51
|
-
*
|
|
52
|
-
* @param filePath - Path to the configuration file
|
|
53
|
-
* @returns The configuration
|
|
54
|
-
*
|
|
55
|
-
* @throws Error
|
|
56
|
-
* Thrown if the file doesn't exist.
|
|
57
|
-
*
|
|
58
|
-
* @internal
|
|
59
|
-
*/
|
|
60
|
-
const loadConfigurationFromFile = filePath => {
|
|
61
|
-
// `readFileSync` returns a string when encoding option is specified.
|
|
62
|
-
// {@link https://nodejs.org/api/fs.html#fs_fs_readfilesync_path_options}
|
|
63
|
-
const fileContent = readFileSync(filePath, 'utf-8');
|
|
34
|
+
const loadConfigurationFromFile = (filePath) => {
|
|
35
|
+
const fileContent = readFileSync(filePath, "utf-8");
|
|
64
36
|
return convertYamlToConfiguration(fileContent);
|
|
65
37
|
};
|
|
66
|
-
|
|
67
|
-
|
|
38
|
+
export {
|
|
39
|
+
convertYamlToConfiguration,
|
|
40
|
+
loadConfigurationFromFile
|
|
41
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@scaleway/configuration-loader",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.5",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"description": "Load configuration via file or environment for NodeJS.",
|
|
6
6
|
"main": "dist/index.cjs",
|
|
@@ -18,8 +18,14 @@
|
|
|
18
18
|
"node": ">=14.13"
|
|
19
19
|
},
|
|
20
20
|
"type": "module",
|
|
21
|
+
"scripts": {
|
|
22
|
+
"typecheck": "tsc --noEmit",
|
|
23
|
+
"type:generate": "tsc --declaration -p tsconfig.build.json",
|
|
24
|
+
"build": "vite build --config ../../vite.config.ts && pnpm run type:generate",
|
|
25
|
+
"build:profile": "npx vite-bundle-visualizer -c ../../vite.config.ts"
|
|
26
|
+
},
|
|
21
27
|
"devDependencies": {
|
|
22
28
|
"@types/node": "18.11.18"
|
|
23
29
|
},
|
|
24
|
-
"gitHead": "
|
|
30
|
+
"gitHead": "a7d80e98eaa7977ce6ac697f3003823972f5c52f"
|
|
25
31
|
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "./tsconfig.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"noEmit": false,
|
|
5
|
+
"emitDeclarationOnly": true,
|
|
6
|
+
"rootDir": "src",
|
|
7
|
+
"outDir": "dist",
|
|
8
|
+
"skipLibCheck": true
|
|
9
|
+
},
|
|
10
|
+
"exclude": [
|
|
11
|
+
"dist/*",
|
|
12
|
+
"*.config.ts",
|
|
13
|
+
"*.setup.ts",
|
|
14
|
+
"**/__tests__",
|
|
15
|
+
"**/__mocks__",
|
|
16
|
+
"src/**/*.test.tsx"
|
|
17
|
+
]
|
|
18
|
+
}
|
package/tsconfig.json
ADDED