@bigcommerce/stencil-cli 7.2.3 → 7.3.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.
@@ -15,6 +15,7 @@ const privateThemeConfigValidationSchema = require('./schemas/privateThemeConfig
15
15
  const themeConfigValidationSchema = require('./schemas/themeConfig.json');
16
16
  const themeValidationSchema = require('./schemas/themeSchema.json');
17
17
  const ScssValidator = require('./ScssValidator');
18
+ const LangHelpersValidator = require('./lang/validator');
18
19
 
19
20
  const VALID_IMAGE_TYPES = ['.jpg', '.jpeg', '.png', '.gif'];
20
21
  const WIDTH_COMPOSED = 600;
@@ -42,6 +43,7 @@ class BundleValidator {
42
43
  this.objectsToValidate = ['head.scripts', 'footer.scripts'];
43
44
  this.jsonSchemaValidatorOptions = { schemaId: 'auto', allErrors: true };
44
45
  this.scssValidator = new ScssValidator(themePath, themeConfig);
46
+ this.langHelpersValidator = new LangHelpersValidator(themePath, themeConfig);
45
47
 
46
48
  // Array of tasks used in async.series
47
49
  this.validationTasks = [
@@ -50,6 +52,7 @@ class BundleValidator {
50
52
  this._validateSchemaTranslations.bind(this),
51
53
  this._validateTemplatesFrontmatter.bind(this),
52
54
  this._validateCssFiles.bind(this),
55
+ this._validateLangFiles.bind(this),
53
56
  ];
54
57
 
55
58
  if (!this.isPrivate) {
@@ -405,6 +408,10 @@ class BundleValidator {
405
408
  return true;
406
409
  }
407
410
 
411
+ async _validateLangFiles() {
412
+ await this.langHelpersValidator.run();
413
+ }
414
+
408
415
  async _validateCssFiles() {
409
416
  await this.scssValidator.run();
410
417
  }
@@ -0,0 +1,113 @@
1
+ require('colors');
2
+ const fs = require('fs');
3
+ const path = require('path');
4
+ const { recursiveReadDir } = require('../utils/fsUtils');
5
+
6
+ const LANG_HELPER_REGEXP = /{{\s*lang\s*(?:'|")((?:\w*(?:-\w*)*(\.\w*(?:-\w*)*)*)+)/gim;
7
+
8
+ class LangpathsValidator {
9
+ /**
10
+ *
11
+ * @param {String} themePath
12
+ */
13
+ constructor(themePath) {
14
+ this.themePath = themePath;
15
+ }
16
+
17
+ async run(defaultLang = null) {
18
+ const templatesPath = path.join(this.themePath, 'templates');
19
+ const paths = await this.getLangHelpersPaths(templatesPath);
20
+ const dedupePaths = [...new Set(paths)];
21
+ const langFiles = await this.getLangFilesContent(defaultLang);
22
+ const errors = this.validate(dedupePaths, langFiles);
23
+ this.printErrors(errors);
24
+ return errors;
25
+ }
26
+
27
+ printErrors(errors) {
28
+ if (errors.length > 0) {
29
+ console.log(
30
+ 'Warning: Your theme has some missing translations used in the theme:'.yellow,
31
+ );
32
+ console.log(errors.join('\n').yellow);
33
+ }
34
+ }
35
+
36
+ searchLangPaths(fileContent, langPath) {
37
+ const keys = langPath.split('.');
38
+ let value = fileContent;
39
+
40
+ for (const key of keys) {
41
+ // eslint-disable-next-line no-prototype-builtins
42
+ if (value && value.hasOwnProperty(key)) {
43
+ value = value[key];
44
+ } else {
45
+ return false;
46
+ }
47
+ }
48
+
49
+ return value;
50
+ }
51
+
52
+ validate(paths, langFiles) {
53
+ const errors = [
54
+ ...this.checkLangFiles(langFiles),
55
+ ...this.checkForMissingTranslations(paths, langFiles),
56
+ ];
57
+ return errors;
58
+ }
59
+
60
+ checkForMissingTranslations(paths, langFiles) {
61
+ const errors = [];
62
+ for (const langPath of paths) {
63
+ // eslint-disable-next-line no-restricted-syntax,guard-for-in
64
+ for (const langFile in langFiles) {
65
+ const translation = this.searchLangPaths(langFiles[langFile], langPath);
66
+ if (!translation) {
67
+ errors.push(`Missing translation for ${langPath} in ${langFile}`);
68
+ }
69
+ }
70
+ }
71
+ return errors;
72
+ }
73
+
74
+ checkLangFiles(files) {
75
+ if (files.length === 0) {
76
+ return ['No lang files found in your theme'];
77
+ }
78
+ return [];
79
+ }
80
+
81
+ async getLangHelpersPaths(templatesPath) {
82
+ const files = await recursiveReadDir(templatesPath);
83
+ const paths = [];
84
+ for await (const file of files) {
85
+ const content = await fs.promises.readFile(file, { encoding: 'utf-8' });
86
+ const result = content.matchAll(LANG_HELPER_REGEXP);
87
+ const arr = [...result];
88
+ if (arr.length > 0) {
89
+ const langPath = arr[0][1];
90
+ paths.push(langPath);
91
+ }
92
+ }
93
+ return paths;
94
+ }
95
+
96
+ async getLangFilesContent(defaultLang = null) {
97
+ const filesContent = {};
98
+ const langPath = path.join(this.themePath, 'lang');
99
+ let files = await recursiveReadDir(langPath);
100
+
101
+ if (defaultLang) {
102
+ files = files.filter((file) => file.includes(defaultLang));
103
+ }
104
+
105
+ for await (const file of files) {
106
+ const content = await fs.promises.readFile(file, { encoding: 'utf-8' });
107
+ filesContent[file] = JSON.parse(content);
108
+ }
109
+ return filesContent;
110
+ }
111
+ }
112
+
113
+ module.exports = LangpathsValidator;
@@ -16,6 +16,7 @@ const cliCommonModule = require('./cliCommon');
16
16
  const themeApiClientModule = require('./theme-api-client');
17
17
  const storeSettingsApiClientModule = require('./store-settings-api-client');
18
18
  const LangHelper = require('./lang-helper');
19
+ const LangValidator = require('./lang/validator');
19
20
 
20
21
  class StencilStart {
21
22
  constructor({
@@ -32,6 +33,7 @@ class StencilStart {
32
33
  CyclesDetector = Cycles,
33
34
  stencilPushUtils = stencilPushUtilsModule,
34
35
  logger = console,
36
+ langValidator = new LangValidator(THEME_PATH),
35
37
  } = {}) {
36
38
  this._browserSync = browserSync;
37
39
  this._themeApiClient = themeApiClient;
@@ -46,6 +48,7 @@ class StencilStart {
46
48
  this._CyclesDetector = CyclesDetector;
47
49
  this._stencilPushUtils = stencilPushUtils;
48
50
  this._logger = logger;
51
+ this._langValidator = langValidator;
49
52
  }
50
53
 
51
54
  async run(cliOptions) {
@@ -289,6 +292,7 @@ class StencilStart {
289
292
  } else {
290
293
  try {
291
294
  await this._langHelper.checkLangKeysPresence(filesPaths, defaultShopperLanguage);
295
+ await this._langValidator.run(defaultShopperLanguage);
292
296
  } catch (e) {
293
297
  this._logger.error(e);
294
298
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bigcommerce/stencil-cli",
3
- "version": "7.2.3",
3
+ "version": "7.3.0",
4
4
  "description": "CLI tool to run BigCommerce Stores locally for theme development.",
5
5
  "main": "index.js",
6
6
  "engines": {