drupal-image-style-generator 0.1.2 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +30 -0
- package/index.js +63 -31
- package/package.json +1 -1
- package/src/helpers.js +14 -0
package/README.md
CHANGED
|
@@ -22,3 +22,33 @@ col_8:
|
|
|
22
22
|
width: 545
|
|
23
23
|
```
|
|
24
24
|
|
|
25
|
+
## Usage
|
|
26
|
+
|
|
27
|
+
Can be used in your script (grunt or gulp):
|
|
28
|
+
|
|
29
|
+
```js
|
|
30
|
+
const gen = require('drupal-image-style-generator');
|
|
31
|
+
const themeName = 'myTheme';
|
|
32
|
+
const themePath = './web/themes/custom/' + themeName;
|
|
33
|
+
|
|
34
|
+
gen({
|
|
35
|
+
themePath: themePath,
|
|
36
|
+
themeName: themeName,
|
|
37
|
+
syncFolder: './config/sync',
|
|
38
|
+
gridSize: 100,
|
|
39
|
+
});
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
## Options
|
|
44
|
+
|
|
45
|
+
**themePath**: Relative path to the theme - needs to contain the `theme.breakpoints.yml`
|
|
46
|
+
|
|
47
|
+
**themeName**: the name of the theme
|
|
48
|
+
|
|
49
|
+
**syncFolder**: Relative path to the sync folder
|
|
50
|
+
|
|
51
|
+
**gridSize**: (Default: 0) Number to which grid the images are rounded up. In the worst case, the image is gridSize - 1 px to big for the breakpoint, but a higher grid will result in lower individual image styles.
|
|
52
|
+
|
|
53
|
+
**convertTo**: (Default: null) enables the image_convert style - allowed values are `png`, `jpg`, `jpeg`, `jpe`, `gif`, `webp`
|
|
54
|
+
|
package/index.js
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const
|
|
1
|
+
const REQUIRED_OPTIONS = ['themePath', 'themeName', 'syncFolder'];
|
|
2
|
+
const helpers = require('./src/helpers');
|
|
3
|
+
const fs = require("fs");
|
|
4
|
+
const yaml = require("js-yaml");
|
|
5
|
+
const log = require("fancy-log");
|
|
6
|
+
const {v4} = require("uuid");
|
|
4
7
|
|
|
5
8
|
/**
|
|
6
|
-
* @param {{themePath: string, themeName: string, syncFolder: string, gridSize?: number}} options
|
|
9
|
+
* @param {{themePath: string, themeName: string, syncFolder: string, gridSize?: number, convertTo?: string}} options
|
|
7
10
|
*/
|
|
8
11
|
module.exports = function (options) {
|
|
9
12
|
|
|
@@ -13,14 +16,16 @@ module.exports = function (options) {
|
|
|
13
16
|
}
|
|
14
17
|
})
|
|
15
18
|
|
|
19
|
+
const convertTo = options.convertTo
|
|
16
20
|
const themePath = options.themePath;
|
|
17
21
|
const themeName = options.themeName;
|
|
18
22
|
const syncFolder = options.syncFolder;
|
|
19
23
|
const IMAGE_STYLE_GRID_SIZE = options.gridSize || 0; // this means, every style will be rounded up to the next divisible number of this
|
|
20
24
|
|
|
21
25
|
const { v4 } = require("uuid");
|
|
22
|
-
const yaml
|
|
23
|
-
const fs
|
|
26
|
+
const yaml = require('js-yaml');
|
|
27
|
+
const fs = require('fs');
|
|
28
|
+
const log = require('fancy-log');
|
|
24
29
|
const breakpointsFile = themePath + '/' + themeName + '.breakpoints.yml';
|
|
25
30
|
const imageStyles = {};
|
|
26
31
|
const usedStyleConfigs = {};
|
|
@@ -86,6 +91,8 @@ module.exports = function (options) {
|
|
|
86
91
|
let concreteStyleId;
|
|
87
92
|
let styleFileName;
|
|
88
93
|
let styleFilePath;
|
|
94
|
+
let styleYml;
|
|
95
|
+
let hasChanges = false;
|
|
89
96
|
|
|
90
97
|
if (aspectRatio) {
|
|
91
98
|
styleHeight = imageStyles[styleId].height ? imageStyles[styleId].height * multiplyNum : Math.round(styleWidth * aspectRatio);
|
|
@@ -98,7 +105,7 @@ module.exports = function (options) {
|
|
|
98
105
|
|
|
99
106
|
usedStyleConfigs[styleFileName] = true;
|
|
100
107
|
|
|
101
|
-
|
|
108
|
+
styleYml = fs.existsSync(styleFilePath) ?
|
|
102
109
|
yaml.load(fs.readFileSync(styleFilePath, 'utf8')) :
|
|
103
110
|
{
|
|
104
111
|
uuid: uniqueId,
|
|
@@ -113,14 +120,29 @@ module.exports = function (options) {
|
|
|
113
120
|
}
|
|
114
121
|
;
|
|
115
122
|
|
|
116
|
-
const
|
|
117
|
-
|
|
118
|
-
});
|
|
123
|
+
const scaleEffect = helpers.getEffectByTypeId('focal_point_scale_and_crop', styleYml.effects);
|
|
124
|
+
const convertEffect = helpers.getEffectByTypeId('image_convert', styleYml.effects);
|
|
119
125
|
|
|
120
|
-
|
|
121
|
-
|
|
126
|
+
const hasChanges = !scaleEffect || scaleEffect.data.width !== styleWidth || scaleEffect.data.height !== styleHeight ||
|
|
127
|
+
(convertTo && (!convertEffect || (convertEffect && convertEffect.data.extension !== convertTo))) || (!convertTo && convertEffect)
|
|
128
|
+
;
|
|
122
129
|
|
|
130
|
+
if (hasChanges) {
|
|
123
131
|
styleYml.effects = {};
|
|
132
|
+
|
|
133
|
+
if (convertTo) {
|
|
134
|
+
const convertEffectId = v4();
|
|
135
|
+
styleYml.effects[convertEffectId] = {
|
|
136
|
+
uuid: convertEffectId,
|
|
137
|
+
id: 'image_convert',
|
|
138
|
+
weight: -1,
|
|
139
|
+
data: {
|
|
140
|
+
extension: convertTo
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const effectId = v4();
|
|
124
146
|
styleYml.effects[effectId] = {
|
|
125
147
|
uuid: effectId,
|
|
126
148
|
id: 'focal_point_scale_and_crop',
|
|
@@ -131,14 +153,7 @@ module.exports = function (options) {
|
|
|
131
153
|
crop_type: 'focal_point'
|
|
132
154
|
}
|
|
133
155
|
};
|
|
134
|
-
|
|
135
|
-
// write the file
|
|
136
|
-
fs.writeFileSync(styleFilePath, yaml.dump(styleYml));
|
|
137
|
-
log('Created ' + styleFilePath);
|
|
138
|
-
} else {
|
|
139
|
-
log('skipping ' + styleFileName + ' - it already exists and there are no changes.');
|
|
140
156
|
}
|
|
141
|
-
|
|
142
157
|
} else {
|
|
143
158
|
|
|
144
159
|
// generate the filename
|
|
@@ -149,7 +164,7 @@ module.exports = function (options) {
|
|
|
149
164
|
|
|
150
165
|
usedStyleConfigs[styleFileName] = true;
|
|
151
166
|
|
|
152
|
-
|
|
167
|
+
styleYml = fs.existsSync(styleFilePath) ?
|
|
153
168
|
yaml.load(fs.readFileSync(styleFilePath, 'utf8')) :
|
|
154
169
|
{
|
|
155
170
|
uuid: uniqueId,
|
|
@@ -162,14 +177,29 @@ module.exports = function (options) {
|
|
|
162
177
|
}
|
|
163
178
|
;
|
|
164
179
|
|
|
165
|
-
const
|
|
166
|
-
|
|
167
|
-
});
|
|
180
|
+
const scaleEffect = helpers.getEffectByTypeId('image_scale', styleYml.effects);
|
|
181
|
+
const convertEffect = helpers.getEffectByTypeId('image_convert', styleYml.effects);
|
|
168
182
|
|
|
169
|
-
|
|
170
|
-
|
|
183
|
+
const hasChanges = !scaleEffect || scaleEffect.data.width !== styleWidth ||
|
|
184
|
+
(convertTo && (!convertEffect || (convertEffect && convertEffect.data.extension !== convertTo))) || (!convertTo && convertEffect)
|
|
185
|
+
;
|
|
171
186
|
|
|
187
|
+
if (hasChanges) {
|
|
172
188
|
styleYml.effects = {};
|
|
189
|
+
|
|
190
|
+
if (convertTo) {
|
|
191
|
+
const convertEffectId = v4();
|
|
192
|
+
styleYml.effects[convertEffectId] = {
|
|
193
|
+
uuid: convertEffectId,
|
|
194
|
+
id: 'image_convert',
|
|
195
|
+
weight: -1,
|
|
196
|
+
data: {
|
|
197
|
+
extension: convertTo
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
const effectId = v4();
|
|
173
203
|
styleYml.effects[effectId] = {
|
|
174
204
|
uuid: effectId,
|
|
175
205
|
id: 'image_scale',
|
|
@@ -180,16 +210,18 @@ module.exports = function (options) {
|
|
|
180
210
|
upscale: false
|
|
181
211
|
}
|
|
182
212
|
};
|
|
183
|
-
|
|
184
|
-
// write the file
|
|
185
|
-
fs.writeFileSync(styleFilePath, yaml.dump(styleYml));
|
|
186
|
-
log('Created ' + styleFilePath);
|
|
187
|
-
} else {
|
|
188
|
-
log('skipping ' + styleFileName + ' - it already exists and there are no changes.');
|
|
189
213
|
}
|
|
190
214
|
|
|
191
215
|
}
|
|
192
216
|
|
|
217
|
+
if (hasChanges) {
|
|
218
|
+
// write the file
|
|
219
|
+
fs.writeFileSync(styleFilePath, yaml.dump(styleYml));
|
|
220
|
+
log('Created ' + styleFilePath);
|
|
221
|
+
} else {
|
|
222
|
+
log('skipping ' + styleFileName + ' - it already exists and there are no changes.');
|
|
223
|
+
}
|
|
224
|
+
|
|
193
225
|
imageStyles[styleId].image_style_mappings.push({
|
|
194
226
|
breakpoint_id: bpName,
|
|
195
227
|
multiplier: multiplier,
|
package/package.json
CHANGED
package/src/helpers.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
*
|
|
5
|
+
* @param {string} effectTypeId
|
|
6
|
+
* @param {Record<string, {uuid: string, id: string, weight: number, data: {}}>} [effects]
|
|
7
|
+
*/
|
|
8
|
+
getEffectByTypeId: function (effectTypeId, effects) {
|
|
9
|
+
return effects ? Object.values(effects).find((effect) => {
|
|
10
|
+
return effect.id === effectTypeId
|
|
11
|
+
}) : null;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
}
|