This repository was archived by the owner on Sep 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathdev.js
117 lines (112 loc) · 3.57 KB
/
dev.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// reusable code for netlify dev
// bit of a hasty abstraction but recommended by oclif
const { getAddons } = require("netlify/src/addons");
const chalk = require("chalk");
const {
NETLIFYDEVLOG,
NETLIFYDEVWARN,
NETLIFYDEVERR
} = require("netlify-cli-logo");
/**
* inject environment variables from netlify addons and buildbot
* into your local dev process.env
*
* ```
* // usage example
* const { site, api } = this.netlify
* if (site.id) {
* const accessToken = api.accessToken
* const addonUrls = await addEnvVariables(site, accessToken)
* // addonUrls is only for startProxy in netlify dev:index
* }
* ```
*/
async function addEnvVariables(api, site, accessToken) {
/** from addons */
const addonUrls = {};
const addons = await getAddons(site.id, accessToken).catch(err => {
console.error(err);
switch (err.status) {
default:
console.error(
`${NETLIFYDEVERR} Error retrieving addons data for site ${chalk.yellow(
site.id
)}. Double-check your login status with 'netlify status' or contact support with details of your error.`
);
process.exit();
}
});
if (Array.isArray(addons)) {
addons.forEach(addon => {
addonUrls[addon.slug] = `${addon.config.site_url}/.netlify/${addon.slug}`;
for (const key in addon.env) {
const msg = () =>
console.log(
`${NETLIFYDEVLOG} Injected ${chalk.yellow.bold("addon")} env var: `,
chalk.yellow(key)
);
process.env[key] = assignLoudly(process.env[key], addon.env[key], msg);
}
});
}
/** from web UI */
const apiSite = await api.getSite({ site_id: site.id }).catch(err => {
console.error(err);
switch (err.status) {
case 401:
console.error(
`${NETLIFYDEVERR} Unauthorized error: This Site ID ${chalk.yellow(
site.id
)} does not belong to your account.`
);
console.error(
`${NETLIFYDEVERR} If you cloned someone else's code, try running 'npm unlink' and then 'npm init' or 'npm link'.`
);
process.exit();
default:
console.error(
`${NETLIFYDEVERR} Error retrieving site data for site ${chalk.yellow(
site.id
)}. Double-check your login status with 'netlify status' or contact support with details of your error.`
);
process.exit();
}
});
// TODO: We should move the environment outside of build settings and possibly have a
// `/api/v1/sites/:site_id/environment` endpoint for it that we can also gate access to
// In the future and that we could make context dependend
if (apiSite.build_settings && apiSite.build_settings.env) {
for (const key in apiSite.build_settings.env) {
const msg = () =>
console.log(
`${NETLIFYDEVLOG} Injected ${chalk.blue.bold(
"build setting"
)} env var: `,
chalk.yellow(key)
);
process.env[key] = assignLoudly(
process.env[key],
apiSite.build_settings.env[key],
msg
);
}
}
return addonUrls;
}
module.exports = {
addEnvVariables
};
// if first arg is undefined, use default, but tell user about it in case it is unintentional
function assignLoudly(
optionalValue,
defaultValue,
tellUser = dV => console.log(`No value specified, using fallback of `, dV)
) {
if (defaultValue === undefined) throw new Error("must have a defaultValue");
if (defaultValue !== optionalValue && optionalValue === undefined) {
tellUser(defaultValue);
return defaultValue;
} else {
return optionalValue;
}
}