-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
124 lines (104 loc) · 2.77 KB
/
index.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
const fs = require('fs');
module.exports = function(customOptions) {
var defaultOptions = {
base: 'src/twig/components',
packageJson: require('./package.json'),
fileName: "fs-component-image-crops",
dest: 'static-html/templates'
};
var options = customOptions ? Object.assign(customOptions, defaultOptions) : defaultOptions;
var cropsHTML = '';
var resultsHTML = '';
fs.readdirSync(options.base).forEach(file => {
var content = fs.readFileSync(options.base + '/' + file, 'utf8');
var sizes = content.match(/(img\.[a-z]*\.[a-z]*)/g);
var uniqueSizes = [...new Set(sizes)];
cropsHTML +=
`<div class="crops_item">
<h2 class="crops_item_title">${ file.replace('.twig', '') }</h2>
<ul class="crops_item_list">`;
for (var x = 0; x < uniqueSizes.length; x++) {
for (var crop in options.packageJson.img) {
for (var size in options.packageJson.img[crop]) {
if ('img.' + crop + '.' + size == uniqueSizes[x]) {
cropsHTML +=
`<li class="crop">
<span class="crop-size">${ options.packageJson.img[crop][size].width }x${ options.packageJson.img[crop][size].height }</span>
<span class="crop-name">${ crop }.${ size }</span>
</li>`;
}
}
}
}
cropsHTML +=
`</ul>
</div>`;
});
resultsHTML =
`<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<title>Component Image Crops</title>
<style>
html {
height: 100%;
font-family: "Neue Haas Grotesk Text Std", Helvetica, Arial, sans-serif;
overflow-y: scroll;
}
body {
max-width: 780px;
margin: 0 auto;
padding: 40px 20px;
}
.crops_item {
border: 2px solid #eee;
margin-bottom: 40px;
}
.crops_item_title {
border-bottom: 1px solid #000;
margin: 0;
padding: 21px 20px 20px;
}
.crops_item_list {
list-style: none;
margin: 0;
padding: 0;
}
.crop {
padding: 11px 40px;
}
.crop:nth-of-type(even) {
background: #eee;
}
.crop-size {}
.crop-size:after {
color: #999;
content: "•";
display: inline-block;
margin: 0 6px 0 10px;
vertical-align: middle;
}
.crop-name {}
</style>
</head>
<body>
<header class="crops_header">
<h1 class="crops_title">Component Image Crops</h1>
</header>
<main class="crops_main">
<div class="crops_items">
${ cropsHTML }
</div>
</main>
</body>
</html>`;
fs.mkdir(options.dest, {
recursive: true
}, (err) => {
if (err) throw err;
fs.writeFile(options.dest + '/' + options.fileName + '.html', resultsHTML, function(err) {
if (err) throw err;
});
});
}