MCPcopy Create free account
hub / github.com/bigcommerce/stencil-cli / _validateImage

Method _validateImage

lib/bundle-validator.js:313–353  ·  view source on GitHub ↗

* @private * @param imagePath * @param width * @param height * @param cb

(imagePath, width, height, cb)

Source from the content-addressed store, hash-verified

311 * @param cb
312 */
313 _validateImage(imagePath, width, height, cb) {
314 this.sizeOf(imagePath, (err, dimensions) => {
315 if (err) {
316 cb(err);
317 return;
318 }
319 let failureMessage = '';
320 const imageHeight = dimensions.height;
321 const imageWidth = dimensions.width;
322 const { size } = fs.statSync(imagePath);
323 if (width === WIDTH_DESKTOP && height === HEIGHT_DESKTOP && size > MAX_SIZE_DESKTOP) {
324 failureMessage =
325 `Image of size ${size} bytes at path (${imagePath}) ` +
326 `is greater than allowed size ${MAX_SIZE_DESKTOP}\n`;
327 } else if (
328 width === WIDTH_COMPOSED &&
329 height === HEIGHT_COMPOSED &&
330 size > MAX_SIZE_COMPOSED
331 ) {
332 failureMessage =
333 `Image of size ${size} bytes at path (${imagePath}) ` +
334 `is greater than allowed size ${MAX_SIZE_COMPOSED}\n`;
335 } else if (
336 width === WIDTH_MOBILE &&
337 height === HEIGHT_MOBILE &&
338 size > MAX_SIZE_MOBILE
339 ) {
340 failureMessage =
341 `Image of size ${size} bytes at path (${imagePath}) ` +
342 `is greater than allowed size ${MAX_SIZE_MOBILE}\n`;
343 }
344 if (imageWidth !== width || imageHeight !== height) {
345 failureMessage +=
346 `Image at (${imagePath}) has incorrect dimensions (${imageWidth}x${imageHeight}) should be` +
347 `(${width}x${height})`;
348 cb(new Error(failureMessage));
349 return;
350 }
351 cb(null, true);
352 });
353 }
354
355 async _validateTemplatesFrontmatter() {
356 const config = await this.themeConfig.getRawConfig();

Callers 1

_validateMetaImagesMethod · 0.95

Calls 2

sizeOfMethod · 0.95
cbFunction · 0.85

Tested by

no test coverage detected