-
-
Notifications
You must be signed in to change notification settings - Fork 171
/
create-aws-lambda-renderer.js
66 lines (60 loc) 路 1.82 KB
/
create-aws-lambda-renderer.js
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const chromium = require('chrome-aws-lambda');
const mapLimit = require('async/mapLimit');
const { createChromeAppTarget } = require('@loki/target-chrome-app');
const { serializeError, unwrapError } = require('@loki/core');
const getStorybook = async target => {
const stories = await target.getStorybook();
return stories;
};
const captureScreenshotForStory = async (target, event) => {
const screenshot = await target.captureScreenshotForStory(
event.kind,
event.story,
event.options || {},
event.configuration || {}
);
return screenshot.toString('base64');
};
const captureScreenshotsForStories = async (target, event) => {
const concurrency =
(event.options && event.options.chromeAwsLambdaBatchConcurrency) || 1;
return mapLimit(event.stories, concurrency, async task => {
try {
const screenshot = await captureScreenshotForStory(target, {
kind: task.kind,
story: task.story,
configuration: task.configuration,
options: event.options,
});
return screenshot;
} catch (error) {
return { errorMessage: serializeError(unwrapError(error)) };
}
});
};
const commands = {
getStorybook,
captureScreenshotForStory,
captureScreenshotsForStories,
};
const createChromeAWSLambdaRenderer = () => async event => {
const command = commands[event.command];
if (!command) {
throw serializeError(new Error(`Unknown command "${event.command}"`));
}
const target = createChromeAppTarget({
baseUrl: event.baseUrl,
});
try {
await target.start({
chromeFlags: chromium.args,
chromePath: await chromium.executablePath,
});
return await command(target, event);
} catch (error) {
throw serializeError(unwrapError(error));
} finally {
await target.stop();
}
};
module.exports = createChromeAWSLambdaRenderer;