UNPKG

3.13 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = getStaticAssets;
7
8var _path = require('path');
9
10var _path2 = _interopRequireDefault(_path);
11
12var _walk = require('walk');
13
14var _walk2 = _interopRequireDefault(_walk);
15
16function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
18var fs = require('fs');
19
20// Some build assets we never want to upload.
21var SKIPPED_ASSETS = ['index.html', 'iframe.html', 'favicon.ico', 'static/', /\.map$/, /\.log$/, /\.DS_Store$/];
22var MAX_FILE_SIZE_BYTES = 15728640; // 15MB.
23
24// Synchronously walk the build directory, read each file
25function gatherBuildResources(buildDir) {
26 var hashToResource = {};
27 var walkOptions = {
28 // Follow symlinks because many assets in the ember build directory are just symlinks.
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 // Windows support: transform filesystem backslashes into forward-slashes for the URL.
38 resourceUrl = resourceUrl.replace('\\', '/');
39 }
40
41 // Remove the leading /
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 // Skip large files.
54 if (fs.statSync(absolutePath).size > MAX_FILE_SIZE_BYTES) {
55 // eslint-disable-next-line no-console
56 console.warn('\n[percy][WARNING] Skipping large file: ', resourceUrl);
57 return;
58 }
59
60 // TODO(fotinakis): this is synchronous and potentially memory intensive, but we don't
61 // keep a reference to the content around so this should be garbage collected. Re-evaluate?
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
74function getStaticAssets() {
75 var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
76
77 // Load iframe.html that is used for every snapshot asset
78 var storybookStaticPath = _path2.default.resolve(options.buildDir);
79 var storyHtml = fs.readFileSync(_path2.default.join(storybookStaticPath, 'iframe.html'), 'utf8');
80
81 // Load the special static/preview.js that contains all stories
82 var storybookJavascriptPath = storyHtml.match(/<script src="(.*?static\/preview.*?)"><\/script>/)[1];
83 var storyJavascript = fs.readFileSync(_path2.default.join(storybookStaticPath, storybookJavascriptPath), 'utf8');
84
85 // Load other build assets
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