-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
addons.ts
51 lines (46 loc) · 1.52 KB
/
addons.ts
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
import Graph from '../Graph';
import { OutputOptions } from '../rollup/types';
import { error } from './error';
export interface Addons {
intro?: string;
outro?: string;
banner?: string;
footer?: string;
}
function evalIfFn(strOrFn: string | (() => string | Promise<string>)): string | Promise<string> {
switch (typeof strOrFn) {
case 'function':
return strOrFn();
case 'string':
return strOrFn;
default:
return '';
}
}
const concatSep = (out: string, next: string) => (next ? `${out}\n${next}` : out);
const concatDblSep = (out: string, next: string) => (next ? `${out}\n\n${next}` : out);
export function createAddons(graph: Graph, options: OutputOptions): Promise<Addons> {
const pluginDriver = graph.pluginDriver;
return Promise.all([
pluginDriver.hookReduceValue('banner', evalIfFn(options.banner), [], concatSep),
pluginDriver.hookReduceValue('footer', evalIfFn(options.footer), [], concatSep),
pluginDriver.hookReduceValue('intro', evalIfFn(options.intro), [], concatDblSep),
pluginDriver.hookReduceValue('outro', evalIfFn(options.outro), [], concatDblSep)
])
.then(([banner, footer, intro, outro]) => {
if (intro) intro += '\n\n';
if (outro) outro = `\n\n${outro}`;
if (banner.length) banner += '\n';
if (footer.length) footer = '\n' + footer;
return { intro, outro, banner, footer };
})
.catch(
(err): any => {
error({
code: 'ADDON_ERROR',
message: `Could not retrieve ${err.hook}. Check configuration of ${err.plugin}.
\tError Message: ${err.message}`
});
}
);
}