forked from bigcommerce/stencil-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbundle-validator.js
449 lines (391 loc) · 15 KB
/
bundle-validator.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
require('colors');
const os = require('os');
const _ = require('lodash');
const async = require('async');
const fs = require('fs');
const sizeOf = require('image-size');
const path = require('path');
const Validator = require('ajv');
const yamlValidator = require('js-yaml');
const { recursiveReadDir } = require('./utils/fsUtils');
const { getFrontmatterContent, interpolateThemeSettings } = require('./utils/frontmatter');
const ValidatorSchemaTranslations = require('./validator/schema-translations');
const privateThemeConfigValidationSchema = require('./schemas/privateThemeConfig.json');
const themeConfigValidationSchema = require('./schemas/themeConfig.json');
const themeValidationSchema = require('./schemas/themeSchema.json');
const ScssValidator = require('./ScssValidator');
const LangHelpersValidator = require('./lang/validator');
const VALID_IMAGE_TYPES = ['.jpg', '.jpeg', '.png', '.gif'];
const WIDTH_COMPOSED = 600;
const HEIGHT_COMPOSED = 760;
const WIDTH_MOBILE = 304;
const HEIGHT_MOBILE = 540;
const WIDTH_DESKTOP = 2048;
const HEIGHT_DESKTOP = 2600;
const MAX_SIZE_COMPOSED = 1024 * 1024 * 2; // 2MB
const MAX_SIZE_MOBILE = 1024 * 1024; // 1MB
const MAX_SIZE_DESKTOP = 1024 * 1024 * 5; // 5MB
class BundleValidator {
/**
* Run some validations to ensure that the platform will accept the theme
* @param themePath
* @param themeConfig
* @param isPrivate
* @constructor
*/
constructor(themePath, themeConfig, isPrivate) {
this.isPrivate = isPrivate;
this.themeConfig = themeConfig;
this.themePath = themePath;
this.objectsToValidate = ['head.scripts', 'footer.scripts'];
this.jsonSchemaValidatorOptions = { schemaId: 'auto', allErrors: true };
this.scssValidator = new ScssValidator(themePath, themeConfig);
this.langHelpersValidator = new LangHelpersValidator(themePath, themeConfig);
// Array of tasks used in async.series
this.validationTasks = [
this._validateThemeConfiguration.bind(this),
this._validateThemeSchema.bind(this),
this._validateSchemaTranslations.bind(this),
this._validateTemplatesFrontmatter.bind(this),
this._validateCssFiles.bind(this),
this._validateLangFiles.bind(this),
];
if (!this.isPrivate) {
this.validationTasks.push(this._validateMetaImages.bind(this));
}
}
/**
* Run all validation tasks in parallel
* @param callback
*/
validateTheme(callback) {
async.series(this.validationTasks, (error, result) => {
return callback(error, result);
});
}
/**
* Validates that required objects/properties exist in theme
* @param {array} assembledTemplates
* @param {function} callback
*/
validateObjects(assembledTemplates, callback) {
async.map(
assembledTemplates,
(template, cb) => {
const validated = [];
for (const templateString of Object.keys(template)) {
const matches = this.objectsToValidate.filter(
(element) =>
template[templateString].search(
new RegExp(`{+\\s*${element}\\s*}+`),
) !== -1,
);
validated.push(...matches);
}
cb(null, validated);
},
(err, validationRes) => {
if (err) {
callback(err);
return;
}
const results = _.difference(
this.objectsToValidate,
_.uniq(_.flatten(validationRes)),
);
if (results.length !== 0) {
callback(
new Error(`Missing required objects/properties: ${results.join('\n')}`),
);
return;
}
callback(null, !!results);
},
);
}
/**
* Wrapper to enable testability.
* @param imagePath
* @param callback
*/
sizeOf(imagePath, callback) {
return sizeOf(imagePath, callback);
}
/**
* If theme schema exists we need to validate it to make sure it passes all defined checks.
* @private
* @returns {boolean}
*/
async _validateThemeSchema() {
if (this.themeConfig.schemaExists()) {
const rawSchema = await this.themeConfig.getRawSchema();
this._validateJsonSchema('schema', themeValidationSchema, rawSchema);
}
return true;
}
/**
* Ensure theme configuration exists and passes the json schema file
* @private
* @returns {boolean}
*/
async _validateThemeConfiguration() {
if (!this.themeConfig.configExists()) {
const errMsg =
'You must have a '.red +
'config.json'.cyan +
' file in your top level theme directory.';
throw new Error(errMsg);
}
if (!this.isPrivate && !this.themeConfig.schemaExists()) {
console.log("Warning: Your theme is missing a 'schema.json' file.".orange);
}
// Validate against the theme registry config schema
const validationSchema = this.isPrivate
? privateThemeConfigValidationSchema
: themeConfigValidationSchema;
const rawConfig = await this.themeConfig.getRawConfig();
return this._validateJsonSchema('config', validationSchema, rawConfig);
}
/**
* @private
* @param type
* @param schema
* @param data
* @returns {boolean}
*/
_validateJsonSchema(type, schema, data) {
const validator = new Validator(this.jsonSchemaValidatorOptions);
validator.validate(schema, data);
if (validator.errors && validator.errors.length > 0) {
let errorMessage = `Your theme's ${type}.json has errors:`;
for (const error of validator.errors) {
errorMessage += `${os.EOL + type + error.dataPath} ${error.message}`;
}
throw new Error(errorMessage.red);
}
return true;
}
/**
* Ensure that schema translations exists and there are no missing or unused keys.
* @private
* @returns {boolean}
*/
async _validateSchemaTranslations() {
const validatorSchemaTranslations = new ValidatorSchemaTranslations();
const validator = new Validator(this.jsonSchemaValidatorOptions);
if (this.themeConfig.schemaExists()) {
validatorSchemaTranslations.setSchema(await this.themeConfig.getRawSchema());
}
if (this.themeConfig.schemaTranslationsExists()) {
try {
const rawSchemaTranslations = await this.themeConfig.getRawSchemaTranslations();
validatorSchemaTranslations.setTranslations(rawSchemaTranslations);
} catch (e) {
throw new Error('Corrupted schemaTranslations.json file'.red);
}
} else if (validatorSchemaTranslations.getSchemaKeys().length) {
throw new Error('Missed schemaTranslations.json file'.red);
}
const missedKeys = validatorSchemaTranslations.findMissedKeys();
const unusedKeys = validatorSchemaTranslations.findUnusedKeys();
validator.validate(
validatorSchemaTranslations.getValidationSchema(),
validatorSchemaTranslations.getTranslations(),
);
if (
(validator.errors && validator.errors.length) ||
missedKeys.length ||
unusedKeys.length
) {
let errorMessage = "Your theme's schemaTranslations.json has errors:";
missedKeys.forEach((key) => {
errorMessage += `\r\nmissing translation key "${key}"`;
});
unusedKeys.forEach((key) => {
errorMessage += `\r\nunused translation key "${key}"`;
});
if (validator.errors && validator.errors.length) {
validator.errors.forEach((error) => {
errorMessage += `\r\nschemaTranslations${error.message}`;
});
}
throw new Error(errorMessage.red);
}
return true;
}
/**
* Validates images for marketplace themes
* @private
* @returns {boolean[]}
*/
async _validateMetaImages() {
const { meta, variations } = await this.themeConfig.getConfig();
const composedImagePath = path.resolve(this.themePath, 'meta', meta.composed_image);
const imageTasks = [];
if (!this._isValidImageType(composedImagePath)) {
throw new Error(
'Invalid file type for "meta.composed_image".'.red +
`\r\nValid types (${VALID_IMAGE_TYPES.join(', ')})`.red,
);
}
if (!fs.existsSync(composedImagePath)) {
throw new Error(
'The path you specified for your "meta.composed_image" does not exist.'.red,
);
}
imageTasks.push((cb) =>
this._validateImage(composedImagePath, WIDTH_COMPOSED, HEIGHT_COMPOSED, cb),
);
for (const variation of variations) {
const id = variation.id.blue;
const desktopScreenshotPath = path.resolve(
this.themePath,
'meta',
variation.meta.desktop_screenshot,
);
if (!this._isValidImageType(desktopScreenshotPath)) {
throw new Error(
`Invalid file type for ${id} variation's "desktop_screenshot".`.red +
`\r\nValid types (${VALID_IMAGE_TYPES.join(', ')})`.red,
);
}
if (!fs.existsSync(desktopScreenshotPath)) {
throw new Error(
`The path you specified for the ${id} variation's "desktop_screenshot" does not exist.`.red,
);
}
imageTasks.push((cb) =>
this._validateImage(desktopScreenshotPath, WIDTH_DESKTOP, HEIGHT_DESKTOP, cb),
);
const mobileScreenshotPath = path.resolve(
this.themePath,
'meta',
variation.meta.mobile_screenshot,
);
if (!this._isValidImageType(mobileScreenshotPath)) {
throw new Error(
`Invalid file type for ${id} variation's "mobile_screenshot".`.red +
`\r\nValid types (${VALID_IMAGE_TYPES.join(', ')})`.red,
);
}
if (!fs.existsSync(mobileScreenshotPath)) {
throw new Error(
`The path you specified for the ${id} variation's "mobile_screenshot" does not exist.`.red,
);
}
imageTasks.push((cb) =>
this._validateImage(mobileScreenshotPath, WIDTH_MOBILE, HEIGHT_MOBILE, cb),
);
}
return async.parallel(imageTasks);
}
/**
* @private
* @param imagePath
* @returns {boolean}
*/
_isValidImageType(imagePath) {
const ext = path.extname(imagePath);
return VALID_IMAGE_TYPES.includes(ext);
}
/**
* @private
* @param imagePath
* @param width
* @param height
* @param cb
*/
_validateImage(imagePath, width, height, cb) {
this.sizeOf(imagePath, (err, dimensions) => {
if (err) {
cb(err);
return;
}
let failureMessage = '';
const imageHeight = dimensions.height;
const imageWidth = dimensions.width;
const { size } = fs.statSync(imagePath);
if (width === WIDTH_DESKTOP && height === HEIGHT_DESKTOP && size > MAX_SIZE_DESKTOP) {
failureMessage =
`Image of size ${size} bytes at path (${imagePath}) ` +
`is greater than allowed size ${MAX_SIZE_DESKTOP}\n`;
} else if (
width === WIDTH_COMPOSED &&
height === HEIGHT_COMPOSED &&
size > MAX_SIZE_COMPOSED
) {
failureMessage =
`Image of size ${size} bytes at path (${imagePath}) ` +
`is greater than allowed size ${MAX_SIZE_COMPOSED}\n`;
} else if (
width === WIDTH_MOBILE &&
height === HEIGHT_MOBILE &&
size > MAX_SIZE_MOBILE
) {
failureMessage =
`Image of size ${size} bytes at path (${imagePath}) ` +
`is greater than allowed size ${MAX_SIZE_MOBILE}\n`;
}
if (imageWidth !== width || imageHeight !== height) {
failureMessage +=
`Image at (${imagePath}) has incorrect dimensions (${imageWidth}x${imageHeight}) should be` +
`(${width}x${height})`;
cb(new Error(failureMessage));
return;
}
cb(null, true);
});
}
async _validateTemplatesFrontmatter() {
const config = await this.themeConfig.getRawConfig();
const filePaths = await recursiveReadDir(path.join(this.themePath, 'templates'), [
'!*.html',
]);
for await (const filePath of filePaths) {
const fileContent = await fs.promises.readFile(filePath, { encoding: 'utf-8' });
const frontmatter = getFrontmatterContent(fileContent);
if (frontmatter) {
const yaml = interpolateThemeSettings(frontmatter, config.settings);
try {
const result = yamlValidator.loadAll(yaml);
this.validateTrailingSymbols(result[0]);
} catch (e) {
throw new Error(
`Error: ${e.message}, while parsing frontmatter at "${filePath}".`.red,
);
}
}
}
return true;
}
async _validateLangFiles() {
await this.langHelpersValidator.run();
}
async _validateCssFiles() {
await this.scssValidator.run();
}
validateTrailingSymbols(data) {
if (_.isObject(data)) {
return _.every(data, (value) => this.validateTrailingSymbols(value));
}
if (_.isArray(data)) {
return data.every((row) => this.validateTrailingSymbols(row));
}
return data ? this.hasFrontmatterValidValue(data) : true;
}
hasFrontmatterValidValue(value) {
if (this.hasUnallowedTrailingSymbol(value)) {
throw new Error(`Found unallowed trailing symbol in: "${value}"`);
}
return true;
}
getUnallowedTrailingSymbols() {
return [',', ';'];
}
hasUnallowedTrailingSymbol(value) {
const symbols = this.getUnallowedTrailingSymbols();
const trailingSymbol = value.toString().trim().slice(-1);
return symbols.includes(trailingSymbol);
}
}
module.exports = BundleValidator;