-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
89 lines (71 loc) · 2.8 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
module.exports = {
onPreBuild: ({inputs, netlifyConfig}) => {
console.group("Starting the \"uefn\" plugin process");
const prefix = inputs.prefix || process.env.NETLIFY_PLUGIN_USE_ENV_IN_RUNTIME_PREFIX;
// Stop the process if there is no prefix input without breaking the build
if (!prefix) {
return console.warn("No \"prefix\" input defined. Skip the process.");
}
console.info(`Defined prefix: "${prefix}"`);
// Stop the process if there is no def input without breaking the build
const hasDef = inputs.def || process.env.NETLIFY_PLUGIN_USE_ENV_IN_RUNTIME_DEF;
if (!hasDef) {
return console.warn("No \"def\" input defined. Skip the process.");
}
// Build definitions
const definitions = buildGlobalDefinitions(inputs.def);
console.info("Built-in definitions: ", definitions);
console.group("Definition process");
// Set the process env object
for (const definition of definitions) {
// Use old concat to provide a support to old Node versions
const key = `${prefix}_${definition}`;
netlifyConfig.build.environment[key] = process.env[definition];
}
console.groupEnd();
console.info("Use Env In Runtime plugin process completed");
console.groupEnd();
console.info("The environment variables have been added successfully!");
}
};
/**
* Builds an array of definitions from the definitions defined in Netlify UI and in the netlify.toml file.
* @param tomlDef
* @return {*[]}
*/
function buildGlobalDefinitions(tomlDef) {
console.group("Set global definitions (merge UI definitions with TOML definitions if exists)");
// Get definitions sets in the Netlify UI
const uiDef = parseUIDefinitions();
console.info("- Parse TOML definitions if exists");
const parsedTomlDef = Array.isArray(tomlDef) ? tomlDef : splitDefinitions(tomlDef);
console.groupEnd();
// Important! The definitions sets in the TOML file override those sets in the Netlify UI
return [...uiDef, ...parsedTomlDef];
}
/**
* Parse definitions sets in the Netlify user interface
* @return {*[]}
*/
function parseUIDefinitions() {
console.info("- Parse UI definitions if exists");
const {NETLIFY_PLUGIN_USE_ENV_IN_RUNTIME_DEF: uiDef} = process.env;
if (!uiDef) {
console.info("No def has been defined through the Netlify UI");
return [];
}
// If the string is an array
const isArrayLike = /\[/.test(uiDef);
return isArrayLike ? JSON.parse(uiDef) : splitDefinitions(uiDef);
}
/**
* Convert a string of definitions into an array of definitions
* @param {string} rawDef - Definitions in string format
* @example
* // returns ["VAR_1", "VAR_2", "VAR_3"]
* splitDefinitions("VAR_1, VAR_2, VAR_3")
* @return {string[]}
*/
function splitDefinitions(rawDef) {
return rawDef.split(/\s*[;|,|\s]\s*/);
}