1 | var toml = require('toml');
|
2 | var fs = require('fs');
|
3 | var path = require('path');
|
4 | var util = require('util');
|
5 |
|
6 | const readFile = util.promisify(fs.readFile);
|
7 |
|
8 | exports.load = async function () {
|
9 | try {
|
10 | const configPath = path.join(process.cwd(), 'netlify.toml');
|
11 | const content = await readFile(configPath, 'utf8');
|
12 | return toml.parse(content);
|
13 | } catch (error) {
|
14 | if (error.code === 'ENOENT') {
|
15 | console.error(
|
16 | 'No netlify.toml found. This is needed to configure the function settings. For more info: https://github.com/netlify/netlify-lambda#installation',
|
17 | );
|
18 | } else {
|
19 | console.error(error);
|
20 | }
|
21 | process.exit(1);
|
22 | }
|
23 | };
|
24 |
|
25 | exports.loadContext = function (config) {
|
26 | var buildConfig = config.build;
|
27 | var contextConfig =
|
28 | (process.env.CONTEXT &&
|
29 | config.context &&
|
30 | config.context[process.env.CONTEXT]) ||
|
31 | {};
|
32 | var branchConfig =
|
33 | (process.env.BRANCH &&
|
34 | config.context &&
|
35 | config.context[process.env.BRANCH]) ||
|
36 | {};
|
37 | var buildEnv = buildConfig.environment || buildConfig.Environment || {};
|
38 | var contextEnv = contextConfig.environment || contextConfig.Environment || {};
|
39 | var branchEnv = branchConfig.environment || branchConfig.Environment || {};
|
40 | return {
|
41 | ...buildConfig,
|
42 | ...contextConfig,
|
43 | ...branchConfig,
|
44 | environment: {
|
45 | ...buildEnv,
|
46 | ...contextEnv,
|
47 | ...branchEnv,
|
48 | },
|
49 | };
|
50 | };
|