UNPKG

2.3 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.prepareHooks = prepareHooks;
7exports.runHook = runHook;
8function _decache() {
9 const data = _interopRequireDefault(require("decache"));
10 _decache = function () {
11 return data;
12 };
13 return data;
14}
15function _resolveFrom() {
16 const data = _interopRequireDefault(require("resolve-from"));
17 _resolveFrom = function () {
18 return data;
19 };
20 return data;
21}
22function _internal() {
23 const data = require("../internal");
24 _internal = function () {
25 return data;
26 };
27 return data;
28}
29function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
30function requireFromProject(projectRoot, modulePath) {
31 try {
32 const fullPath = (0, _resolveFrom().default)(projectRoot, modulePath);
33 // Clear the require cache for this module so get a fresh version of it
34 // without requiring the user to restart Expo CLI
35 (0, _decache().default)(fullPath);
36 return require(fullPath);
37 } catch {
38 return null;
39 }
40}
41function 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}
65async function runHook(hook, hookOptions) {
66 let result = hook._fn({
67 config: hook.config,
68 ...hookOptions
69 });
70
71 // If it's a promise, wait for it to resolve
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//# sourceMappingURL=runHook.js.map
\No newline at end of file