1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = getStaticAssets;
|
7 |
|
8 | var _path = require('path');
|
9 |
|
10 | var _path2 = _interopRequireDefault(_path);
|
11 |
|
12 | var _walk = require('walk');
|
13 |
|
14 | var _walk2 = _interopRequireDefault(_walk);
|
15 |
|
16 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
17 |
|
18 | var fs = require('fs');
|
19 |
|
20 |
|
21 | var SKIPPED_ASSETS = ['index.html', 'iframe.html', 'favicon.ico', 'static/', /\.map$/, /\.log$/, /\.DS_Store$/];
|
22 | var MAX_FILE_SIZE_BYTES = 15728640;
|
23 |
|
24 |
|
25 | function gatherBuildResources(buildDir) {
|
26 | var hashToResource = {};
|
27 | var walkOptions = {
|
28 |
|
29 | followLinks: true,
|
30 |
|
31 | listeners: {
|
32 | file: function file(root, fileStats, next) {
|
33 | var absolutePath = _path2.default.join(root, fileStats.name);
|
34 | var resourceUrl = absolutePath.replace(buildDir, '');
|
35 |
|
36 | if (_path2.default.sep === '\\') {
|
37 |
|
38 | resourceUrl = resourceUrl.replace('\\', '/');
|
39 | }
|
40 |
|
41 |
|
42 | if (resourceUrl.charAt(0) === '/') {
|
43 | resourceUrl = resourceUrl.substr(1);
|
44 | }
|
45 |
|
46 | for (var assetPattern in SKIPPED_ASSETS) {
|
47 | if (resourceUrl.match(SKIPPED_ASSETS[assetPattern])) {
|
48 | next();
|
49 | return;
|
50 | }
|
51 | }
|
52 |
|
53 |
|
54 | if (fs.statSync(absolutePath).size > MAX_FILE_SIZE_BYTES) {
|
55 |
|
56 | console.warn('\n[percy][WARNING] Skipping large file: ', resourceUrl);
|
57 | return;
|
58 | }
|
59 |
|
60 |
|
61 |
|
62 | var content = fs.readFileSync(absolutePath);
|
63 |
|
64 | hashToResource[encodeURI(resourceUrl)] = content;
|
65 | next();
|
66 | }
|
67 | }
|
68 | };
|
69 | _walk2.default.walkSync(buildDir, walkOptions);
|
70 |
|
71 | return hashToResource;
|
72 | }
|
73 |
|
74 | function getStaticAssets() {
|
75 | var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
76 |
|
77 |
|
78 | var storybookStaticPath = _path2.default.resolve(options.buildDir);
|
79 | var storyHtml = fs.readFileSync(_path2.default.join(storybookStaticPath, 'iframe.html'), 'utf8');
|
80 |
|
81 |
|
82 | var storybookJavascriptPath = storyHtml.match(/<script src="(.*?static\/preview.*?)"><\/script>/)[1];
|
83 | var storyJavascript = fs.readFileSync(_path2.default.join(storybookStaticPath, storybookJavascriptPath), 'utf8');
|
84 |
|
85 |
|
86 | var assets = gatherBuildResources(options.buildDir);
|
87 | assets[storybookJavascriptPath] = storyJavascript;
|
88 |
|
89 | return { storyHtml: storyHtml, assets: assets, storybookJavascriptPath: storybookJavascriptPath };
|
90 | } |
\ | No newline at end of file |