@pega/dx-component-builder-sdk 0.1.23 → 0.1.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.
@@ -0,0 +1,218 @@
1
+ import path from 'path';
2
+ import fs from 'fs';
3
+ import { promisify } from 'util';
4
+ import { join } from 'path';
5
+
6
+ import inquirer from 'inquirer';
7
+ import ncp from 'ncp';
8
+ import chalk from 'chalk';
9
+ import { updateComponentMap } from '../map/map.js';
10
+
11
+ import { checkPathAccess, showVersion, getPegaConfig } from '../../util.js';
12
+ import {
13
+ getComponentDirectoryPath,
14
+ getComponents,
15
+ getComponentDefaults
16
+ } from '../../util.js';
17
+
18
+ import { COMPONENT_SCHEMA,
19
+ CATEGORY_OVERRIDE,
20
+ SDK_CONFIG_JSON_FILENAME,
21
+ DXCB_CONFIG,
22
+ DXCB_COMPONENT_DEFAULTS,
23
+ CUSTOM_TEMPLATES,
24
+ OVERRIDE_TEMPLATE_PATH,
25
+ OVERRIDE_LIB_PATH } from '../../constants.js';
26
+ import { compileMustacheTemplates } from '@pega/dx-component-builder-sdk/src/tasks/create/index.js';
27
+
28
+ const pegaConfigJsonPath = path.join(path.resolve(), SDK_CONFIG_JSON_FILENAME);
29
+
30
+ const copy = promisify(ncp);
31
+
32
+ const copyComponentTemplate = async options => {
33
+ return copy(options.overrideDirectory, options.targetDirectory, {
34
+ clobber: false
35
+ });
36
+ };
37
+
38
+
39
+ const checkAndCreateConfigComponentKey = async (targetDirectory) => {
40
+
41
+ const configFile = path.join(targetDirectory, "config-ext.json");
42
+ try {
43
+ const configData = fs.readFileSync(path.resolve(configFile), 'utf8');
44
+ let configObj = JSON.parse(configData);
45
+
46
+ let writeUpdate = false;
47
+ if (!configObj.componentKey || !configObj.label) {
48
+ const name = configObj.name;
49
+
50
+ if (!configObj.componentKey) {
51
+ const componentKey = name;
52
+ configObj = { componentKey, ...configObj };
53
+ }
54
+
55
+ if (!configObj.label) {
56
+ const label = name;
57
+ configObj = { label, ...configObj };
58
+ }
59
+
60
+
61
+ // stringify "4", makes the json string look like JSON in the file, formated instead of a single line
62
+ const configContent = JSON.stringify(configObj, null, 4);
63
+
64
+ // update file
65
+ fs.writeFileSync(configFile, configContent, { encoding: 'utf8' });
66
+ }
67
+
68
+ }
69
+ catch (err) {
70
+ // no config-ext.json file, so no update, which is ok
71
+ const arPaths = targetDirectory.split("/");
72
+ const compName = arPaths[arPaths.length-1] ;
73
+ console.log(chalk.yellow(`Component ${compName} has no config file for extension.`));
74
+ }
75
+ }
76
+
77
+
78
+ const getListAvailableComponents = async (type) => {
79
+ const components = await getComponents(false, CATEGORY_OVERRIDE);
80
+ const fullList = COMPONENT_SCHEMA.overrideComponent[type];
81
+ let availList = [];
82
+ for (var el in fullList) {
83
+
84
+ let listEl = fullList[el].value;
85
+
86
+ if (listEl.indexOf("/") >=0) {
87
+ const arKey = listEl.split("/");
88
+ listEl = arKey[arKey.length - 1];
89
+ }
90
+
91
+ if (components.includes(listEl)) {
92
+ // currently don't show, because too many separators inquirer flaky
93
+ //availList.push(new inquirer.Separator("** " + fullList[el].name));
94
+ }
95
+ else {
96
+ availList.push(fullList[el]);
97
+ }
98
+ }
99
+
100
+ return availList;
101
+ }
102
+
103
+
104
+ const validateCompile = async (
105
+ {
106
+ componentName,
107
+ type
108
+ },
109
+ options
110
+ ) => {
111
+
112
+ let pegaConfig =
113
+ await getPegaConfig();
114
+ let templateParentDir = OVERRIDE_LIB_PATH;
115
+
116
+ if (pegaConfig[DXCB_CONFIG] &&
117
+ pegaConfig[DXCB_CONFIG][CUSTOM_TEMPLATES] &&
118
+ pegaConfig[DXCB_CONFIG][CUSTOM_TEMPLATES][OVERRIDE_TEMPLATE_PATH] &&
119
+ pegaConfig[DXCB_CONFIG][CUSTOM_TEMPLATES][OVERRIDE_TEMPLATE_PATH] != "") {
120
+ templateParentDir = pegaConfig[DXCB_CONFIG][CUSTOM_TEMPLATES][OVERRIDE_TEMPLATE_PATH];
121
+ }
122
+
123
+ // componentName has sub directory if components are in a sub directory of main "type"
124
+ const overrideDirectory = `${templateParentDir}/${type}/${componentName}`;
125
+
126
+ await checkPathAccess(overrideDirectory);
127
+
128
+ let componentKey = componentName;
129
+ if (componentKey.indexOf("/") >= 0) {
130
+ componentKey = componentKey.split("/")[1];
131
+ }
132
+ const typeLC = type.toLowerCase();
133
+ const targetDirectory = await getComponentDirectoryPath(join(typeLC, "/",componentKey), CATEGORY_OVERRIDE);
134
+
135
+ const components = await getComponents(false, CATEGORY_OVERRIDE);
136
+ if (components.includes(componentKey)) {
137
+ console.log(chalk.red(`${componentKey} component already exists in ${targetDirectory}`));
138
+ return;
139
+ }
140
+
141
+ await copyComponentTemplate({
142
+ ...options,
143
+ targetDirectory,
144
+ overrideDirectory
145
+ });
146
+
147
+ await checkAndCreateConfigComponentKey(targetDirectory);
148
+
149
+
150
+ console.log(chalk.green(`> Created ${componentKey} component in ${targetDirectory}`));
151
+
152
+ const componentKeyWithType = join(type, componentKey);
153
+ await updateComponentMap(componentKeyWithType, CATEGORY_OVERRIDE);
154
+
155
+ console.log(chalk.green(`> Added ${componentKeyWithType} to map`));
156
+
157
+ };
158
+
159
+ const buildOverridComponents = async (type, compList, options) => {
160
+ if (compList.length > 0) {
161
+ for (var index in compList) {
162
+ let componentName = compList[index].value;
163
+ await validateCompile(
164
+ {
165
+ componentName,
166
+ type
167
+ },
168
+ options
169
+ );
170
+ }
171
+ }
172
+ else {
173
+ console.log(chalk.green(`> All ${type} components have already been created.`));
174
+ }
175
+
176
+
177
+ }
178
+
179
+ export default async options => {
180
+ showVersion();
181
+
182
+ await checkPathAccess(pegaConfigJsonPath);
183
+
184
+ let data = fs.readFileSync(pegaConfigJsonPath, { encoding: 'utf8' });
185
+ data = data && JSON.parse(data);
186
+
187
+ if (!data[DXCB_CONFIG] || !data[DXCB_CONFIG][DXCB_COMPONENT_DEFAULTS]) {
188
+ console.error(
189
+ `${chalk.red.bold('ERROR')} Could not able find components defaults path in config.json`
190
+ );
191
+ process.exit(1);
192
+ }
193
+
194
+ const componentData = data[DXCB_CONFIG][DXCB_COMPONENT_DEFAULTS];
195
+
196
+ const { organization, library} = componentData;
197
+
198
+ let componentDefaults = await getComponentDefaults();
199
+ componentDefaults.library = library;
200
+
201
+ const fieldList = await getListAvailableComponents("field");
202
+ const desList = await getListAvailableComponents("designSystemExtension");
203
+ const templateList = await getListAvailableComponents("template");
204
+ const widgetList = await getListAvailableComponents("widget");
205
+ const infraList = await getListAvailableComponents("infra");
206
+
207
+
208
+ await buildOverridComponents("field", fieldList, options);
209
+ await buildOverridComponents("template", templateList, options);
210
+ await buildOverridComponents("widget", widgetList, options);
211
+ await buildOverridComponents("designSystemExtension", desList, options);
212
+ await buildOverridComponents("infra", infraList, options);
213
+
214
+
215
+
216
+ };
217
+
218
+
@@ -31,6 +31,12 @@ export const mapConfigJsonToCompanion = async ( componentKey ) => {
31
31
 
32
32
  }
33
33
 
34
+ export const removeCompanionConfigJson = async ( componentKey ) => {
35
+ const companionDirectory = await getComponentDirectoryPath(componentKey, CATEGORY_CONSTELLATION);
36
+
37
+ fs.rmSync(`${companionDirectory}/config.json`, { force: true });
38
+ }
39
+
34
40
  export const getBuildCategory = async (options) => {
35
41
  const categoryType = getOptionsCategory(options);
36
42
 
@@ -14,7 +14,8 @@ import { getPublishComponentQuestions,
14
14
  publishComponentToServer,
15
15
  zipComponent,
16
16
  getBuildCategory,
17
- mapConfigJsonToCompanion } from './helper.js';
17
+ mapConfigJsonToCompanion,
18
+ removeCompanionConfigJson} from './helper.js';
18
19
 
19
20
 
20
21
  export default async options => {
@@ -95,6 +96,12 @@ export default async options => {
95
96
  task: async () => {
96
97
  await publishComponentToServer(content);
97
98
  }
99
+ },
100
+ {
101
+ title: 'Clean up companion',
102
+ task: async () => {
103
+ removeCompanionConfigJson(componentKey);
104
+ }
98
105
  }
99
106
  ],
100
107
  {
@@ -31,6 +31,13 @@ export const mapConfigJsonToCompanion = async ( componentKey ) => {
31
31
 
32
32
  }
33
33
 
34
+ export const removeCompanionConfigJson = async ( componentKey ) => {
35
+ const companionDirectory = await getComponentDirectoryPath(componentKey, CATEGORY_CONSTELLATION);
36
+
37
+ fs.rmSync(`${companionDirectory}/config.json`, { force: true });
38
+ }
39
+
40
+
34
41
  export const getBuildCategory = async (options) => {
35
42
  const categoryType = getOptionsCategory(options);
36
43
 
@@ -14,7 +14,8 @@ import { getPublishComponentQuestions,
14
14
  publishComponentToServer,
15
15
  zipComponent,
16
16
  getBuildCategory,
17
- mapConfigJsonToCompanion } from './helper.js';
17
+ mapConfigJsonToCompanion,
18
+ removeCompanionConfigJson } from './helper.js';
18
19
 
19
20
 
20
21
 
@@ -94,6 +95,12 @@ export default async options => {
94
95
  task: async () => {
95
96
  await publishComponentToServer(content);
96
97
  }
98
+ },
99
+ {
100
+ title: 'Clean up companion',
101
+ task: async () => {
102
+ removeCompanionConfigJson(componentKey);
103
+ }
97
104
  }
98
105
  ],
99
106
  {
package/src/util.js CHANGED
@@ -19,7 +19,6 @@ import {
19
19
  OOTB_COMPONENT_SERVICE_REST_ENDPOINT,
20
20
  DELETE_COMPONENT_SERVICE_REST_ENDPOINT,
21
21
  OOTB_COMPONENTS,
22
- TOKEN_URL_V1,
23
22
  FIELD_PATH,
24
23
  WIDGET_PATH,
25
24
  TEMPLATE_PATH,
@@ -614,7 +613,3 @@ export const getOOTBComponents = async () => {
614
613
  });
615
614
  });
616
615
  };
617
-
618
- export const getTokenUrl = async (server) => {
619
- return server.endsWith('/') ? `${server}${TOKEN_URL_V1}` : `${server}/${TOKEN_URL_V1}`;
620
- };