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 pathhook.js
65 lines (55 loc) · 1.8 KB
/
hook.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
const execa = require("execa");
const Command = require("@netlify/cli-utils");
const { track } = require("@netlify/cli-utils/src/utils/telemetry");
const {
// NETLIFYDEV,
NETLIFYDEVLOG,
// NETLIFYDEVWARN,
NETLIFYDEVERR
} = require("netlify-cli-logo");
class HookCommand extends Command {
async run() {
const { site, api, config } = this.netlify;
if (!(config && config.dev && config.dev.hooks)) {
this.log(`${NETLIFYDEVERR} No hooks defined in your netlify.toml.`);
process.exit(1);
}
if (site.id) {
this.log(
`${NETLIFYDEVLOG} Checking your site's environment variables...`
); // just to show some visual response first
const accessToken = api.accessToken;
const { addEnvVariables } = require("../../utils/dev");
await addEnvVariables(api, site, accessToken);
} else {
this.log(
`${NETLIFYDEVERR} No Site ID detected. You probably forgot to run \`netlify link\` or \`netlify init\`. `
);
}
const hook = this.argv.join(" ");
const cmd = config.dev.hooks[hook];
if (!cmd) {
this.log(
`${NETLIFYDEVERR} No hook called "${hook}" defined in your netlify.toml.`
);
process.exit(1);
}
execa(cmd.split(" ")[0], cmd.split(" ").slice(1), {
env: process.env,
stdio: "inherit"
});
// Todo hoist this telemetry `command` to CLI hook
track("command", {
command: "dev:hook"
});
}
}
HookCommand.description = `Hook command
Responds to one of the named hook defined in your netlify.toml file.
Used with \`netlify dev --live\` to handle updates on webhooks triggered
by external systems like Contentful, Sanity, etc...
`;
HookCommand.examples = ["$ netlify dev:hook Contentful"];
HookCommand.strict = false;
HookCommand.parse = false;
module.exports = HookCommand;