UNPKG

1.55 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const tslib_1 = require("tslib");
4const cli_ux_1 = require("cli-ux");
5const path = require("path");
6const deps_1 = require("./deps");
7const debug = require('debug')('cli:hooks');
8class Hook {
9 constructor(config, options = {}) {
10 this.config = config;
11 this.options = options;
12 }
13}
14exports.Hook = Hook;
15class Hooks {
16 constructor(config) {
17 this.config = config;
18 }
19 run(event, options = {}) {
20 return tslib_1.__awaiter(this, void 0, void 0, function* () {
21 let scripts = this.config.hooks[event];
22 if (!scripts || !this.config.root)
23 return;
24 for (let script of scripts) {
25 script = path.join(this.config.root, script);
26 debug(`%s %s`, event, script);
27 let Hook;
28 try {
29 Hook = deps_1.default.util.undefault(require(script));
30 }
31 catch (err) {
32 cli_ux_1.default.warn(err, { context: `hook:${event} loading ${script}` });
33 continue;
34 }
35 if (this.isLegacyHook(Hook)) {
36 yield Hook(this.config, options);
37 }
38 else {
39 const hook = new Hook(this.config, options);
40 yield hook.run();
41 }
42 }
43 });
44 }
45 isLegacyHook(Hook) {
46 return !Hook.prototype;
47 }
48}
49exports.Hooks = Hooks;