Skip to content

Commit 754cd94

Browse files
committed
get tests passing
1 parent 4a77268 commit 754cd94

File tree

1 file changed

+80
-84
lines changed

1 file changed

+80
-84
lines changed

src/utils/prepareRunHook.ts

Lines changed: 80 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -5,108 +5,104 @@ import createReadOnlyProxy from './createReadOnlyProxy';
55
function prepareRunHook({ hooks, allSupportedHooks, settings }) {
66
// eslint-disable-next-line consistent-return
77
return async function processHook(hookName, props: any = {}) {
8-
try {
9-
if (props.perf) props.perf.start(`hook.${hookName}`);
8+
if (props.perf) props.perf.start(`hook.${hookName}`);
109

11-
// do we have a contract for the hook
12-
const hookDefinition = allSupportedHooks.find((h) => h.hook === hookName);
13-
if (!hookDefinition) {
14-
throw new Error(`Hook ${hookName} not defined in hookInterface or via plugins.`);
15-
}
10+
// do we have a contract for the hook
11+
const hookDefinition = allSupportedHooks.find((h) => h.hook === hookName);
12+
if (!hookDefinition) {
13+
throw new Error(`Hook ${hookName} not defined in hookInterface or via plugins.`);
14+
}
1615

17-
const hookProps = hookDefinition.props.reduce((out, cv) => {
18-
if (cv === 'perf') return out; // perf added and prefixed below
16+
const hookProps = hookDefinition.props.reduce((out, cv) => {
17+
if (cv === 'perf') return out; // perf added and prefixed below
1918

20-
if (Object.hasOwnProperty.call(props, cv)) {
21-
if (!hookDefinition.mutable.includes(cv)) {
22-
out[cv] = createReadOnlyProxy(props[cv], cv, hookName);
23-
} else {
24-
out[cv] = props[cv];
25-
}
19+
if (Object.hasOwnProperty.call(props, cv)) {
20+
if (!hookDefinition.mutable.includes(cv)) {
21+
out[cv] = createReadOnlyProxy(props[cv], cv, hookName);
2622
} else {
27-
console.error(
28-
`Hook named '${hookName}' cannot be run because prop ${cv} is not in scope to pass to the hook. Hook contract broken.`,
29-
);
23+
out[cv] = props[cv];
3024
}
25+
} else {
26+
console.error(
27+
`Hook named '${hookName}' cannot be run because prop ${cv} is not in scope to pass to the hook. Hook contract broken.`,
28+
);
29+
}
3130

32-
return out;
33-
}, {});
34-
35-
const theseHooks = hooks.filter((h) => h.hook === hookName);
36-
if (theseHooks && Array.isArray(theseHooks) && theseHooks.length > 0) {
37-
// higher priority is more important.
38-
const hookList = theseHooks.sort((a, b) => b.priority - a.priority);
31+
return out;
32+
}, {});
3933

40-
if (settings && settings.debug && settings.debug.hooks) {
41-
console.log(`Hooks registered on ${hookName}:`, hookList);
42-
}
34+
const theseHooks = hooks.filter((h) => h.hook === hookName);
35+
if (theseHooks && Array.isArray(theseHooks) && theseHooks.length > 0) {
36+
// higher priority is more important.
37+
const hookList = theseHooks.sort((a, b) => b.priority - a.priority);
4338

44-
const hookOutput = {};
39+
if (settings && settings.debug && settings.debug.hooks) {
40+
console.log(`Hooks registered on ${hookName}:`, hookList);
41+
}
4542

46-
// loop through the hooks, updating the output and the props in order
47-
await hookList.reduce((p, hook) => {
48-
return p.then(async () => {
49-
if (props.perf) props.perf.start(`hook.${hookName}.${hook.name}`);
50-
try {
51-
let hookResponse = await hook.run({
52-
...hookProps,
53-
perf: props.perf.prefix(`hook.${hookName}.${hook.name}`),
54-
});
43+
const hookOutput = {};
5544

56-
if (!hookResponse) hookResponse = {};
45+
// loop through the hooks, updating the output and the props in order
46+
await hookList.reduce((p, hook) => {
47+
return p.then(async () => {
48+
if (props.perf) props.perf.start(`hook.${hookName}.${hook.name}`);
49+
try {
50+
let hookResponse = await hook.run({
51+
...hookProps,
52+
perf: props.perf.prefix(`hook.${hookName}.${hook.name}`),
53+
});
5754

58-
if (settings && settings.debug && settings.debug.hooks) {
59-
console.log(`${hook.name} ran on ${hookName} and returned`, hookResponse);
60-
}
55+
if (!hookResponse) hookResponse = {};
6156

62-
Object.keys(hookResponse).forEach((key) => {
63-
if (hookDefinition.mutable && hookDefinition.mutable.includes(key)) {
64-
hookOutput[key] = hookResponse[key];
65-
hookProps[key] = hookResponse[key];
66-
} else {
67-
console.error(
68-
`Received attempted mutation on "${hookName}" from "${hook.name}" on the object "${key}". ${key} is not mutable on this hook `,
69-
hook.$$meta,
70-
);
71-
}
72-
});
73-
} catch (e) {
74-
console.error(e);
75-
e.message = `Hook: "${hook.name}" threw an error: ${e.message}`;
76-
props.errors.push(e);
77-
if (hookName === 'buildComplete') console.error(e);
78-
}
79-
if (props.perf) props.perf.end(`hook.${hookName}.${hook.name}`);
80-
});
81-
}, Promise.resolve());
82-
83-
// this actually mutates the props.
84-
if (
85-
Object.keys(hookOutput).length > 0 &&
86-
Array.isArray(hookDefinition.mutable) &&
87-
hookDefinition.mutable.length > 0
88-
) {
89-
hookDefinition.mutable.forEach((key) => {
90-
if ({}.hasOwnProperty.call(hookOutput, key)) {
91-
props[key] = hookOutput[key];
57+
if (settings && settings.debug && settings.debug.hooks) {
58+
console.log(`${hook.name} ran on ${hookName} and returned`, hookResponse);
9259
}
93-
});
94-
}
95-
96-
if (settings && settings.debug && settings.debug.hooks) console.log(`${hookName} finished`);
9760

98-
if (props.perf) props.perf.end(`hook.${hookName}`);
99-
return hookOutput;
100-
}
101-
if (settings && settings.debug && settings.debug.hooks) {
102-
console.log(`${hookName} finished without executing any functions`);
61+
Object.keys(hookResponse).forEach((key) => {
62+
if (hookDefinition.mutable && hookDefinition.mutable.includes(key)) {
63+
hookOutput[key] = hookResponse[key];
64+
hookProps[key] = hookResponse[key];
65+
} else {
66+
console.error(
67+
`Received attempted mutation on "${hookName}" from "${hook.name}" on the object "${key}". ${key} is not mutable on this hook `,
68+
hook.$$meta,
69+
);
70+
}
71+
});
72+
} catch (e) {
73+
console.error(e);
74+
e.message = `Hook: "${hook.name}" threw an error: ${e.message}`;
75+
props.errors.push(e);
76+
if (hookName === 'buildComplete') console.error(e);
77+
}
78+
if (props.perf) props.perf.end(`hook.${hookName}.${hook.name}`);
79+
});
80+
}, Promise.resolve());
81+
82+
// this actually mutates the props.
83+
if (
84+
Object.keys(hookOutput).length > 0 &&
85+
Array.isArray(hookDefinition.mutable) &&
86+
hookDefinition.mutable.length > 0
87+
) {
88+
hookDefinition.mutable.forEach((key) => {
89+
if ({}.hasOwnProperty.call(hookOutput, key)) {
90+
props[key] = hookOutput[key];
91+
}
92+
});
10393
}
10494

95+
if (settings && settings.debug && settings.debug.hooks) console.log(`${hookName} finished`);
96+
10597
if (props.perf) props.perf.end(`hook.${hookName}`);
106-
return props;
107-
} catch (e) {
108-
console.error(e);
98+
return hookOutput;
10999
}
100+
if (settings && settings.debug && settings.debug.hooks) {
101+
console.log(`${hookName} finished without executing any functions`);
102+
}
103+
104+
if (props.perf) props.perf.end(`hook.${hookName}`);
105+
return props;
110106
};
111107
}
112108

0 commit comments

Comments
 (0)