1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.prepareHooks = prepareHooks;
|
7 | exports.runHook = runHook;
|
8 | function _decache() {
|
9 | const data = _interopRequireDefault(require("decache"));
|
10 | _decache = function () {
|
11 | return data;
|
12 | };
|
13 | return data;
|
14 | }
|
15 | function _resolveFrom() {
|
16 | const data = _interopRequireDefault(require("resolve-from"));
|
17 | _resolveFrom = function () {
|
18 | return data;
|
19 | };
|
20 | return data;
|
21 | }
|
22 | function _internal() {
|
23 | const data = require("../internal");
|
24 | _internal = function () {
|
25 | return data;
|
26 | };
|
27 | return data;
|
28 | }
|
29 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
30 | function requireFromProject(projectRoot, modulePath) {
|
31 | try {
|
32 | const fullPath = (0, _resolveFrom().default)(projectRoot, modulePath);
|
33 |
|
34 |
|
35 | (0, _decache().default)(fullPath);
|
36 | return require(fullPath);
|
37 | } catch {
|
38 | return null;
|
39 | }
|
40 | }
|
41 | function prepareHooks(hooks, hookType, projectRoot) {
|
42 | const validHooks = [];
|
43 | if (hooks) {
|
44 | var _hooks$hookType;
|
45 | if (hooks[hookType]) {
|
46 | hooks[hookType].forEach(hook => {
|
47 | const {
|
48 | file
|
49 | } = hook;
|
50 | const fn = requireFromProject(projectRoot, file);
|
51 | if (typeof fn !== 'function') {
|
52 | _internal().Logger.global.error(`Unable to load ${hookType} hook: '${file}'. The module does not export a function.`);
|
53 | } else {
|
54 | hook._fn = fn;
|
55 | validHooks.push(hook);
|
56 | }
|
57 | });
|
58 | }
|
59 | if (hooks[hookType] !== undefined && validHooks.length !== ((_hooks$hookType = hooks[hookType]) === null || _hooks$hookType === void 0 ? void 0 : _hooks$hookType.length)) {
|
60 | throw new (_internal().XDLError)('HOOK_INITIALIZATION_ERROR', `Please fix your ${hookType} hook configuration`);
|
61 | }
|
62 | }
|
63 | return validHooks;
|
64 | }
|
65 | async function runHook(hook, hookOptions) {
|
66 | let result = hook._fn({
|
67 | config: hook.config,
|
68 | ...hookOptions
|
69 | });
|
70 |
|
71 |
|
72 | if (result && result.then) {
|
73 | result = await result;
|
74 | }
|
75 | if (result) {
|
76 | _internal().Logger.global.info({
|
77 | quiet: true
|
78 | }, result);
|
79 | }
|
80 | }
|
81 |
|
\ | No newline at end of file |