@pega/custom-dx-components 0.25.13 → 0.25.15

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.
@@ -5,12 +5,14 @@ import fetch from 'node-fetch';
5
5
  import archiver from 'archiver';
6
6
  import path from 'path';
7
7
  import fs from 'fs';
8
+ import ora from 'ora';
8
9
 
9
10
  import validate from '../validator/index.js';
10
11
  import { lintComponent } from '../linter/index.js';
11
12
  import { constructCompleteUrl, getComponentDirectoryPath, getHttpsAgent, getPegaServerConfig, showVersion, getC11NB2STokenAndStaticServer, getLocalLibraryVersions, getLibraryBased, getLibraryBasedCL, deletePublishExtras, addDebugLog, checkLibraryAndArchives, checkJWTExpiration, getConfigDefaults } from '../../util.js';
12
13
 
13
14
  import { publishLib, listLibJSONResponse, deleteLibVersion } from '@pega/constellation-dx-components-build-utils/index.js';
15
+ import { default as buildLibrary } from '../build-lib/index.js';
14
16
 
15
17
  import { getLibraryVersionQuestion } from './helper.js';
16
18
 
@@ -165,6 +167,11 @@ export const publishComponentLibraryToServer = async (data, doFetch) => {
165
167
  headers.Authorization = `${tokenType} ${accessToken}`;
166
168
  }
167
169
 
170
+ let spinner;
171
+
172
+ console.log();
173
+ spinner = ora(chalk.green.bold('Publishing library...')).start();
174
+
168
175
  const response = await fetch(url, {
169
176
  method: 'POST',
170
177
  agent: getHttpsAgent(defaultPegaConfig),
@@ -172,6 +179,8 @@ export const publishComponentLibraryToServer = async (data, doFetch) => {
172
179
  body: JSON.stringify(apiBody)
173
180
  });
174
181
 
182
+ spinner.stop();
183
+
175
184
  status = response.status;
176
185
  if (!response.ok) {
177
186
  if (status === 401) {
@@ -192,18 +201,25 @@ export const publishComponentLibraryToServer = async (data, doFetch) => {
192
201
  }
193
202
 
194
203
  try {
195
- const respData = isLaunchpad ? await response.text() : await response.json();
196
- console.log(chalk.bold.green(`Success : ${isLaunchpad ? respData : respData.message}`));
204
+ // const respData = isLaunchpad ? await response.text() : await response.json();
205
+ const headerType = response.headers.get('content-type');
206
+ const respData = headerType.includes("json") ? await response.json() : await response.text();
207
+ let respMessage = headerType.includes("json") ? respData.message : respData;
208
+
197
209
 
198
210
  if (status === 500) {
199
211
  status = 999;
200
- throw new Error(respData.message);
212
+ if (respMessage === '') {
213
+ respMessage = `Server error, please check server logs for error.`;
214
+ }
215
+ throw new Error(respMessage);
201
216
  }
217
+
218
+ console.log(chalk.bold.green(`Success : ${respMessage }`));
219
+
202
220
  } catch (err) {
203
221
  if (status === 500) {
204
- throw new Error(
205
- `Error occurred in authentication. Please regenerate using authenticate`
206
- );
222
+ throw new Error(`Server error, please check server logs for error.`);
207
223
  } else if (status === 999) {
208
224
  throw new Error(err);
209
225
  }
@@ -417,62 +433,64 @@ addDebugLog("publishLibrary", "", "+");
417
433
  return;
418
434
  }
419
435
 
420
- if (okToPublish) {
421
-
422
- // /*
423
-
424
- let libVersionExists = await doesLibraryExist(libraryName, version);
425
- let overrideExisting = false;
426
- let checkDeleted = false;
427
436
 
428
- await deletePublishExtras(libraryName, version);
437
+ const isLibraryBasedCL = getLibraryBasedCL();
438
+ if (okToPublish) {
439
+ if (isLibraryBasedCL) {
440
+ // developer test
429
441
 
442
+ let content = { libraryName, version, rulesetName, rulesetVersion };
443
+ await publishUILibraryComponentToInfinity(content);
430
444
 
431
- if (libVersionExists) {
432
- console.log("\nLibrary " + chalk.bold.yellow(`${libraryName}/${version}`) + " already exists.");
433
- const questions = [
434
- {
435
- name: 'override',
436
- type: 'confirm',
437
- message: `Do you wish to overrite it`,
438
- default: false
439
- }
440
- ];
441
445
 
442
- await inquirer.prompt(questions).then(async answers => {
443
- overrideExisting = answers.override;
444
- });
445
446
  }
446
-
447
- if (libVersionExists) {
448
- if (overrideExisting) {
449
- console.log("\nDeleting existing library " + chalk.bold.yellow(`${libraryName}:${version}`) + " ...");
450
- addDebugLog("deleteLibVersion", `services call: libraryName: ${libraryName}, version: ${version}`, "");
451
- await deleteLibVersion(libraryName, version, tokenAndStaticServer.C11NB2S, tokenAndStaticServer.appStaticContentServer);
452
- addDebugLog("deleteLibVersion", "services end", "");
453
- checkDeleted = true;
447
+ else {
448
+ let libVersionExists = await doesLibraryExist(libraryName, version);
449
+ let overrideExisting = false;
450
+ let checkDeleted = false;
451
+
452
+ await deletePublishExtras(libraryName, version);
453
+
454
+
455
+ if (libVersionExists) {
456
+ console.log("\nLibrary " + chalk.bold.yellow(`${libraryName}/${version}`) + " already exists.");
457
+ const questions = [
458
+ {
459
+ name: 'override',
460
+ type: 'confirm',
461
+ message: `Do you wish to overrite it`,
462
+ default: false
463
+ }
464
+ ];
465
+
466
+ await inquirer.prompt(questions).then(async answers => {
467
+ overrideExisting = answers.override;
468
+ });
454
469
  }
455
- else {
456
- // don't override, so end
457
- console.log(chalk.yellow(`You will need to increment your library version to publish`));
458
- addDebugLog("publishLibrary", "END", "-");
459
- return;
460
- }
461
- }
462
470
 
463
- // need a timeout to give services time to delete the library
464
- // setTimeout( function(){ await publishNewLibraryAsync(libraryName, version, checkDeleted)}, 700 );
465
- await publishNewLibraryAsync(libraryName, version, checkDeleted);
466
- // */
471
+ if (libVersionExists) {
472
+ if (overrideExisting) {
473
+ console.log("\nDeleting existing library " + chalk.bold.yellow(`${libraryName}:${version}`) + " ...");
474
+ addDebugLog("deleteLibVersion", `services call: libraryName: ${libraryName}, version: ${version}`, "");
475
+ await deleteLibVersion(libraryName, version, tokenAndStaticServer.C11NB2S, tokenAndStaticServer.appStaticContentServer);
476
+ addDebugLog("deleteLibVersion", "services end", "");
477
+ checkDeleted = true;
478
+ }
479
+ else {
480
+ // don't override, so end
481
+ console.log(chalk.yellow(`You will need to increment your library version to publish`));
482
+ addDebugLog("publishLibrary", "END", "-");
483
+ return;
484
+ }
485
+ }
467
486
 
468
- // developer test
469
- const isLibraryBasedCL = getLibraryBasedCL();
470
- if (isLibraryBasedCL) {
471
- let content = { libraryName, version, rulesetName, rulesetVersion };
487
+ // need a timeout to give services time to delete the library
488
+ // setTimeout( function(){ await publishNewLibraryAsync(libraryName, version, checkDeleted)}, 700 );
489
+ await publishNewLibraryAsync(libraryName, version, checkDeleted);
472
490
 
473
- await publishUILibraryComponentToInfinity(content);
474
491
  }
475
492
 
493
+
476
494
  }
477
495
 
478
496
  }
@@ -19,7 +19,8 @@ import {
19
19
  updateDefaultOrganization,
20
20
  addDebugLog,
21
21
  checkLibraryAndArchives,
22
- getConfigDefaults
22
+ getConfigDefaults,
23
+ getLibraryBasedCL
23
24
  } from '../../util.js';
24
25
  import {
25
26
  TASKS_CONFIG_JSON_FILENAME,
@@ -105,6 +106,8 @@ addDebugLog("renameLibrary", `library: ${library}, organization: ${organization}
105
106
  const currentLibrary = compDef.library;
106
107
  const currentVersion = compDef.version;
107
108
 
109
+ const isLibraryBasedCL = getLibraryBasedCL();
110
+
108
111
  let compVersion;
109
112
 
110
113
  const tokenAndStaticServer = await getC11NB2STokenAndStaticServer();
@@ -143,6 +146,12 @@ addDebugLog("renameLibrary", `library: ${library}, organization: ${organization}
143
146
  if (compVersion && compVersion.indexOf('-dev') != -1) {
144
147
  wasDevBuild = true;
145
148
  }
149
+
150
+ // no -dev with PegaInfinity
151
+ if (isLibraryBasedCL) {
152
+ wasDevBuild = false;
153
+ }
154
+
146
155
  const allowedApplications = configData.allowedApplications;
147
156
 
148
157
  await renameComponent(
package/src/util.js CHANGED
@@ -8,6 +8,7 @@ import { createRequire } from 'module';
8
8
  import {jwtDecode} from 'jwt-decode';
9
9
  import archiver from 'archiver';
10
10
  import {unarchive} from 'unarchive';
11
+ import ora from 'ora';
11
12
 
12
13
  import chalk from 'chalk';
13
14
  import mustache from 'mustache';
@@ -21,10 +22,12 @@ import {
21
22
  COMPONENTS_CONFIG_FILE_NAME,
22
23
  TASKS_CONFIG_JSON_FILENAME,
23
24
  PACKAGE_JSON_FILENAME,
25
+ PACKAGE_LOCK_JSON_FILENAME,
24
26
  TOKEN_PATH,
25
27
  OOTB_COMPONENT_SERVICE_REST_ENDPOINT,
26
28
  DELETE_COMPONENT_SERVICE_REST_ENDPOINT,
27
29
  LP_DELETE_COMPONENT_SERVICE_REST_ENDPOINT,
30
+ DELETE_COMPONENT_LIBRARY_SERVICE_REST_ENDPOINT,
28
31
  OOTB_COMPONENTS,
29
32
  USE_PROMOTED_WEB_PACK,
30
33
  BOOTSTRAP_CONFIG,
@@ -42,6 +45,7 @@ import {
42
45
  } from './constants.js';
43
46
 
44
47
 
48
+
45
49
  const access = promisify(fs.access);
46
50
 
47
51
  let showDebug = false;
@@ -288,19 +292,34 @@ export const applyPegaData = async (configJson) => {
288
292
  if (showDebug) addDebugLog("applyPegaData", "", "");
289
293
  const tokenAndStaticServer = await getC11NB2STokenAndStaticServer();
290
294
  const currentDirectory = process.cwd()
291
- const currentPackagePath = join(currentDirectory, PACKAGE_JSON_FILENAME);
292
- let currentPackageData;
295
+ // const currentPackagePath = join(currentDirectory, PACKAGE_JSON_FILENAME);
296
+ const currentPackageLockPath = join(currentDirectory, PACKAGE_LOCK_JSON_FILENAME);
297
+ // let currentPackageData;
298
+ let currentPackageLockData;
299
+
300
+ // try {
301
+ // currentPackageData = fs.readFileSync(currentPackagePath);
302
+ // }
303
+ // catch (ex) {
304
+ // console.log(chalk.red(`\nUnable to retrieve current package.json.`));
305
+ // process.exit(1);
306
+ // }
293
307
 
294
308
  try {
295
- currentPackageData = fs.readFileSync(currentPackagePath);
309
+ currentPackageLockData = fs.readFileSync(currentPackageLockPath);
296
310
  }
297
311
  catch (ex) {
298
- console.log($chalk.red(`\nUnable to retrieve current package.json.`))
312
+ console.log(chalk.red(`\nUnable to retrieve current package-lock.json.`));
299
313
  process.exit(1);
300
314
  }
301
315
 
302
- const cData = JSON.parse(currentPackageData);
303
- const cosmosVersion = cData.dependencies["@pega/cosmos-react-core"];
316
+ // const cData = JSON.parse(currentPackageData);
317
+ // const cosmosVersion = cData.dependencies["@pega/cosmos-react-core"];
318
+
319
+ const cData = JSON.parse(currentPackageLockData);
320
+ const cosmosReact = cData.packages["node_modules/@pega/cosmos-react-core"];
321
+ const cosmosVersion = cosmosReact.version;
322
+
304
323
  let infinityVersion = "";
305
324
 
306
325
  if (tokenAndStaticServer.C11NB2S === undefined) {
@@ -361,6 +380,20 @@ export const checkAccessTokenExpiration = async() => {
361
380
 
362
381
  }
363
382
 
383
+ export const getApplicationAndVersionFromToken = async() => {
384
+ const tokenAndStaticServer = await getC11NB2STokenAndStaticServer();
385
+
386
+ if (tokenAndStaticServer.access_token === undefined) {
387
+ console.log(chalk.redBright("Need to authenticate, missing token.\n"));
388
+ process.exit(1);
389
+ }
390
+
391
+ const jwt = jwtDecode(tokenAndStaticServer.access_token);
392
+ const { app_name, app_version } = jwt;
393
+ return { app_name, app_version };
394
+
395
+ }
396
+
364
397
  export const getLibraryBased = () => {
365
398
  if (showDebug) addDebugLog("getLibraryBased", "", "");
366
399
  // const currentDirectory = process.cwd();
@@ -1300,6 +1333,91 @@ if (showDebug) addDebugLog("deleteInfinityServerComponent", `componentKey: ${com
1300
1333
  }
1301
1334
  };
1302
1335
 
1336
+ export const deleteInfinityServerComponentLibrary = async (libraryName, rulesetName, rulesetVersion) => {
1337
+ if (showDebug) addDebugLog("deleteInfinityServerComponentLibrary", `componentKey: ${componentKey}, rulesetName: ${rulesetName}, rulesetVersion: ${rulesetVersion}`, "");
1338
+ const serverConfig = await getPegaServerConfig();
1339
+ const { server } = serverConfig;
1340
+ const url = constructCompleteUrl(server, DELETE_COMPONENT_LIBRARY_SERVICE_REST_ENDPOINT);
1341
+ const deleteUrl = `${url}/${libraryName}/rulesetname/${rulesetName}/rulesetversion/${rulesetVersion}`;
1342
+ try {
1343
+ const OauthData = fs.readFileSync(TOKEN_PATH, 'utf8');
1344
+
1345
+ let status = 500;
1346
+
1347
+ if (OauthData) {
1348
+ const { access_token: accessToken, token_type: tokenType } = JSON.parse(OauthData);
1349
+ let spinner;
1350
+
1351
+ console.log();
1352
+ spinner = ora(chalk.green.bold('Deleting component library...')).start();
1353
+
1354
+ const response = await fetch(deleteUrl, {
1355
+ method: 'DELETE',
1356
+ agent: getHttpsAgent(serverConfig),
1357
+ headers: {
1358
+ Authorization: `${tokenType} ${accessToken}`
1359
+ }
1360
+ });
1361
+
1362
+ spinner.stop();
1363
+ console.log();
1364
+
1365
+ status = response.status;
1366
+ if (!response.ok) {
1367
+ if (status === 401) {
1368
+ throw new Error(
1369
+ 'Error occurred in authentication. Please regenerate using authenticate'
1370
+ );
1371
+ // console.log(accessTokenUri, refreshToken);
1372
+ /* TODO - Handle refresh_token */
1373
+ }
1374
+ else if (status === 404){
1375
+ throw new Error('404: Server resource not found');
1376
+ }
1377
+ else if (status === 405){
1378
+ throw new Error('405: Server method not allowed');
1379
+ }
1380
+ else if (status === 408){
1381
+ throw new Error('408: Server timed out');
1382
+ }
1383
+ else if (response.status === 403) {
1384
+ throw new Error(
1385
+ 'Error forbidden: User does not have privileges to Publish.'
1386
+ );
1387
+ }
1388
+
1389
+ }
1390
+
1391
+ try {
1392
+ const respData = await response.json();
1393
+ console.log(
1394
+ chalk.bold.green(`${respData.message}`)
1395
+ );
1396
+
1397
+ if (status === 500) {
1398
+ status = 999;
1399
+ throw new Error(respData.message);
1400
+ }
1401
+ }
1402
+ catch (err) {
1403
+ if (status === 500) {
1404
+ throw new Error(
1405
+ `Error occurred in authentication. Please regenerate using authenticate`
1406
+ );
1407
+ }
1408
+ else if (status === 999) {
1409
+ throw new Error (err);
1410
+ }
1411
+ }
1412
+
1413
+ }
1414
+ } catch (error) {
1415
+
1416
+ throw new Error(error);
1417
+ }
1418
+ };
1419
+
1420
+
1303
1421
  export const deleteLaunchpadServerComponent = async componentKey => {
1304
1422
  if (showDebug) addDebugLog("deleteLaunchpadServerComponent", `componentKey: ${componentKey}`, "");
1305
1423
  const { server, isolationId } = await getPegaServerConfig();
@@ -1729,6 +1847,161 @@ if (showDebug) addDebugLog("zipVersionAndArchive", `orgLibName: ${orgLibName}, v
1729
1847
 
1730
1848
  };
1731
1849
 
1850
+ export const zipVersionAndArchiveAndReturnBuffer = async (orgLibName, version, currentDirectory = "") => {
1851
+ if (showDebug) addDebugLog("zipVersionAndArchive", `orgLibName: ${orgLibName}, version: ${version}`, "");
1852
+ // const srcDirectoryPath = await getSrcDirectoryPath();
1853
+ if (currentDirectory === "") {
1854
+ currentDirectory = process.cwd();
1855
+ }
1856
+ const srcDirectoryPath = join(currentDirectory, "src");
1857
+ const tasksConfig = join(currentDirectory, TASKS_CONFIG_JSON_FILENAME);
1858
+ const packageJson = join(currentDirectory, PACKAGE_JSON_FILENAME);
1859
+ const archiveFileName = `${orgLibName}_${version}.arch.zip`;
1860
+
1861
+
1862
+ // const configContent = Buffer.from(fs.readFileSync(configJson)).toString();
1863
+ const mainDirectory = process.cwd();
1864
+ const archiveDirectory = join (mainDirectory, ARCHIVES_PATH, `${orgLibName}`, `${version}`);
1865
+ if (!fs.existsSync(archiveDirectory)) {
1866
+ fs.mkdirSync(archiveDirectory, { recursive: true });
1867
+ }
1868
+
1869
+ // create componentsconfig.json
1870
+ await createComponentsConfigFile(archiveDirectory, join(srcDirectoryPath, "components"));
1871
+
1872
+ const archiveFile = join(archiveDirectory, archiveFileName);
1873
+ if (fs.existsSync(orgLibName)) {
1874
+
1875
+ }
1876
+
1877
+ const output = fs.createWriteStream(archiveFile);
1878
+ const archive = archiver('zip', {
1879
+ zlib: { level: 9 } // Sets the compression level.
1880
+ });
1881
+
1882
+ const zipChunks = [];
1883
+ archive.on('data', chunk => zipChunks.push(chunk));
1884
+
1885
+
1886
+ // Add src directory
1887
+ archive.directory(srcDirectoryPath, 'src');
1888
+
1889
+ // grab all files (not directories and archive)
1890
+ const configFiles = await getTopLevelConfigFiles(currentDirectory);
1891
+ if (configFiles && configFiles.length > 0) {
1892
+ for (const configF of configFiles) {
1893
+ if (configF !== "package-lock.json") {
1894
+ archive.file(
1895
+ join(currentDirectory, configF), { name: configF });
1896
+ }
1897
+
1898
+ }
1899
+ }
1900
+
1901
+ // pipe archive data to the file
1902
+ archive.pipe(output);
1903
+
1904
+ output.on('end', function() {
1905
+ console.log('Data has been drained');
1906
+ });
1907
+
1908
+
1909
+ // good practice to catch warnings (ie stat failures and other non-blocking errors)
1910
+ archive.on('warning', function(err) {
1911
+ if (err.code === 'ENOENT') {
1912
+ // log warning
1913
+ } else {
1914
+ // throw error
1915
+ throw err;
1916
+ }
1917
+ });
1918
+
1919
+ // good practice to catch this error explicitly
1920
+ archive.on('error', function(err) {
1921
+ throw err;
1922
+ });
1923
+
1924
+ await archive.finalize();
1925
+
1926
+ const zipBuffer = Buffer.concat(zipChunks);
1927
+ const zipContent = zipBuffer.toString('base64');
1928
+
1929
+ return { output, zipContent};
1930
+
1931
+ };
1932
+
1933
+ export const zipUnArchiveToTempAndReturnBuffer = async(orgLibName, version) => {
1934
+
1935
+ // unarchive to temp
1936
+ const currentDirectory = process.cwd();
1937
+ const tempDirectory = join (currentDirectory, TEMP_PATH);
1938
+ const srcDirectoryPath = join(tempDirectory, "src");
1939
+
1940
+ const archiveFileName = `${orgLibName}_${version}.arch.zip`;
1941
+ const archiveDirectory = join (currentDirectory, ARCHIVES_PATH, `${orgLibName}`, `${version}`);
1942
+ const archFileName = join(archiveDirectory, archiveFileName);
1943
+
1944
+ await unZipFromArchive(archFileName);
1945
+
1946
+ // archive is in temp directory
1947
+ const archiveFile = join(tempDirectory, archiveFileName);
1948
+
1949
+ const output = fs.createWriteStream(archiveFile);
1950
+ const archive = archiver('zip', {
1951
+ zlib: { level: 9 } // Sets the compression level.
1952
+ });
1953
+
1954
+ const zipChunks = [];
1955
+ archive.on('data', chunk => zipChunks.push(chunk));
1956
+
1957
+ // Add src directory
1958
+ archive.directory(srcDirectoryPath, 'src');
1959
+
1960
+ // grab all files (not directories and archive)
1961
+ const configFiles = await getTopLevelConfigFiles(tempDirectory);
1962
+ if (configFiles && configFiles.length > 0) {
1963
+ for (const configF of configFiles) {
1964
+ if (configF !== "package-lock.json") {
1965
+ archive.file(
1966
+ join(currentDirectory, configF), { name: configF });
1967
+ }
1968
+
1969
+ }
1970
+ }
1971
+
1972
+ // pipe archive data to the file
1973
+ archive.pipe(output);
1974
+
1975
+ output.on('end', function() {
1976
+ console.log('Data has been drained');
1977
+ });
1978
+
1979
+
1980
+ // good practice to catch warnings (ie stat failures and other non-blocking errors)
1981
+ archive.on('warning', function(err) {
1982
+ if (err.code === 'ENOENT') {
1983
+ // log warning
1984
+ } else {
1985
+ // throw error
1986
+ throw err;
1987
+ }
1988
+ });
1989
+
1990
+ // good practice to catch this error explicitly
1991
+ archive.on('error', function(err) {
1992
+ throw err;
1993
+ });
1994
+
1995
+ await archive.finalize();
1996
+
1997
+ const zipBuffer = Buffer.concat(zipChunks);
1998
+ const zipContent = zipBuffer.toString('base64');
1999
+
2000
+ return { output, zipContent};
2001
+
2002
+
2003
+ }
2004
+
1732
2005
  export const zipVersionAndArchiveCL = async (orgLibName, version, currentDirectory = "") => {
1733
2006
  if (showDebug) addDebugLog("zipVersionAndArchiveCL", `orgLibName: ${orgLibName}, version: ${version}, currentDirectory: ${currentDirectory}`, "");
1734
2007
  // const srcDirectoryPath = await getSrcDirectoryPath();