-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.ts
51 lines (39 loc) · 1.4 KB
/
index.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
export type MaybeObject = void | {}
export type StartReporter = NodeJS.EventEmitter
export type StartFile = {
path: string,
data?: string,
map?: {},
}
export type StartDataFile = StartFile & {
data: string,
}
export type StartFilesProps = {
files: StartFile[],
}
export type StartDataFilesProps = {
files: StartDataFile[],
}
export type StartPluginUtils = {
reporter: StartReporter,
logPath: (path: string) => void,
logMessage: (message: string) => void,
}
export type StartPluginCallback<P extends {}, R extends MaybeObject> = (utils: StartPluginUtils) => (props: P) => R | Promise<R>
export type StartPluginWrapper<P extends {}, R extends MaybeObject> = (reporter: StartReporter) => (inProps?: P) => Promise<R>
export type StartPlugin<P extends {}, R extends MaybeObject> = StartPluginWrapper<P, R> | Promise<StartPluginWrapper<P, R>>
export default <P extends {}, R extends MaybeObject> (name: string, pluginCallback: StartPluginCallback<P, R>): StartPlugin<P, R> => (reporter) => async (inProps?: P): Promise<R> => {
try {
reporter.emit('start', name)
const outProps = await pluginCallback({
reporter,
logPath: (path) => reporter.emit('path', name, path),
logMessage: (message) => reporter.emit('message', name, message),
})(inProps as P)
reporter.emit('done', name)
return outProps
} catch (error) {
reporter.emit('error', name, error)
throw null
}
}