From 11b0294736ab054a81b9fed261bfe68e9de42c35 Mon Sep 17 00:00:00 2001 From: Anthony Pizzurro Date: Fri, 6 Aug 2021 14:45:40 -0400 Subject: [PATCH 1/7] add template utils for vscode --- packages/cli-lib/lib/constants.js | 44 +++++++++++++++++++++++++++++++ packages/cli-lib/templates.js | 25 ++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/packages/cli-lib/lib/constants.js b/packages/cli-lib/lib/constants.js index a03018fb8..a0941215c 100644 --- a/packages/cli-lib/lib/constants.js +++ b/packages/cli-lib/lib/constants.js @@ -118,6 +118,49 @@ const PROJECT_TEMPLATE_TYPES = { }, }; +const TEMPLATE_TYPE_ID = { + unmapped: 0, + email_base_template: 1, + email: 2, + landing_page_base_template: 3, + landing_page: 4, + blog_base: 5, + blog: 6, + blog_listing: 42, + site_page: 8, + blog_listing_context: 9, + blog_post_context: 10, + error_page: 11, + subscription_preferences: 12, + unsubscribe_confirmation: 13, + unsubscribe_simple: 14, + optin_email: 15, + optin_followup_email: 16, + optin_confirmation_page: 17, + global_group: 18, + password_prompt_page: 19, + resubscribe_email: 20, + unsubscribe_confirmation_email: 21, + resubscribe_confirmation_email: 22, + custom_module: 23, + css: 24, + js: 25, + search_results: 27, + membership_login: 29, + membership_register: 30, + membership_reset: 31, + membership_reset_request: 32, + drag_drop_email: 34, + knowledge_article: 35, + membership_email: 36, + section: 37, + global_content_partial: 38, + simple_landing_page_template: 39, + proposal: 40, + blog_post: 41, + quote: 43, +}; + module.exports = { ConfigFlags, Mode, @@ -144,4 +187,5 @@ module.exports = { POLLING_DELAY, PROJECT_TEMPLATE_TYPES, SCOPE_GROUPS, + TEMPLATE_TYPE_ID, }; diff --git a/packages/cli-lib/templates.js b/packages/cli-lib/templates.js index aa1e19ba7..345a59529 100644 --- a/packages/cli-lib/templates.js +++ b/packages/cli-lib/templates.js @@ -3,6 +3,10 @@ const { logger } = require('./logger'); // Matches the .html file extension, excluding module.html const TEMPLATE_EXTENSION_REGEX = new RegExp(/(?/; // Matches an annotation value, ending at space, newline, or end of string @@ -27,6 +31,12 @@ const getFileAnnotations = filePath => { } }; +const getAnnotationsFromSource = source => { + const match = source.match(ANNOTATIONS_REGEX); + const annotation = match && match[1] ? match[1] : ''; + return annotation; +}; + const getAnnotationValue = (annotations, key) => { const valueRegex = new RegExp(`${key}${ANNOTATION_VALUE_REGEX}`); const match = annotations.match(valueRegex); @@ -39,9 +49,24 @@ const getAnnotationValue = (annotations, key) => { */ const isCodedFile = filePath => TEMPLATE_EXTENSION_REGEX.test(filePath); +/* + * Returns true if: + * filename is module.html, inside of *.module folder + */ +const isModuleHTMLFile = filePath => MODULE_HTML_EXTENSION_REGEX.test(filePath); + +/* + * Returns true if: + * filename is module.css, inside of *.module folder + */ +const isModuleCSSFile = filePath => MODULE_CSS_EXTENSION_REGEX.test(filePath); + module.exports = { ANNOTATION_KEYS, getAnnotationValue, + getAnnotationsFromSource, getFileAnnotations, isCodedFile, + isModuleCSSFile, + isModuleHTMLFile, }; From 6ab7619a86651a4f0e2ee38dc0517d28720d77b4 Mon Sep 17 00:00:00 2001 From: Anthony Pizzurro Date: Fri, 6 Aug 2021 15:19:47 -0400 Subject: [PATCH 2/7] handle empty source --- packages/cli-lib/lib/constants.js | 4 ++-- packages/cli-lib/templates.js | 10 +++++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/packages/cli-lib/lib/constants.js b/packages/cli-lib/lib/constants.js index a0941215c..f750a06c0 100644 --- a/packages/cli-lib/lib/constants.js +++ b/packages/cli-lib/lib/constants.js @@ -118,7 +118,7 @@ const PROJECT_TEMPLATE_TYPES = { }, }; -const TEMPLATE_TYPE_ID = { +const TEMPLATE_TYPES = { unmapped: 0, email_base_template: 1, email: 2, @@ -187,5 +187,5 @@ module.exports = { POLLING_DELAY, PROJECT_TEMPLATE_TYPES, SCOPE_GROUPS, - TEMPLATE_TYPE_ID, + TEMPLATE_TYPES, }; diff --git a/packages/cli-lib/templates.js b/packages/cli-lib/templates.js index 345a59529..b1401e2a3 100644 --- a/packages/cli-lib/templates.js +++ b/packages/cli-lib/templates.js @@ -32,9 +32,13 @@ const getFileAnnotations = filePath => { }; const getAnnotationsFromSource = source => { - const match = source.match(ANNOTATIONS_REGEX); - const annotation = match && match[1] ? match[1] : ''; - return annotation; + try { + const match = source.match(ANNOTATIONS_REGEX); + const annotation = match && match[1] ? match[1] : ''; + return annotation; + } catch (err) { + return ''; + } }; const getAnnotationValue = (annotations, key) => { From a28d895cc5a934a8e24bdd25509c3cede3181ef9 Mon Sep 17 00:00:00 2001 From: Anthony Pizzurro Date: Wed, 18 Aug 2021 16:22:42 -0400 Subject: [PATCH 3/7] move module utils to modules.js --- packages/cli-lib/modules.js | 21 +++++++++++++++++++++ packages/cli-lib/templates.js | 19 +------------------ 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/packages/cli-lib/modules.js b/packages/cli-lib/modules.js index a9ca97be7..81285123c 100644 --- a/packages/cli-lib/modules.js +++ b/packages/cli-lib/modules.js @@ -4,6 +4,11 @@ const { getCwd, getExt, splitHubSpotPath, splitLocalPath } = require('./path'); const { walk } = require('./lib/walk'); const { MODULE_EXTENSION } = require('./lib/constants'); +// Matches files named module.html +const MODULE_HTML_EXTENSION_REGEX = new RegExp(/(\.module\/module\.html)/); +// Matches files named module.css +const MODULE_CSS_EXTENSION_REGEX = new RegExp(/(\.module\/module\.css)/); + const isBool = x => !!x === x; /** @@ -150,9 +155,25 @@ async function validateSrcAndDestPaths(src, dest) { return results; } +/** + * Checks if the given path points to an .html file within a .module folder + * @param {string} filePath + * @returns {boolean} + */ +const isModuleHTMLFile = filePath => MODULE_HTML_EXTENSION_REGEX.test(filePath); + +/** + * Checks if the given path points to an .css file within a .module folder + * @param {string} filePath + * @returns {boolean} + */ +const isModuleCSSFile = filePath => MODULE_CSS_EXTENSION_REGEX.test(filePath); + module.exports = { isModuleFolder, isModuleFolderChild, validateSrcAndDestPaths, ValidationIds, + isModuleHTMLFile, + isModuleCSSFile, }; diff --git a/packages/cli-lib/templates.js b/packages/cli-lib/templates.js index b1401e2a3..933edc69f 100644 --- a/packages/cli-lib/templates.js +++ b/packages/cli-lib/templates.js @@ -3,10 +3,7 @@ const { logger } = require('./logger'); // Matches the .html file extension, excluding module.html const TEMPLATE_EXTENSION_REGEX = new RegExp(/(?/; // Matches an annotation value, ending at space, newline, or end of string @@ -53,24 +50,10 @@ const getAnnotationValue = (annotations, key) => { */ const isCodedFile = filePath => TEMPLATE_EXTENSION_REGEX.test(filePath); -/* - * Returns true if: - * filename is module.html, inside of *.module folder - */ -const isModuleHTMLFile = filePath => MODULE_HTML_EXTENSION_REGEX.test(filePath); - -/* - * Returns true if: - * filename is module.css, inside of *.module folder - */ -const isModuleCSSFile = filePath => MODULE_CSS_EXTENSION_REGEX.test(filePath); - module.exports = { ANNOTATION_KEYS, getAnnotationValue, getAnnotationsFromSource, getFileAnnotations, isCodedFile, - isModuleCSSFile, - isModuleHTMLFile, }; From 532146bb8ddacc312e9b2eb30cbd0920a7c1dc4d Mon Sep 17 00:00:00 2001 From: Anthony Pizzurro Date: Wed, 18 Aug 2021 17:04:27 -0400 Subject: [PATCH 4/7] refactor getFileAnnotations --- packages/cli-lib/templates.js | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/packages/cli-lib/templates.js b/packages/cli-lib/templates.js index 933edc69f..eef166b49 100644 --- a/packages/cli-lib/templates.js +++ b/packages/cli-lib/templates.js @@ -19,9 +19,7 @@ const ANNOTATION_KEYS = { const getFileAnnotations = filePath => { try { const data = fs.readFileSync(filePath, 'utf8'); - const match = data.match(ANNOTATIONS_REGEX); - const annotation = match && match[1] ? match[1] : ''; - return annotation; + return getAnnotationsFromSource(data); } catch (err) { logger.debug(err); return ''; @@ -29,13 +27,9 @@ const getFileAnnotations = filePath => { }; const getAnnotationsFromSource = source => { - try { - const match = source.match(ANNOTATIONS_REGEX); - const annotation = match && match[1] ? match[1] : ''; - return annotation; - } catch (err) { - return ''; - } + const match = source.match(ANNOTATIONS_REGEX); + const annotation = match && match[1] ? match[1] : ''; + return annotation; }; const getAnnotationValue = (annotations, key) => { From 42459f358c58d5c37108d007036bfdce4aa989eb Mon Sep 17 00:00:00 2001 From: Anthony Pizzurro Date: Wed, 18 Aug 2021 17:26:10 -0400 Subject: [PATCH 5/7] refactor getAnnotationValue related logic --- packages/cli-lib/templates.js | 28 +++++-------------- .../theme/TemplateValidator.js | 26 ++++++++--------- 2 files changed, 20 insertions(+), 34 deletions(-) diff --git a/packages/cli-lib/templates.js b/packages/cli-lib/templates.js index eef166b49..88eb4b33a 100644 --- a/packages/cli-lib/templates.js +++ b/packages/cli-lib/templates.js @@ -1,6 +1,3 @@ -const fs = require('fs'); -const { logger } = require('./logger'); - // Matches the .html file extension, excluding module.html const TEMPLATE_EXTENSION_REGEX = new RegExp(/(? { - try { - const data = fs.readFileSync(filePath, 'utf8'); - return getAnnotationsFromSource(data); - } catch (err) { - logger.debug(err); - return ''; - } -}; - -const getAnnotationsFromSource = source => { - const match = source.match(ANNOTATIONS_REGEX); - const annotation = match && match[1] ? match[1] : ''; - return annotation; -}; - const getAnnotationValue = (annotations, key) => { const valueRegex = new RegExp(`${key}${ANNOTATION_VALUE_REGEX}`); const match = annotations.match(valueRegex); return match ? match[1].trim() : null; }; +const buildAnnotationValueGetter = source => { + const match = source.match(ANNOTATIONS_REGEX); + const annotation = match && match[1] ? match[1] : ''; + return annotationKey => getAnnotationValue(annotation, annotationKey); +}; + /* * Returns true if: * .html extension (ignoring module.html) @@ -47,7 +34,6 @@ const isCodedFile = filePath => TEMPLATE_EXTENSION_REGEX.test(filePath); module.exports = { ANNOTATION_KEYS, getAnnotationValue, - getAnnotationsFromSource, - getFileAnnotations, + buildAnnotationValueGetter, isCodedFile, }; diff --git a/packages/cli/lib/validators/marketplaceValidators/theme/TemplateValidator.js b/packages/cli/lib/validators/marketplaceValidators/theme/TemplateValidator.js index 7178f3560..782588795 100644 --- a/packages/cli/lib/validators/marketplaceValidators/theme/TemplateValidator.js +++ b/packages/cli/lib/validators/marketplaceValidators/theme/TemplateValidator.js @@ -1,11 +1,12 @@ +const fs = require('fs'); const { ANNOTATION_KEYS, - getAnnotationValue, - getFileAnnotations, + buildAnnotationValueGetter, isCodedFile, } = require('@hubspot/cli-lib/templates'); const BaseValidator = require('../BaseValidator'); const { VALIDATOR_KEYS } = require('../../constants'); +const { logger } = require('@hubspot/cli-lib/logger'); const TEMPLATE_LIMIT = 50; const TEMPLATE_COUNT_IGNORE_LIST = ['global_partial', 'none']; @@ -99,24 +100,23 @@ class TemplateValidator extends BaseValidator { files.forEach(file => { if (isCodedFile(file)) { - const annotations = getFileAnnotations(file); + let data; + try { + data = fs.readFileSync(file, 'utf8'); + } catch (e) { + logger.error(`Error reading file ${file}`); + } + const getAnnotationValue = buildAnnotationValueGetter(data); + const isAvailableForNewContent = getAnnotationValue( - annotations, ANNOTATION_KEYS.isAvailableForNewContent ); if (isAvailableForNewContent !== 'false') { - const templateType = getAnnotationValue( - annotations, - ANNOTATION_KEYS.templateType - ); + const templateType = getAnnotationValue(ANNOTATION_KEYS.templateType); if (templateType) { - const label = getAnnotationValue( - annotations, - ANNOTATION_KEYS.label - ); + const label = getAnnotationValue(ANNOTATION_KEYS.label); const screenshotPath = getAnnotationValue( - annotations, ANNOTATION_KEYS.screenshotPath ); From f40131cb010385e4287fd0a95597823d8eba9fbd Mon Sep 17 00:00:00 2001 From: Anthony Pizzurro Date: Fri, 10 Sep 2021 17:09:32 -0400 Subject: [PATCH 6/7] update tests for buildAnnotationValueGetter --- .../lib/validators/__tests__/TemplateValidator.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/cli/lib/validators/__tests__/TemplateValidator.js b/packages/cli/lib/validators/__tests__/TemplateValidator.js index 3da6e9dec..d5ad90b08 100644 --- a/packages/cli/lib/validators/__tests__/TemplateValidator.js +++ b/packages/cli/lib/validators/__tests__/TemplateValidator.js @@ -15,11 +15,13 @@ jest.mock('@hubspot/cli-lib/templates'); const TEMPLATE_LIMIT = 50; const mockGetAnnotationValue = (templateType, rest) => { - templates.getAnnotationValue.mockImplementation((x, key) => { - if (key === 'templateType') { - return templateType; - } - return rest; + templates.buildAnnotationValueGetter.mockImplementation(() => { + return key => { + if (key === 'templateType') { + return templateType; + } + return rest; + }; }); }; From 7611ea279ef523952d8b9610de62ba61ada54ea4 Mon Sep 17 00:00:00 2001 From: Anthony Pizzurro Date: Mon, 13 Sep 2021 09:39:02 -0400 Subject: [PATCH 7/7] update regex patterns --- packages/cli-lib/modules.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/cli-lib/modules.js b/packages/cli-lib/modules.js index 81285123c..a88a21ffd 100644 --- a/packages/cli-lib/modules.js +++ b/packages/cli-lib/modules.js @@ -5,9 +5,9 @@ const { walk } = require('./lib/walk'); const { MODULE_EXTENSION } = require('./lib/constants'); // Matches files named module.html -const MODULE_HTML_EXTENSION_REGEX = new RegExp(/(\.module\/module\.html)/); +const MODULE_HTML_EXTENSION_REGEX = new RegExp(/\.module\/module\.html$/); // Matches files named module.css -const MODULE_CSS_EXTENSION_REGEX = new RegExp(/(\.module\/module\.css)/); +const MODULE_CSS_EXTENSION_REGEX = new RegExp(/\.module\/module\.css$/); const isBool = x => !!x === x;