forked from storybookjs/ember-cli-storybook
-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.js
146 lines (124 loc) Β· 3.86 KB
/
util.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
const cheerio = require('cheerio');
const lookupTable = {
meta: [{
selector: 'meta[name*="/config/environment"]',
attributes: ['name', 'content']
}, {
selector: 'meta[name*="/config/asset-manifest"]',
attributes: ['name', 'content']
}, {
selector: 'meta[id]',
attributes: ['name', 'content', 'id']
}],
link: [{
selector: 'link',
attributes: ['rel', 'href']
}],
script: [{
selector: 'script',
attributes: ['src']
}]
};
function getDocumentValues($, selector, attributes=[], ignoreRegexs=[]) {
let $tags = $(selector);
let config = [];
$tags.each(function() {
var $tag = $(this);
var data = attributes.reduce(function(data, attribute) {
const value = $tag.attr(attribute);
let ignored = false;
if(value && !ignoreRegexs.find((regex) => {
return regex.exec(value)
})) {
data[attribute] = value
} else {
ignored = true;
}
return ignored ? {} : data;
}, {})
if(Object.keys(data).length > 0) {
config.push(data);
}
});
return config;
}
function removeRootURL(config) {
// extract and parse the application config
let appConfig = JSON.parse(decodeURIComponent(config.meta[0].content));
let { rootURL } = appConfig;
if (!rootURL) return config;
config.script = config.script.map(s => {
s.src = s.src.replace(rootURL, './');
return s;
});
config.link = config.link.map(l => {
l.href = l.href.replace(rootURL, './');
return l;
});
return config;
}
function parse(data, ignoreTestFiles) {
const ignoreRegexs = ignoreTestFiles ? [/assets\/test/] : []
var $ = cheerio.load(data);
var json = {};
for(var prop in lookupTable) {
var value = lookupTable[prop];
for (var selector of value) {
const tagsFound = getDocumentValues($, selector.selector, selector.attributes, ignoreRegexs);
// if we have multiple selectors for a certain block
// we need to make sure we don't just set the array every time
if(json[prop]) {
json[prop] = json[prop].concat(tagsFound)
} else {
json[prop] = tagsFound;
}
}
}
return removeRootURL(json);
}
function objectToHTMLAttributes(obj) {
return Object.keys(obj).map((key) => {
return `${key}="${obj[key]}"`
}).join(' ')
}
function generatePreviewHead(parsedConfig) {
const doc = [];
for(const key of Object.keys(parsedConfig)) {
for(const value of parsedConfig[key]) {
if(key == 'script') {
if(value.src.indexOf('ember-cli-live-reload.js') > -1) {
doc.push(`<script>
(function() {
var srcUrl = null;
var host = location.hostname || 'localhost';
var defaultPort = location.protocol === 'https:' ? 443 : 80;
var port = ${process.env.EMBER_CLI_INJECT_LIVE_RELOAD_PORT};
var path = '';
var prefixURL = '';
var src = srcUrl || prefixURL + '/_lr/livereload.js?port=' + port + '&host=' + host + path;
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = location.protocol + '//' + host + ':${process.env.EMBER_CLI_INJECT_LIVE_RELOAD_PORT}' + src;
document.getElementsByTagName('head')[0].appendChild(script);
}());
</script>`);
continue;
}
if(value.src.indexOf('assets/vendor.js') > -1) {
// make sure we push this before vendor gets loaded to ensure the application does not bind to the window
doc.push('<script>runningTests = true;</script>');
}
doc.push(`<${key} ${objectToHTMLAttributes(value)}></${key}>`);
} else {
doc.push(`<${key} ${objectToHTMLAttributes(value)} />`);
}
}
}
return doc.join('\n')
}
module.exports = {
getDocumentValues,
parse,
objectToHTMLAttributes,
generatePreviewHead,
};