@appdirect/sfb-toolkit 4.0.22 → 4.0.25
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/bin/index.js +92 -14
- package/coverage/clover.xml +32 -32
- package/coverage/coverage-final.json +4 -4
- package/coverage/lcov-report/constants/customUserAttributesMock.js.html +1 -1
- package/coverage/lcov-report/constants/index.html +1 -1
- package/coverage/lcov-report/constants/pageUrls.js.html +1 -1
- package/coverage/lcov-report/constants/userMock.js.html +1 -1
- package/coverage/lcov-report/controllers/controllers-utils/assetsUtils.js.html +1 -1
- package/coverage/lcov-report/controllers/controllers-utils/index.html +1 -1
- package/coverage/lcov-report/controllers/data-filters/bootstrap-data-filter.js.html +1 -1
- package/coverage/lcov-report/controllers/data-filters/call-to-actions-filter.js.html +1 -1
- package/coverage/lcov-report/controllers/data-filters/index.html +1 -1
- package/coverage/lcov-report/controllers/index.html +1 -1
- package/coverage/lcov-report/controllers/index.js.html +1 -1
- package/coverage/lcov-report/front-end-utils/src/components/base/ResultItem.js.html +7 -7
- package/coverage/lcov-report/front-end-utils/src/components/base/SearchBar.js.html +3 -3
- package/coverage/lcov-report/front-end-utils/src/components/base/index.html +1 -1
- package/coverage/lcov-report/front-end-utils/src/components/base/item.js.html +11 -11
- package/coverage/lcov-report/front-end-utils/src/components/container.js.html +1 -1
- package/coverage/lcov-report/front-end-utils/src/components/data-view.js.html +1 -1
- package/coverage/lcov-report/front-end-utils/src/components/header-view.js.html +1 -1
- package/coverage/lcov-report/front-end-utils/src/components/index.html +1 -1
- package/coverage/lcov-report/front-end-utils/src/components/search.js.html +13 -13
- package/coverage/lcov-report/front-end-utils/src/constants/index.html +1 -1
- package/coverage/lcov-report/front-end-utils/src/constants/pageUrl.js.html +1 -1
- package/coverage/lcov-report/front-end-utils/src/constants/roleType.js.html +1 -1
- package/coverage/lcov-report/front-end-utils/src/constants/searchType.js.html +1 -1
- package/coverage/lcov-report/front-end-utils/src/utils/debounce.js.html +1 -1
- package/coverage/lcov-report/front-end-utils/src/utils/index.html +1 -1
- package/coverage/lcov-report/index.html +1 -1
- package/coverage/lcov-report/middlewares/config.js.html +1 -1
- package/coverage/lcov-report/middlewares/index.html +1 -1
- package/coverage/lcov-report/tools/index.html +1 -1
- package/coverage/lcov-report/tools/proxy-fetch.js.html +1 -1
- package/coverage/lcov.info +81 -81
- package/customComponents/index.js +10 -2
- package/npm-shrinkwrap.json +144 -20
- package/package.json +4 -4
- package/readme.md +9 -0
- package/src/boilerplates/AddComponent/AddComponent.jsx +234 -0
- package/src/boilerplates/AddComponent/styles/AddComponent.scss +47 -0
- package/src/tools/prompt-questions.js +109 -14
- package/src/tools/toolkit-common-fun.js +30 -15
- package/src/tools/toolkit-common-vars-namespace.js +106 -0
- package/src/tools/toolkit-common-vars.js +1 -1
- package/src/tools/toolkit-components-add.js +334 -0
- package/src/tools/toolkit-components-copy.js +78 -46
- package/src/tools/toolkit-components-delete.js +370 -0
- package/src/tools/toolkit-components-fun.js +126 -7
- package/src/tools/toolkit-components-info.js +6 -4
- package/src/tools/toolkit-components-vars.js +50 -0
- package/src/tools/toolkit-components.js +1 -1
- package/src/tools/toolkit-create.js +2 -3
- package/src/tools/toolkit-import.js +182 -0
- package/src/tools/toolkit-install.js +6 -5
- package/src/tools/toolkit-package.js +3 -0
- package/src/tools/toolkit-setup.js +1 -1
- package/src/tools/toolkit-start.js +4 -1
- package/src/tools/toolkit-storybook.js +136 -48
- package/src/tools/toolkit-zip-utils.js +35 -0
- package/src/tools/zip-theme.js +62 -50
- package/themes-config.json +0 -7
- package/customComponents/atoms/label/Label.jsx +0 -20
- package/customComponents/atoms/label/stories/Label.story.mdx +0 -20
- package/customComponents/atoms/label/styles/Label.scss +0 -4
- package/customComponents/components/atoms-showcase-custom/AtomsShowcaseCustom.jsx +0 -8
- package/customComponents/components/atoms-showcase-custom/stories/AtomsShowcaseCustom.story.mdx +0 -17
- package/customComponents/components/atoms-showcase-modules/AtomsShowcaseModules.jsx +0 -223
- package/customComponents/components/atoms-showcase-modules/stories/AtomsShowcaseModules.story.mdx +0 -49
- package/customComponents/components/index.js +0 -2
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
/* eslint-disable no-unused-expressions, no-param-reassign */
|
|
2
|
+
|
|
3
|
+
const path = require("path");
|
|
4
|
+
const chalk = require("chalk");
|
|
5
|
+
const fs = require("fs-extra");
|
|
6
|
+
const crypto = require("crypto");
|
|
7
|
+
|
|
8
|
+
const prompt = require("./prompt-questions");
|
|
9
|
+
const { ACTIVE_THEME_PATH, TOOLKIT_PATH } = require("./toolkit-common-vars");
|
|
10
|
+
const {
|
|
11
|
+
NAMESPACE_FILE_PATHS,
|
|
12
|
+
NAMESPACE_ORDERABLE_PAGE_SETTINGS_KEYS,
|
|
13
|
+
PAGE_NAME_TO_NAMESPACE,
|
|
14
|
+
} = require("./toolkit-common-vars-namespace");
|
|
15
|
+
const { customComponentsEnabled } = require("./toolkit-components-fun");
|
|
16
|
+
const {
|
|
17
|
+
convertPascalCaseToKebabCase,
|
|
18
|
+
convertPascalCaseToCamelCase,
|
|
19
|
+
convertPascalCaseToSentenceCase,
|
|
20
|
+
} = require("./toolkit-common-fun");
|
|
21
|
+
const { COMPONENTS_NOT_INSTALLED_ERROR } = require("./toolkit-components-vars");
|
|
22
|
+
|
|
23
|
+
const SETTINGS_PATH = `${ACTIVE_THEME_PATH}/settings.json`;
|
|
24
|
+
|
|
25
|
+
const BOILERPLATE_PATH = `${TOOLKIT_PATH}/src/boilerplates`;
|
|
26
|
+
const DESTINATION_PATH = `${ACTIVE_THEME_PATH}/customComponents/components`.replaceAll("\\", "/");
|
|
27
|
+
const DESTINATION_INDEX_FILE = `${ACTIVE_THEME_PATH}/customComponents/index.js`;
|
|
28
|
+
|
|
29
|
+
const BOILERPLATE_COMPONENT_NAME = "AddComponent";
|
|
30
|
+
const BOILERPLATE_FOLDER = "AddComponent/";
|
|
31
|
+
const BOILERPLATE_JSX = `AddComponent.jsx`;
|
|
32
|
+
const BOILERPLATE_SCSS = `styles/AddComponent.scss`;
|
|
33
|
+
|
|
34
|
+
const COMPONENT_NAME_PASCAL_CASE = "{{COMPONENT_NAME_PASCAL_CASE}}";
|
|
35
|
+
const COMPONENT_NAME_CAMEL_CASE = "{{COMPONENT_NAME_CAMEL_CASE}}";
|
|
36
|
+
const COMPONENT_NAME_SENTENCE_CASE = "{{COMPONENT_NAME_SENTENCE_CASE}}";
|
|
37
|
+
const COMPONENT_ORDERABLE_NAMESPACE_IMPORT =
|
|
38
|
+
"{{COMPONENT_ORDERABLE_NAMESPACE_IMPORT}}";
|
|
39
|
+
const COMPONENT_ORDERABLE_SECTION = "{{COMPONENT_ORDERABLE_SECTION}}";
|
|
40
|
+
|
|
41
|
+
const formattedComponentStatusWithPath = ({
|
|
42
|
+
data,
|
|
43
|
+
statusMessage,
|
|
44
|
+
componentPath,
|
|
45
|
+
maxStringLength = 30,
|
|
46
|
+
}) => {
|
|
47
|
+
const formattingOffset =
|
|
48
|
+
maxStringLength + 20 <= 50 ? 50 : maxStringLength + 20;
|
|
49
|
+
console.log(
|
|
50
|
+
chalk.green(
|
|
51
|
+
`${data.padEnd(formattingOffset, ".")}[${statusMessage}][${chalk.dim(
|
|
52
|
+
componentPath
|
|
53
|
+
)}]`
|
|
54
|
+
)
|
|
55
|
+
);
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const addOrderableComponentToSettingsFile = (namespace, componentName) => {
|
|
59
|
+
const componentObjectTemplate = {
|
|
60
|
+
name: componentName,
|
|
61
|
+
id: `${componentName}:orderable:${crypto.randomUUID()}`,
|
|
62
|
+
isVisible: true,
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
try {
|
|
66
|
+
const settings = fs.readJsonSync(SETTINGS_PATH);
|
|
67
|
+
|
|
68
|
+
const orderablePageSettingsKey =
|
|
69
|
+
NAMESPACE_ORDERABLE_PAGE_SETTINGS_KEYS[namespace];
|
|
70
|
+
|
|
71
|
+
const orderableComponentsInPage =
|
|
72
|
+
settings?.pages?.[orderablePageSettingsKey]?.orderableComponents;
|
|
73
|
+
|
|
74
|
+
if (orderableComponentsInPage) {
|
|
75
|
+
orderableComponentsInPage.push(componentObjectTemplate);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
fs.writeJsonSync(SETTINGS_PATH, settings, { spaces: 2 });
|
|
79
|
+
|
|
80
|
+
console.log(
|
|
81
|
+
chalk.green(
|
|
82
|
+
`Orderable component [${componentName}] added to ${orderablePageSettingsKey} page through settings.json`
|
|
83
|
+
)
|
|
84
|
+
);
|
|
85
|
+
} catch (e) {
|
|
86
|
+
console.error(
|
|
87
|
+
chalk.red(
|
|
88
|
+
`ERROR: Unable to add component to settings.json file due to ${e}`
|
|
89
|
+
)
|
|
90
|
+
);
|
|
91
|
+
process.exit(1);
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
const addNonOrderableComponentToHtmlFile = (namespace, componentName) => {
|
|
96
|
+
const blockStart = "{% block";
|
|
97
|
+
const blockEnd = "{% endblock %}";
|
|
98
|
+
const NON_ORDERABLE_COMPONENT_TEMPLATE = `\u0009<component name="${componentName}" id="${componentName}1"></component>`;
|
|
99
|
+
|
|
100
|
+
const htmlPage = NAMESPACE_FILE_PATHS[namespace];
|
|
101
|
+
|
|
102
|
+
try {
|
|
103
|
+
const fileContent = fs.readFileSync(htmlPage, "utf8");
|
|
104
|
+
const lines = fileContent.split("\n");
|
|
105
|
+
|
|
106
|
+
let startIndex = -1;
|
|
107
|
+
let endIndex = -1;
|
|
108
|
+
|
|
109
|
+
// insert component inside the content block
|
|
110
|
+
lines.forEach((line, index) => {
|
|
111
|
+
// gets the last block in the page
|
|
112
|
+
if (line.includes(blockStart)) {
|
|
113
|
+
startIndex = index;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (line.includes(blockEnd)) {
|
|
117
|
+
endIndex = index;
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
if (startIndex !== -1 && endIndex !== -1 && endIndex > startIndex) {
|
|
122
|
+
lines.splice(endIndex, 0, NON_ORDERABLE_COMPONENT_TEMPLATE);
|
|
123
|
+
fs.writeFileSync(htmlPage, lines.join("\n"));
|
|
124
|
+
} else {
|
|
125
|
+
throw Error("Cannot find content block in the html page");
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
console.log(
|
|
129
|
+
chalk.green(
|
|
130
|
+
`Non-orderable component [${componentName}] added to ${htmlPage}`
|
|
131
|
+
)
|
|
132
|
+
);
|
|
133
|
+
} catch (e) {
|
|
134
|
+
console.error(
|
|
135
|
+
chalk.red(
|
|
136
|
+
`ERROR: ${componentName} cannot be added to ${htmlPage} due to ${e}`
|
|
137
|
+
)
|
|
138
|
+
);
|
|
139
|
+
process.exit(1);
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
const addComponentRendering = (pageNameAndOrderableType, componentName) => {
|
|
144
|
+
const { namespace, orderable } = pageNameAndOrderableType;
|
|
145
|
+
|
|
146
|
+
if (orderable) {
|
|
147
|
+
addOrderableComponentToSettingsFile(namespace, componentName);
|
|
148
|
+
} else {
|
|
149
|
+
addNonOrderableComponentToHtmlFile(namespace, componentName);
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
const updateOrderableSection = (fileContent, pageNameAndOrderableType) => {
|
|
154
|
+
const { namespace, orderable } = pageNameAndOrderableType || {};
|
|
155
|
+
|
|
156
|
+
if (orderable) {
|
|
157
|
+
const orderableSectionNamespaceImportTemplate = `import { NAMESPACES } from '@appdirect/sfb-theme-components/src/constants/namespaces';\n`;
|
|
158
|
+
const orderableSectionTemplate = `\n\u0009orderableSection: [ ${namespace} ],`;
|
|
159
|
+
|
|
160
|
+
fileContent = fileContent.replace(
|
|
161
|
+
COMPONENT_ORDERABLE_NAMESPACE_IMPORT,
|
|
162
|
+
orderableSectionNamespaceImportTemplate
|
|
163
|
+
);
|
|
164
|
+
|
|
165
|
+
fileContent = fileContent.replace(
|
|
166
|
+
COMPONENT_ORDERABLE_SECTION,
|
|
167
|
+
orderableSectionTemplate
|
|
168
|
+
);
|
|
169
|
+
} else {
|
|
170
|
+
fileContent = fileContent.replace(COMPONENT_ORDERABLE_NAMESPACE_IMPORT, "");
|
|
171
|
+
fileContent = fileContent.replace(COMPONENT_ORDERABLE_SECTION, "");
|
|
172
|
+
}
|
|
173
|
+
return fileContent;
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
const addComponentFromBoilerplate = (
|
|
177
|
+
newComponentName,
|
|
178
|
+
newComponentFolderName,
|
|
179
|
+
pageNameAndOrderableType
|
|
180
|
+
) => {
|
|
181
|
+
const boilerplateFolderPath = path.join(BOILERPLATE_PATH, BOILERPLATE_FOLDER);
|
|
182
|
+
|
|
183
|
+
try {
|
|
184
|
+
// jsx
|
|
185
|
+
let fileContentJsx = fs.readFileSync(
|
|
186
|
+
path.join(boilerplateFolderPath, BOILERPLATE_JSX),
|
|
187
|
+
"utf8"
|
|
188
|
+
);
|
|
189
|
+
|
|
190
|
+
fileContentJsx = fileContentJsx.replaceAll(
|
|
191
|
+
COMPONENT_NAME_PASCAL_CASE,
|
|
192
|
+
newComponentName
|
|
193
|
+
);
|
|
194
|
+
|
|
195
|
+
fileContentJsx = fileContentJsx.replaceAll(
|
|
196
|
+
COMPONENT_NAME_SENTENCE_CASE,
|
|
197
|
+
convertPascalCaseToSentenceCase(newComponentName)
|
|
198
|
+
);
|
|
199
|
+
|
|
200
|
+
fileContentJsx = updateOrderableSection(
|
|
201
|
+
fileContentJsx,
|
|
202
|
+
pageNameAndOrderableType
|
|
203
|
+
);
|
|
204
|
+
|
|
205
|
+
const newFilePathJsx = path.join(
|
|
206
|
+
DESTINATION_PATH,
|
|
207
|
+
newComponentFolderName,
|
|
208
|
+
BOILERPLATE_JSX.replace(BOILERPLATE_COMPONENT_NAME, newComponentName)
|
|
209
|
+
);
|
|
210
|
+
|
|
211
|
+
fs.outputFileSync(newFilePathJsx, fileContentJsx);
|
|
212
|
+
|
|
213
|
+
// add rendering: orderable to settings.json; non-orderable to HTML file
|
|
214
|
+
if (pageNameAndOrderableType) {
|
|
215
|
+
addComponentRendering(pageNameAndOrderableType, newComponentName);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// scss
|
|
219
|
+
let fileContentScss = fs.readFileSync(
|
|
220
|
+
path.join(boilerplateFolderPath, BOILERPLATE_SCSS),
|
|
221
|
+
"utf8"
|
|
222
|
+
);
|
|
223
|
+
|
|
224
|
+
const newFilePathScss = path.join(
|
|
225
|
+
DESTINATION_PATH,
|
|
226
|
+
newComponentFolderName,
|
|
227
|
+
BOILERPLATE_SCSS.replace(BOILERPLATE_COMPONENT_NAME, newComponentName)
|
|
228
|
+
);
|
|
229
|
+
|
|
230
|
+
fileContentScss = fileContentScss.replaceAll(
|
|
231
|
+
COMPONENT_NAME_CAMEL_CASE,
|
|
232
|
+
convertPascalCaseToCamelCase(newComponentName)
|
|
233
|
+
);
|
|
234
|
+
|
|
235
|
+
fs.outputFileSync(newFilePathScss, fileContentScss);
|
|
236
|
+
|
|
237
|
+
formattedComponentStatusWithPath({
|
|
238
|
+
data: newComponentName,
|
|
239
|
+
statusMessage: "ADDED",
|
|
240
|
+
componentPath: `${DESTINATION_PATH}/${newComponentFolderName}`,
|
|
241
|
+
});
|
|
242
|
+
// FIXME: write translations for UI editor settings
|
|
243
|
+
} catch (err) {
|
|
244
|
+
console.error(
|
|
245
|
+
chalk.red(` ERROR: Unable to create component from boilerplate ${err}`)
|
|
246
|
+
);
|
|
247
|
+
process.exit(1);
|
|
248
|
+
}
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
(async () => {
|
|
252
|
+
const args = process.argv.slice(2);
|
|
253
|
+
|
|
254
|
+
const [componentNameFromCommand, page, orderable] = args;
|
|
255
|
+
|
|
256
|
+
const isCustomComponentsEnabled = await customComponentsEnabled();
|
|
257
|
+
|
|
258
|
+
if (!isCustomComponentsEnabled) {
|
|
259
|
+
console.log(COMPONENTS_NOT_INSTALLED_ERROR);
|
|
260
|
+
process.exit(1);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// ComponentName
|
|
264
|
+
let componentName;
|
|
265
|
+
if (
|
|
266
|
+
prompt.validateFun.isComponentNameValidAndAvailable(
|
|
267
|
+
componentNameFromCommand
|
|
268
|
+
)
|
|
269
|
+
) {
|
|
270
|
+
componentName = componentNameFromCommand;
|
|
271
|
+
} else {
|
|
272
|
+
const answer = await prompt.ask(["ComponentName"]);
|
|
273
|
+
componentName = answer.ComponentName;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
// PageNameAndOrderableType
|
|
277
|
+
let pageNameAndOrderableType;
|
|
278
|
+
const pageName = PAGE_NAME_TO_NAMESPACE[page];
|
|
279
|
+
const isOrderable = orderable === "true";
|
|
280
|
+
|
|
281
|
+
// page not provided in CLI param, ask for it
|
|
282
|
+
if (page === "undefined") {
|
|
283
|
+
console.log(`You can optionally add the component to a page as orderable or non-orderable.
|
|
284
|
+
\nFor more information, please see our documentation:
|
|
285
|
+
\u2022 Orderable: ${chalk.underline.bold(
|
|
286
|
+
"https://developer.appdirect.com/storefront/how-tos/components/storefront-custom-components-layout"
|
|
287
|
+
)}
|
|
288
|
+
\u2022 Non-orderable: ${chalk.underline.bold(
|
|
289
|
+
"https://developer.appdirect.com/storefront/how-tos/components/storefront-custom-components-add"
|
|
290
|
+
)}\n`);
|
|
291
|
+
|
|
292
|
+
const answer = await prompt.ask(["PageNameAndOrderableType"]);
|
|
293
|
+
|
|
294
|
+
pageNameAndOrderableType = answer.PageNameAndOrderableType;
|
|
295
|
+
} else if (page === "none") {
|
|
296
|
+
// page set to none in CLI param, don't ask, don't add
|
|
297
|
+
pageNameAndOrderableType = null;
|
|
298
|
+
} else if (isOrderable && !NAMESPACE_ORDERABLE_PAGE_SETTINGS_KEYS[pageName]) {
|
|
299
|
+
// page set to a valid value in CLI param, but orderable does not apply
|
|
300
|
+
console.log(
|
|
301
|
+
chalk.red(
|
|
302
|
+
"ERROR: Only [home] and [profile] page can have orderable components."
|
|
303
|
+
)
|
|
304
|
+
);
|
|
305
|
+
process.exit(1);
|
|
306
|
+
} else {
|
|
307
|
+
// page set to a valid value in CLI param
|
|
308
|
+
pageNameAndOrderableType = {
|
|
309
|
+
namespace: pageName,
|
|
310
|
+
orderable: isOrderable,
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
const newComponentFolderName = convertPascalCaseToKebabCase(componentName);
|
|
315
|
+
|
|
316
|
+
addComponentFromBoilerplate(
|
|
317
|
+
componentName,
|
|
318
|
+
newComponentFolderName,
|
|
319
|
+
pageNameAndOrderableType
|
|
320
|
+
);
|
|
321
|
+
|
|
322
|
+
try {
|
|
323
|
+
// write export in components/index.js
|
|
324
|
+
const exportStringTemplate = `\nexport { default as ${componentName} } from './components/${newComponentFolderName}/${componentName}';`;
|
|
325
|
+
fs.appendFileSync(DESTINATION_INDEX_FILE, exportStringTemplate);
|
|
326
|
+
} catch (e) {
|
|
327
|
+
console.error(
|
|
328
|
+
chalk.red(
|
|
329
|
+
`ERROR: Unable to export component in ${DESTINATION_INDEX_FILE} due to ${e}`
|
|
330
|
+
)
|
|
331
|
+
);
|
|
332
|
+
process.exit(1);
|
|
333
|
+
}
|
|
334
|
+
})();
|
|
@@ -1,38 +1,38 @@
|
|
|
1
1
|
const fs = require("fs");
|
|
2
|
-
const { COMMAND_PARAM, ACTIVE_THEME_PATH } = require("./toolkit-common-vars");
|
|
3
2
|
const { customComponentsEnabled } = require("./toolkit-components-fun");
|
|
4
3
|
const { getSfbThemeComponentsVersion, getToolkitVersionUsedToCreateTheme,
|
|
5
4
|
shouldBeProjectFolder } = require("./toolkit-common-fun");
|
|
5
|
+
const {
|
|
6
|
+
COPIED, NO_MATCH, SUCCESS, FOUND, ERROR, SKIP,
|
|
7
|
+
INDEX_EXPORT_NO_CHANGE, INDEX_EXPORT_COMMENT_OUT, INDEX_EXPORT_REPLACE,
|
|
8
|
+
FEATURE_AVAILABLE_SINCE_TOOLKIT_VERSION, BLACKLISTED_COMPONENTS,
|
|
9
|
+
PATH_TO_CUSTOM_COMPONENTS_INDEX, BASE_PATH_TO_COMPONENTS_INDEX,
|
|
10
|
+
COMPONENTS_NOT_INSTALLED_ERROR, FEATURE_AVAILABLE_SINCE_SFB_THEME_VERSION
|
|
11
|
+
} = require("./toolkit-components-vars");
|
|
6
12
|
const chalk = require("chalk");
|
|
7
13
|
const inquirer = require("inquirer");
|
|
8
14
|
|
|
9
15
|
// check if user is in correct location
|
|
10
16
|
shouldBeProjectFolder(true);
|
|
11
17
|
|
|
12
|
-
|
|
13
|
-
const INDEX_EXPORT_NO_CHANGE = 'nothing';
|
|
14
|
-
const INDEX_EXPORT_COMMENT_OUT = 'comment';
|
|
15
|
-
const INDEX_EXPORT_REPLACE = 'replace';
|
|
16
|
-
const FEATURE_AVAILABLE_SINCE_TOOLKIT_VERSION = '4.0.21';
|
|
17
|
-
const BLACKLISTED_COMPONENTS = ["PageOrderableContainer"];
|
|
18
|
-
const PATH_TO_CUSTOM_COMPONENTS_INDEX = `${ACTIVE_THEME_PATH}/customComponents/index.js`;
|
|
19
|
-
const BASE_PATH_TO_COMPONENTS_INDEX = `${ACTIVE_THEME_PATH}/node_modules/@appdirect/sfb-theme-components/src/components/index.js`;
|
|
20
|
-
const sfbThemeComponentsVersion = getSfbThemeComponentsVersion().replace(/\.|\^/g, '_');
|
|
18
|
+
let sfbThemeComponentsVersionCustom;
|
|
21
19
|
const themeToolkitVersion = getToolkitVersionUsedToCreateTheme();
|
|
22
20
|
|
|
23
21
|
const PAGE_ORDERABLE_CONTAINER_DIR = 'page-orderable-container'
|
|
24
22
|
const PAGE_ORDERABLE_CONTAINER_BASE_PATH = `@appdirect/sfb-theme-components/src/components/${PAGE_ORDERABLE_CONTAINER_DIR}`
|
|
23
|
+
const getPathToCopyTo = (path) => (path.replace('node_modules/@appdirect/sfb-theme-components',
|
|
24
|
+
`customComponents/sfb_theme_components_${sfbThemeComponentsVersionCustom}`));
|
|
25
25
|
|
|
26
|
-
const formattedComponentStatus = ({ data, statusMessage, statusType, maxStringLength = 30 }) => {
|
|
26
|
+
const formattedComponentStatus = ({ data, statusMessage, statusType, path = '', maxStringLength = 30 }) => {
|
|
27
27
|
const formattingOffset = (maxStringLength + 20) <= 50 ? 50 : maxStringLength + 20;
|
|
28
28
|
switch(statusType) {
|
|
29
|
-
case
|
|
30
|
-
console.log(chalk.green(`${data.padEnd(formattingOffset, ".")}[${statusMessage}]`));
|
|
29
|
+
case SUCCESS:
|
|
30
|
+
console.log(chalk.green(`${data.padEnd(formattingOffset, ".")}[${statusMessage}]`) + `[${path && chalk.dim(path)}]`);
|
|
31
31
|
break;
|
|
32
|
-
case
|
|
33
|
-
console.log(chalk.greenBright(`${data.padEnd(formattingOffset, ".")}[${statusMessage}]`));
|
|
32
|
+
case SKIP:
|
|
33
|
+
console.log(chalk.greenBright(`${data.padEnd(formattingOffset, ".")}[${statusMessage}]`) + `[${path && chalk.dim(path)}]`);
|
|
34
34
|
break;
|
|
35
|
-
case
|
|
35
|
+
case ERROR:
|
|
36
36
|
console.log(chalk.red(`${data.padEnd(formattingOffset, ".")}[${statusMessage}]`));
|
|
37
37
|
break;
|
|
38
38
|
}
|
|
@@ -90,7 +90,7 @@ const replacePageOrderableContainerImports = (fileData) => {
|
|
|
90
90
|
}
|
|
91
91
|
|
|
92
92
|
const addDefaultExport = (data, indexFileData, indexExportStrategy) => {
|
|
93
|
-
if(themeToolkitVersion >= FEATURE_AVAILABLE_SINCE_TOOLKIT_VERSION) {
|
|
93
|
+
if(themeToolkitVersion && themeToolkitVersion >= FEATURE_AVAILABLE_SINCE_TOOLKIT_VERSION) {
|
|
94
94
|
const { name, pathToCopyTo } = data;
|
|
95
95
|
const updatedPath = pathToCopyTo.split('customComponents')[1].replace(/.jsx?/g, '');
|
|
96
96
|
const template = `\nexport { default as ${name} } from '.${updatedPath}'`;
|
|
@@ -119,8 +119,8 @@ const addDefaultExport = (data, indexFileData, indexExportStrategy) => {
|
|
|
119
119
|
}
|
|
120
120
|
}
|
|
121
121
|
}
|
|
122
|
-
if (!componentAlreadyExported) {
|
|
123
|
-
return indexFileData + template;
|
|
122
|
+
if (!componentAlreadyExported && indexExportStrategy !== INDEX_EXPORT_NO_CHANGE) {
|
|
123
|
+
return indexFileData.trim() + template;
|
|
124
124
|
}
|
|
125
125
|
};
|
|
126
126
|
|
|
@@ -132,13 +132,7 @@ const sfbThemeComponentsDeepCopy = (
|
|
|
132
132
|
maxComponentStringLength,
|
|
133
133
|
indexExportStrategy = INDEX_EXPORT_NO_CHANGE
|
|
134
134
|
) => {
|
|
135
|
-
|
|
136
|
-
const NO_MATCH = 'NO_MATCH';
|
|
137
|
-
const SUCCESS = 'success';
|
|
138
|
-
const FOUND = 'FOUND';
|
|
139
|
-
const ERROR = 'error';
|
|
140
|
-
const SKIP = 'skip';
|
|
141
|
-
|
|
135
|
+
|
|
142
136
|
const componentsToCopy = pathsToFileList;
|
|
143
137
|
const paths = [...Object.keys(pathsToFileList).reverse()];
|
|
144
138
|
const allPaths = [...Object.keys(pathsToFileList)];
|
|
@@ -161,7 +155,8 @@ const sfbThemeComponentsDeepCopy = (
|
|
|
161
155
|
data: componentBeingCopied.name,
|
|
162
156
|
statusMessage: COPIED,
|
|
163
157
|
statusType: SUCCESS,
|
|
164
|
-
maxComponentStringLength
|
|
158
|
+
maxComponentStringLength,
|
|
159
|
+
path: componentBeingCopied.pathToCopyTo
|
|
165
160
|
});
|
|
166
161
|
indexFileData = addDefaultExport(componentBeingCopied, indexFileData);
|
|
167
162
|
}
|
|
@@ -175,8 +170,7 @@ const sfbThemeComponentsDeepCopy = (
|
|
|
175
170
|
continue;
|
|
176
171
|
}
|
|
177
172
|
|
|
178
|
-
const pathToCopyTo = path
|
|
179
|
-
`customComponents/sfb_theme_components${sfbThemeComponentsVersion}`);
|
|
173
|
+
const pathToCopyTo = getPathToCopyTo(path);
|
|
180
174
|
|
|
181
175
|
if(!fs.existsSync(pathToCopyTo)) {
|
|
182
176
|
const folderPath = pathToCopyTo.slice(0, pathToCopyTo.lastIndexOf('/'));
|
|
@@ -191,7 +185,8 @@ const sfbThemeComponentsDeepCopy = (
|
|
|
191
185
|
data: componentBeingCopied.name,
|
|
192
186
|
statusMessage: COPIED,
|
|
193
187
|
statusType: SUCCESS,
|
|
194
|
-
maxComponentStringLength
|
|
188
|
+
maxComponentStringLength,
|
|
189
|
+
path: componentBeingCopied.pathToCopyTo
|
|
195
190
|
});
|
|
196
191
|
indexFileData = addDefaultExport(componentBeingCopied, indexFileData, indexExportStrategy);
|
|
197
192
|
|
|
@@ -209,17 +204,34 @@ const sfbThemeComponentsDeepCopy = (
|
|
|
209
204
|
data: componentBeingCopied.name,
|
|
210
205
|
statusMessage: COPIED,
|
|
211
206
|
statusType: SUCCESS,
|
|
212
|
-
maxComponentStringLength
|
|
207
|
+
maxComponentStringLength,
|
|
208
|
+
path: componentBeingCopied.pathToCopyTo
|
|
213
209
|
});
|
|
214
210
|
indexFileData = addDefaultExport(componentBeingCopied, indexFileData, indexExportStrategy);
|
|
215
211
|
componentBeingCopied = {};
|
|
216
212
|
}
|
|
213
|
+
|
|
217
214
|
formattedComponentStatus({
|
|
218
215
|
data: componentsToCopy[path],
|
|
219
216
|
statusMessage: FOUND,
|
|
220
217
|
statusType: SKIP,
|
|
221
|
-
maxComponentStringLength
|
|
218
|
+
maxComponentStringLength,
|
|
219
|
+
path: pathToCopyTo
|
|
222
220
|
});
|
|
221
|
+
|
|
222
|
+
const foundComponentPattern = new RegExp(
|
|
223
|
+
`export.*\\s${componentsToCopy[path]}[\\s|}].*`
|
|
224
|
+
+ `${sfbThemeComponentsVersionCustom}.*${componentsToCopy[path]}.*`, 'g');
|
|
225
|
+
const foundComponent = indexFileData.match(foundComponentPattern);
|
|
226
|
+
|
|
227
|
+
if(!foundComponent) {
|
|
228
|
+
const foundComponentData = {
|
|
229
|
+
name: componentsToCopy[path],
|
|
230
|
+
pathToCopyTo: getPathToCopyTo(path)
|
|
231
|
+
}
|
|
232
|
+
indexFileData = addDefaultExport(foundComponentData, indexFileData, indexExportStrategy);
|
|
233
|
+
|
|
234
|
+
}
|
|
223
235
|
}
|
|
224
236
|
};
|
|
225
237
|
|
|
@@ -251,20 +263,35 @@ const sfbThemeComponentsDeepCopy = (
|
|
|
251
263
|
data: componentBeingCopied.name,
|
|
252
264
|
statusMessage: COPIED,
|
|
253
265
|
statusType: SUCCESS,
|
|
254
|
-
maxComponentStringLength
|
|
266
|
+
maxComponentStringLength,
|
|
267
|
+
path: componentBeingCopied.pathToCopyTo
|
|
255
268
|
});
|
|
256
269
|
indexFileData = addDefaultExport(componentBeingCopied, indexFileData, indexExportStrategy);
|
|
257
270
|
}
|
|
258
271
|
}
|
|
259
272
|
if(indexFileDataOriginalLength < indexFileData.length || indexExportStrategy === INDEX_EXPORT_REPLACE) {
|
|
260
|
-
fs.writeFileSync(PATH_TO_CUSTOM_COMPONENTS_INDEX, `${indexFileData}`);
|
|
273
|
+
fs.writeFileSync(PATH_TO_CUSTOM_COMPONENTS_INDEX, `${indexFileData.trim()}\n`);
|
|
261
274
|
}
|
|
262
275
|
}
|
|
263
276
|
|
|
264
277
|
(async () => {
|
|
265
278
|
try {
|
|
266
|
-
|
|
279
|
+
const isCustomComponentsEnabled = await customComponentsEnabled();
|
|
280
|
+
let indexExportStrategy;
|
|
281
|
+
|
|
282
|
+
if(isCustomComponentsEnabled) {
|
|
267
283
|
const componentMap = {};
|
|
284
|
+
|
|
285
|
+
const sfbThemeComponentsVersion = getSfbThemeComponentsVersion().replace('^', '');
|
|
286
|
+
sfbThemeComponentsVersionCustom = sfbThemeComponentsVersion.replaceAll('.', '_');
|
|
287
|
+
|
|
288
|
+
if(sfbThemeComponentsVersion < FEATURE_AVAILABLE_SINCE_SFB_THEME_VERSION) {
|
|
289
|
+
return console.log(chalk.red(
|
|
290
|
+
`[ERROR] This feature is only available starting from sfb-theme-components version: `
|
|
291
|
+
+ `${chalk.green(FEATURE_AVAILABLE_SINCE_SFB_THEME_VERSION)}`
|
|
292
|
+
));
|
|
293
|
+
}
|
|
294
|
+
|
|
268
295
|
const componentsFile = fs.readFileSync(BASE_PATH_TO_COMPONENTS_INDEX, 'utf8').replace(/\/\/+.*\s*/g, '');
|
|
269
296
|
const componentMatches = [...componentsFile.matchAll(/.*as\s+(\w+)\s+.*from\s+['|"](\.+\/.+)['|"]/g)];
|
|
270
297
|
let maxComponentStringLength = 0;
|
|
@@ -281,9 +308,13 @@ const sfbThemeComponentsDeepCopy = (
|
|
|
281
308
|
};
|
|
282
309
|
}
|
|
283
310
|
})
|
|
284
|
-
|
|
285
|
-
if(
|
|
286
|
-
|
|
311
|
+
|
|
312
|
+
if(process.argv[2] !== 'false') {
|
|
313
|
+
const data = Object.entries(JSON.parse(process.argv[2]));
|
|
314
|
+
if(data.length > 0) {
|
|
315
|
+
indexExportStrategy = data[0][0];
|
|
316
|
+
selectedComponentsList = data[0][1].sort();
|
|
317
|
+
}
|
|
287
318
|
} else {
|
|
288
319
|
const selectedComponents = await inquirer.prompt([{
|
|
289
320
|
type: 'checkbox',
|
|
@@ -295,25 +326,27 @@ const sfbThemeComponentsDeepCopy = (
|
|
|
295
326
|
}
|
|
296
327
|
|
|
297
328
|
const selectedComponentsFilePaths = {};
|
|
298
|
-
|
|
329
|
+
if(!selectedComponentsList || selectedComponentsList.length === 0) {
|
|
330
|
+
console.log(chalk.cyanBright(`[INFO] No components were selected.`));
|
|
331
|
+
return;
|
|
332
|
+
}
|
|
333
|
+
|
|
299
334
|
selectedComponentsList.forEach(component => {
|
|
300
335
|
const hasComponent = componentMap[component.toLowerCase()];
|
|
301
336
|
if(hasComponent) {
|
|
302
|
-
const componentPathSfb = constructPath(BASE_PATH_TO_COMPONENTS_INDEX, hasComponent.path);
|
|
337
|
+
const componentPathSfb = constructPath(BASE_PATH_TO_COMPONENTS_INDEX, hasComponent.path)?.replaceAll("\\", "/");
|
|
303
338
|
selectedComponentsFilePaths[componentPathSfb] = hasComponent.componentExportName;
|
|
304
339
|
} else {
|
|
305
340
|
// account for user passed arguments/components that do not exist
|
|
306
341
|
maxComponentStringLength = maxComponentStringLength < component.length
|
|
307
342
|
? component.length
|
|
308
343
|
: maxComponentStringLength;
|
|
309
|
-
const componentPathSfb = constructPath(BASE_PATH_TO_COMPONENTS_INDEX, component);
|
|
344
|
+
const componentPathSfb = constructPath(BASE_PATH_TO_COMPONENTS_INDEX, component)?.replaceAll("\\", "/");
|
|
310
345
|
selectedComponentsFilePaths[componentPathSfb] = component;
|
|
311
346
|
}
|
|
312
347
|
});
|
|
313
348
|
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
if(themeToolkitVersion >= FEATURE_AVAILABLE_SINCE_TOOLKIT_VERSION) {
|
|
349
|
+
if(themeToolkitVersion && themeToolkitVersion >= FEATURE_AVAILABLE_SINCE_TOOLKIT_VERSION) {
|
|
317
350
|
while(!indexExportStrategy) {
|
|
318
351
|
indexExportStrategy = (await inquirer.prompt({
|
|
319
352
|
type: 'list',
|
|
@@ -346,11 +379,10 @@ const sfbThemeComponentsDeepCopy = (
|
|
|
346
379
|
}
|
|
347
380
|
}
|
|
348
381
|
}
|
|
349
|
-
|
|
350
382
|
sfbThemeComponentsDeepCopy(selectedComponentsFilePaths, maxComponentStringLength, indexExportStrategy);
|
|
351
383
|
|
|
352
384
|
} else {
|
|
353
|
-
console.log(
|
|
385
|
+
console.log(COMPONENTS_NOT_INSTALLED_ERROR);
|
|
354
386
|
}
|
|
355
387
|
} catch (err) {
|
|
356
388
|
console.error(chalk.red(`\n ERROR: Unable to copy components due to: ${err}.\n`));
|