@pega/custom-dx-components 0.242.25 → 0.242.27

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.
Files changed (46) hide show
  1. package/package.json +2 -1
  2. package/src/cli.js +44 -2
  3. package/src/constants.js +2 -0
  4. package/src/tasks/build-all/helper.js +1 -1
  5. package/src/tasks/build-all/index.js +4 -3
  6. package/src/tasks/build-comp/helper.js +7 -4
  7. package/src/tasks/build-comp/index.js +4 -2
  8. package/src/tasks/build-jest-assets/index.js +1 -1
  9. package/src/tasks/build-lib/helper.js +2 -34
  10. package/src/tasks/build-lib/index.js +36 -85
  11. package/src/tasks/create/index.js +31 -19
  12. package/src/tasks/create-all/index.js +31 -16
  13. package/src/tasks/create-lib/helper.js +80 -0
  14. package/src/tasks/create-lib/index.js +109 -0
  15. package/src/tasks/create-version/helper.js +81 -0
  16. package/src/tasks/create-version/index.js +108 -0
  17. package/src/tasks/delete/index.js +7 -4
  18. package/src/tasks/delete-all/index.js +14 -9
  19. package/src/tasks/delete-lib/helper.js +19 -46
  20. package/src/tasks/delete-lib/index.js +107 -49
  21. package/src/tasks/delete-pub/helper.js +35 -0
  22. package/src/tasks/delete-pub/index.js +105 -0
  23. package/src/tasks/index.js +7 -0
  24. package/src/tasks/init/helper.js +5 -5
  25. package/src/tasks/init/template/package.json.mustache +15 -6
  26. package/src/tasks/list/helper.js +28 -13
  27. package/src/tasks/list/index.js +4 -9
  28. package/src/tasks/list-lib/helper.js +18 -53
  29. package/src/tasks/list-lib/index.js +68 -110
  30. package/src/tasks/list-pub/helper.js +42 -0
  31. package/src/tasks/list-pub/index.js +170 -0
  32. package/src/tasks/promote-webpack/index.js +1 -1
  33. package/src/tasks/publish/helper.js +44 -44
  34. package/src/tasks/publish/index.js +70 -5
  35. package/src/tasks/publish-all/index.js +10 -2
  36. package/src/tasks/publish-lib/helper.js +0 -38
  37. package/src/tasks/publish-lib/index.js +39 -13
  38. package/src/tasks/rename/index.js +25 -10
  39. package/src/tasks/rename-lib/index.js +8 -41
  40. package/src/tasks/revert-webpack/index.js +1 -1
  41. package/src/tasks/show-status/index.js +41 -0
  42. package/src/tasks/switch-lib/helper.js +81 -0
  43. package/src/tasks/switch-lib/index.js +134 -0
  44. package/src/tasks/switch-version/helper.js +92 -0
  45. package/src/tasks/switch-version/index.js +142 -0
  46. package/src/util.js +728 -59
package/src/util.js CHANGED
@@ -6,17 +6,21 @@ import https from 'https';
6
6
  import crypto from 'node:crypto';
7
7
  import { createRequire } from 'module';
8
8
  import {jwtDecode} from 'jwt-decode';
9
+ import archiver from 'archiver';
10
+ import {unarchive} from 'unarchive';
9
11
 
10
12
  import chalk from 'chalk';
11
13
  import mustache from 'mustache';
12
14
  import pascalCase from 'pascalcase';
13
15
  import fetch from 'node-fetch';
14
- import checkGit from 'check-git';
16
+ // import checkGit from 'check-git';
15
17
 
16
18
  import * as currentPath from './currentPath.cjs';
17
19
  import {
18
20
  COMPONENTS_DIRECTORY_PATH,
21
+ COMPONENTS_CONFIG_FILE_NAME,
19
22
  TASKS_CONFIG_JSON_FILENAME,
23
+ PACKAGE_JSON_FILENAME,
20
24
  TOKEN_PATH,
21
25
  OOTB_COMPONENT_SERVICE_REST_ENDPOINT,
22
26
  DELETE_COMPONENT_SERVICE_REST_ENDPOINT,
@@ -26,14 +30,34 @@ import {
26
30
  BOOTSTRAP_CONFIG,
27
31
  LIBRARY_BASED,
28
32
  SHARED_DIRECTORY,
29
- SHOW_DEBUG
33
+ SHOW_DEBUG,
34
+ COMPONENTS_PATH,
35
+ ARCHIVES_PATH
30
36
  } from './constants.js';
37
+ import { arch } from 'os';
38
+ import lib from 'fs-extra';
39
+ import { or } from 'ajv/dist/compile/codegen/index.js';
40
+ import def from 'ajv/dist/vocabularies/applicator/additionalItems.js';
31
41
 
32
42
  const access = promisify(fs.access);
33
43
 
34
44
  let showDebug = false;
35
45
  let debugLevel = 1;
36
-
46
+ let haveDependencyDifference = false;
47
+
48
+ // updated upon start
49
+ let defaultConfigs = {
50
+ serverType: "",
51
+ isLibraryBased: false,
52
+ devBuild: true,
53
+ library: "",
54
+ organization: "",
55
+ currentOrgLib: "",
56
+ version: "",
57
+ buildVersion: "",
58
+ displayLibVersion: "",
59
+ showDebug: false
60
+ };
37
61
 
38
62
  export const addDebugLog = (sFunctionName, sLogData, sIncDec) => {
39
63
 
@@ -60,6 +84,70 @@ export const addDebugLog = (sFunctionName, sLogData, sIncDec) => {
60
84
  }
61
85
  }
62
86
 
87
+ export const setUpDefaults = async () => {
88
+
89
+ // defaults, start up function will set these up
90
+ // so that won't have to continously hit files to get data
91
+ // thus saving time.
92
+ let retrievedConfigData = {
93
+ serverType: "",
94
+ isLibraryBased: false,
95
+ devBuild: true,
96
+ library: "",
97
+ organization: "",
98
+ currentOrgLib: "",
99
+ version: "",
100
+ buildVersion: "",
101
+ displayLibVersion: "",
102
+ showDebug: false
103
+ };
104
+
105
+ // retrievedConfigData
106
+ const currentDirectory = process.cwd();
107
+ const pegaConfigJsonPath = join(currentDirectory, TASKS_CONFIG_JSON_FILENAME);
108
+
109
+ try {
110
+ // force to throw error and not console log
111
+ await checkPathAccess(pegaConfigJsonPath, {}, true);
112
+
113
+ let data = fs.readFileSync(pegaConfigJsonPath, { encoding: 'utf8' });
114
+ data = JSON.parse(data);
115
+
116
+ if (data[LIBRARY_BASED]) {
117
+ retrievedConfigData.isLibraryBased = data[LIBRARY_BASED];
118
+ }
119
+ retrievedConfigData.library = data.component.library;
120
+ retrievedConfigData.version = data.component.version;
121
+
122
+ const { organization } = JSON.parse(fs.readFileSync(path.resolve('package.json'), 'utf8'));
123
+ retrievedConfigData.organization = organization;
124
+
125
+ if (data["showDebug"]) {
126
+ retrievedConfigData.showDebug = data.showDebug;
127
+ }
128
+
129
+ retrievedConfigData.showDebug = data["server-config"].devBuild;
130
+ retrievedConfigData.serverType = data["server-config"].serverType;
131
+
132
+ retrievedConfigData.buildVersion = retrievedConfigData.devBuild ? `${retrievedConfigData.version}-dev` : retrievedConfigData.version;
133
+ retrievedConfigData.currentOrgLib = `${retrievedConfigData.organization}_${retrievedConfigData.library}`;
134
+ retrievedConfigData.displayLibVersion = `${retrievedConfigData.currentOrgLib }/${retrievedConfigData.buildVersion}`;
135
+
136
+ }
137
+ catch (ex) {
138
+ // when can't find it, always be false, assume no library based.
139
+ // This is IMPORTANT for init (npx install)
140
+ return false;
141
+ }
142
+
143
+
144
+
145
+ return retrievedConfigData;
146
+ }
147
+
148
+ // initialize
149
+ await setUpDefaults();
150
+
63
151
  export const checkPathAccess = async (path, options = {}, throwError = false) => {
64
152
 
65
153
 
@@ -159,33 +247,52 @@ addDebugLog("getAboutData", `currentOrgLib: ${currentOrgLib}, buildVersion: ${bu
159
247
  return `${currentOrgLib} ${buildVersion}, ${dateStamp}, ${infinityVersion}`;
160
248
  }
161
249
 
162
- export const getLibraryBased = async () => {
163
- addDebugLog("getLibraryBased", "", "+");
164
- const currentDirectory = process.cwd();
165
- const pegaConfigJsonPath = join(currentDirectory, TASKS_CONFIG_JSON_FILENAME);
250
+ export const checkJWTExpiration = async () => {
251
+ const tokenAndStaticServer = await getC11NB2STokenAndStaticServer();
166
252
 
167
- try {
168
- // force to throw error and not console log
169
- await checkPathAccess(pegaConfigJsonPath, {}, true);
253
+ if (tokenAndStaticServer.C11NB2S === undefined) {
254
+ console.log(chalk.redBright("Need to authenticate, missing services token.\nBuilding a library requires authentication to acquire a token to build."));
255
+ process.exit(1);
170
256
  }
171
- catch (ex) {
172
- // when can't find it, always be false, assume no library based.
173
- // This is IMPORTANT for init (npx install)
174
- return false;
257
+
258
+ const jwt = jwtDecode(tokenAndStaticServer.C11NB2S);
259
+ const { exp } = jwt;
260
+ if (exp < (new Date().getTime() + 1) / 1000) {
261
+ console.log(chalk.redBright("JWT token has expired, please reauthenticate."));
262
+ process.exit(1);
175
263
  }
176
264
 
177
- let data = fs.readFileSync(pegaConfigJsonPath, { encoding: 'utf8' });
178
- data = JSON.parse(data);
265
+ }
179
266
 
180
- if (data[LIBRARY_BASED]) {
181
- addDebugLog("getLibraryBased", `based: ${data[LIBRARY_BASED]}`, "");
182
- addDebugLog("getLibraryBased", "END", "-");
183
- return data[LIBRARY_BASED];
184
- }
185
- else {
186
- addDebugLog("getLibraryBased", "END", "-");
187
- return false;
188
- }
267
+ export const getLibraryBased = () => {
268
+ // addDebugLog("getLibraryBased", "", "+");
269
+ // const currentDirectory = process.cwd();
270
+ // const pegaConfigJsonPath = join(currentDirectory, TASKS_CONFIG_JSON_FILENAME);
271
+
272
+ // try {
273
+ // // force to throw error and not console log
274
+ // await checkPathAccess(pegaConfigJsonPath, {}, true);
275
+ // }
276
+ // catch (ex) {
277
+ // // when can't find it, always be false, assume no library based.
278
+ // // This is IMPORTANT for init (npx install)
279
+ // return false;
280
+ // }
281
+
282
+ // let data = fs.readFileSync(pegaConfigJsonPath, { encoding: 'utf8' });
283
+ // data = JSON.parse(data);
284
+
285
+ // if (data[LIBRARY_BASED]) {
286
+ // addDebugLog("getLibraryBased", `based: ${data[LIBRARY_BASED]}`, "");
287
+ // addDebugLog("getLibraryBased", "END", "-");
288
+ // return data[LIBRARY_BASED];
289
+ // }
290
+ // else {
291
+ // addDebugLog("getLibraryBased", "END", "-");
292
+ // return false;
293
+ // }
294
+
295
+ return defaultConfigs.isLibraryBased;
189
296
  }
190
297
 
191
298
  export const setLibraryBased = async (isLibraryBased) => {
@@ -445,14 +552,34 @@ addDebugLog("getPegaServerConfig", "END", "-");
445
552
  return config['server-config'];
446
553
  };
447
554
 
448
- export const getComponentDefaults = async () => {
449
- addDebugLog("getComponentDefaults", "", "+");
450
- const config = await getPegaConfig();
451
- addDebugLog("getComponentDefaults", "config:" + JSON.stringify(config), "");
452
555
 
453
- addDebugLog("getComponentDefaults", "END", "-");
454
- return config.component;
455
- };
556
+ // contains all the config, task config, etc. created upon start
557
+ export const getConfigDefaults = () => {
558
+ addDebugLog("getConfigDefaults", `defaults: ${JSON.stringify(defaultConfigs)}`, "");
559
+
560
+ return defaultConfigs;
561
+
562
+ }
563
+
564
+ export const forceDefaultsUpdate = async () => {
565
+ defaultConfigs = await setUpDefaults();
566
+ }
567
+
568
+ export const updateServerConfigRuleSetAndVersion = async (rulesetName, rulesetVersion) => {
569
+ addDebugLog("updateServerConfigRuleSetAndVersion", `rulesetName: ${rulesetName}, rulesetVersion: ${rulesetVersion}`, "");
570
+ const currentDirectory = process.cwd();
571
+ const pegaConfigJsonPath = join(currentDirectory, TASKS_CONFIG_JSON_FILENAME);
572
+ let configData = await getPegaConfig();
573
+ let serverConfig = configData['server-config'];
574
+
575
+ serverConfig.rulesetName = rulesetName;
576
+ serverConfig.rulesetVersion = rulesetVersion;
577
+
578
+ // update file
579
+ // stringify "4", makes the json string look like JSON in the file, formated instead of a single line
580
+ fs.writeFileSync(pegaConfigJsonPath, JSON.stringify(configData, null, 4), { encoding: 'utf8' });
581
+
582
+ }
456
583
 
457
584
  export const updateComponentDefaultLibrary = async (library) => {
458
585
  addDebugLog("updateComponentDefaultLibrary", `library: ${library}`, "");
@@ -486,6 +613,28 @@ addDebugLog("updateComponentDefaultVersion", `version: ${version}`, "");
486
613
 
487
614
  }
488
615
 
616
+ export const getConfigDevBuild = async() => {
617
+ addDebugLog("getConfigDevBuild", "", "");
618
+ // const serverConfig = await getPegaServerConfig();
619
+
620
+ // return serverConfig.devBuild;
621
+ return defaultConfigs.devBuild;
622
+
623
+ }
624
+
625
+ export const setConfigDevBuild = async (devBuild) => {
626
+ addDebugLog("setConfigDevBuild", `devBuild: ${devBuild}`, "");
627
+ const currentDirectory = process.cwd();
628
+ const pegaConfigJsonPath = join(currentDirectory, TASKS_CONFIG_JSON_FILENAME);
629
+ let configData = await getPegaConfig();
630
+ let serverConfig = configData['server-config'];
631
+ serverConfig.devBuild = devBuild;
632
+
633
+ // update file
634
+ // stringify "4", makes the json string look like JSON in the file, formated instead of a single line
635
+ fs.writeFileSync(pegaConfigJsonPath, JSON.stringify(configData, null, 4), { encoding: 'utf8' });
636
+ }
637
+
489
638
  export const updateDefaultOrganization = async (organization) => {
490
639
  addDebugLog("updateDefaultOrganization", `organization: ${organization}`, "");
491
640
  let packageData = JSON.parse(fs.readFileSync(path.resolve('package.json'), 'utf-8'));
@@ -496,12 +645,82 @@ addDebugLog("updateDefaultOrganization", `organization: ${organization}`, "");
496
645
 
497
646
  }
498
647
 
499
- export const getServerType = async () => {
648
+ export const updateConfigVersion = async (componentKey, buildVersion, currentOrgLib) => {
649
+ addDebugLog("updateConfigVersion", `componentKey: ${componentKey}, buildVersion: ${buildVersion}, currentOrgLib: ${currentOrgLib}`, "");
650
+ const componentDirectory = await getComponentDirectoryPath(componentKey);
651
+ const configFileName = 'config.json';
652
+
653
+ const configFile = path.join(componentDirectory, configFileName);
654
+ let configData;
655
+ try {
656
+ configData = fs.readFileSync(path.resolve(configFile), 'utf8');
657
+ } catch (err) {
658
+ // for now, if now file just end
659
+ console.log(`${chalk.bold.yellow(`Config file not available for: ${componentKey}`)}`);
660
+ return;
661
+ }
662
+ const currentDate = new Date(Date.now());
663
+ const dateStamp = currentDate.toDateString();
664
+ const aboutData = await getAboutData(currentOrgLib, buildVersion, dateStamp);
665
+
666
+ let data;
667
+ try {
668
+ data = JSON.parse(configData);
669
+
670
+ //find "About" and create or update
671
+ const properties = data.properties;
672
+ if (properties) {
673
+ let foundProp = null;
674
+
675
+ // check to see if PegaAboutData exists. If customer decides to put
676
+ // data under PegaAboutData, we won't change the order
677
+ // otherise, will put at bottom
678
+ for (let i in properties) {
679
+ if (properties[i].name && properties[i].name === "PegaAboutData") {
680
+ foundProp = properties[i];
681
+ break;
682
+ }
683
+ }
684
+
685
+ if (foundProp) {
686
+ foundProp.label = aboutData;
687
+ }
688
+ else {
689
+ // don't have "About"
690
+ const aboutLabelJSON = {
691
+ "format": "LABEL",
692
+ "label": "About",
693
+ "name": "PegaAboutLabel",
694
+ "variant": "h3"
695
+ };
696
+ const aboutDataJSON = {
697
+ "format": "LABEL",
698
+ "label": aboutData,
699
+ "name": "PegaAboutData",
700
+ "variant": "primary"
701
+ }
702
+ properties.push(aboutLabelJSON);
703
+ properties.push(aboutDataJSON);
704
+ }
705
+ }
706
+ } catch (er) {
707
+ throw new Error(`Invalid schema json in config.json: ${er}`);
708
+ }
709
+
710
+ data.version = buildVersion;
711
+ data.buildDate = dateStamp;
712
+
713
+ // write back file
714
+ fs.writeFileSync(configFile, JSON.stringify(data, null, 4), { encoding: 'utf8' });
715
+ };
716
+
717
+ export const getServerType = () => {
500
718
  addDebugLog("getServerType", "", "+");
501
- const defaultPegaServerConfig = await getPegaServerConfig();
502
- addDebugLog("getServerType", "serverType:" + defaultPegaServerConfig.serverType, "");
719
+ // const defaultPegaServerConfig = await getPegaServerConfig();
720
+ // addDebugLog("getServerType", "serverType:" + defaultPegaServerConfig.serverType, "");
503
721
  addDebugLog("getServerType", "END", "-");
504
- return defaultPegaServerConfig.serverType;
722
+ // return defaultPegaServerConfig.serverType;
723
+ return defaultConfigs.serverType;
505
724
  }
506
725
 
507
726
  export const setServerType = async(serverType) => {
@@ -518,6 +737,12 @@ addDebugLog("setServerType", `serverType: ${serverType}`, "");
518
737
 
519
738
  }
520
739
 
740
+ export const getSrcDirectoryPath = async () => {
741
+ const currentDirectory = process.cwd();
742
+
743
+ return join(currentDirectory, "src");
744
+ }
745
+
521
746
  export const getComponentDirectoryPath = async componentKey => {
522
747
  addDebugLog("getComponentDirectoryPath", "", "+");
523
748
  const currentDirectory = process.cwd();
@@ -611,14 +836,12 @@ export const showVersion = async () => {
611
836
  process.exit(1);
612
837
  }
613
838
 
614
- const hasGit = checkGit(process.cwd());
615
-
616
- if (!hasGit) {
617
- console.log(chalk.redBright('DX Component Builder - Git needs to be installed.'));
618
- process.exit(1);
619
- }
620
-
839
+ // const hasGit = checkGit(process.cwd());
621
840
 
841
+ // if (!hasGit) {
842
+ // console.log(chalk.redBright('DX Component Builder - Git needs to be installed.'));
843
+ // process.exit(1);
844
+ // }
622
845
  };
623
846
 
624
847
  export const getOOTBComponents = async () => {
@@ -911,29 +1134,51 @@ addDebugLog("getC11NB2STokenAndStaticServer", "", "");
911
1134
 
912
1135
  }
913
1136
  return ({});
914
- }
1137
+ };
915
1138
 
916
- export const getLocalLibraryVersions = async (libraryName) => {
1139
+ export const getLocalLibraryVersions = async (libraryName, currentVersion = "0.0.0") => {
917
1140
  addDebugLog("getLocalLibraryVersions", `libraryName: ${libraryName}` , "");
918
1141
  const currentDirectory = process.cwd();
919
1142
 
920
1143
  const directory = join(currentDirectory, libraryName);
921
1144
  return fs
922
1145
  .readdirSync(directory, { withFileTypes: true })
923
- .filter(dirent => dirent.isDirectory())
1146
+ .filter(dirent => dirent.isDirectory() && dirent.name != currentVersion)
924
1147
  .map(dirent => dirent.name);
925
1148
  };
926
1149
 
1150
+ export const getLocalArchivedLibraryVersions = async (libraryName, currentVersion = "0.0.0") => {
1151
+ addDebugLog("getLocalArchivedLibraryVersions", `libraryName: ${libraryName}` , "");
1152
+ const currentDirectory = process.cwd();
1153
+ const archiveDirectory = join (currentDirectory, ARCHIVES_PATH, `${libraryName}`);
1154
+
1155
+ return fs
1156
+ .readdirSync(archiveDirectory, { withFileTypes: true })
1157
+ .filter(dirent => dirent.isDirectory() && dirent.name != currentVersion)
1158
+ .map(dirent => dirent.name);
1159
+ };
1160
+
1161
+ export const getLocalArchivedLibrraryVersionData = async(libraryName, libraryVersion) => {
1162
+ const currentDirectory = process.cwd();
1163
+ const archiveDirectory = join (currentDirectory, ARCHIVES_PATH, `${libraryName}`, `${libraryVersion}`);
1164
+
1165
+ const componentConfigFile = join (archiveDirectory, COMPONENTS_CONFIG_FILE_NAME);
1166
+
1167
+ let data = fs.readFileSync(componentConfigFile, { encoding: 'utf8' })
1168
+
1169
+ return data;
1170
+ }
1171
+
927
1172
  export const getLocalLibraryVersionData = async (libraryName, libraryVersion) => {
928
1173
  addDebugLog("getLocalLibraryVersionDate", `libraryName: ${libraryName}, libraryVersion: ${libraryVersion}` , "");
929
1174
  const currentDirectory = process.cwd();
930
1175
  const libDir = join(currentDirectory, libraryName, libraryVersion);
931
- const libDataFile = join(libDir, 'componentsconfig.json');
1176
+ const libDataFile = join(libDir, COMPONENTS_CONFIG_FILE_NAME);
932
1177
 
933
1178
  let data = fs.readFileSync(libDataFile, { encoding: 'utf8' })
934
1179
 
935
1180
  return data;
936
- }
1181
+ };
937
1182
 
938
1183
  export const deleteLocalLibrary = async (libraryName) => {
939
1184
  addDebugLog("deleteLocalLibrary", `libraryName: ${libraryName}` , "");
@@ -962,9 +1207,7 @@ addDebugLog("deleteLocalLibrary", `libraryName: ${libraryName}` , "");
962
1207
  }
963
1208
  }
964
1209
 
965
-
966
-
967
- }
1210
+ };
968
1211
 
969
1212
  export const deleteLocalLibraryVersion = async (libraryName, libraryVersion) => {
970
1213
  addDebugLog("getLocalLibraryVersions", `libraryName: ${libraryName}, libraryVersion: ${libraryVersion}` , "");
@@ -987,8 +1230,7 @@ addDebugLog("getLocalLibraryVersions", `libraryName: ${libraryName}, libraryVers
987
1230
  throw error;
988
1231
  }
989
1232
 
990
-
991
- }
1233
+ };
992
1234
 
993
1235
  export const deletePublishExtras = async (libraryName, libraryVersion) => {
994
1236
  addDebugLog("deletePublishExtras", `libraryName: ${libraryName}, libraryVersion: ${libraryVersion}` , "");
@@ -1011,14 +1253,11 @@ addDebugLog("deletePublishExtras", `libraryName: ${libraryName}, libraryVersion:
1011
1253
  fs.rmSync(dsStore, { force: true });
1012
1254
  }
1013
1255
 
1014
-
1015
- }
1016
-
1017
-
1256
+ };
1018
1257
 
1019
1258
 
1020
1259
  export const cleanUp = async () => {
1021
- addDebugLog("cleanUp", "", "");
1260
+ addDebugLog("cleanUp", "", "");
1022
1261
  fs.rm('dist/components', { recursive: true }, err => {
1023
1262
  if (err) {
1024
1263
  throw err;
@@ -1030,4 +1269,434 @@ export const cleanUp = async () => {
1030
1269
  throw err;
1031
1270
  }
1032
1271
  });
1033
- };
1272
+ };
1273
+
1274
+ export const getComponentsConfigs = async () => {
1275
+ addDebugLog("getComponentsConfigs", "", "");
1276
+ const componentsList = await getComponents();
1277
+ let componentsConfigJSON = [];
1278
+ if (componentsList && componentsList.length > 0) {
1279
+ // open each config.json and copy into componentsConfigJSON
1280
+ for (let i in componentsList) {
1281
+ const componentKey = componentsList[i];
1282
+ const componentDirectory = await getComponentDirectoryPath(componentKey);
1283
+ const configPath = join(componentDirectory, 'config.json');
1284
+
1285
+ let data = fs.readFileSync(configPath, { encoding: 'utf8' });
1286
+
1287
+ data = JSON.parse(data);
1288
+ componentsConfigJSON.push(data);
1289
+
1290
+ }
1291
+ }
1292
+ return componentsConfigJSON;
1293
+ }
1294
+
1295
+
1296
+ export const createComponentsConfigFile = async (filePath) => {
1297
+ addDebugLog("createComponentsConfigFile", `filePath: ${filePath}`, "");
1298
+ const componentsConfigJSON = await getComponentsConfigs();
1299
+ const compConfgFile = join(filePath, COMPONENTS_CONFIG_FILE_NAME);
1300
+
1301
+ // write out file
1302
+ fs.writeFileSync(compConfgFile, JSON.stringify(componentsConfigJSON), { encoding: 'utf8' });
1303
+
1304
+ };
1305
+
1306
+
1307
+ export const zipVersionAndArchive = async (orgLibName, version, copyExisting) => {
1308
+ addDebugLog("zipVersionAndArchive", `orgLibName: ${orgLibName}, version: ${version}`, "");
1309
+ const srcDirectoryPath = await getSrcDirectoryPath();
1310
+ const currentDirectory = process.cwd();
1311
+ const tasksConfig = join(currentDirectory, TASKS_CONFIG_JSON_FILENAME);
1312
+ const packageJson = join(currentDirectory, PACKAGE_JSON_FILENAME);
1313
+ const archiveFileName = `${orgLibName}_${version}.zip`;
1314
+
1315
+
1316
+ // const configContent = Buffer.from(fs.readFileSync(configJson)).toString();
1317
+ const archiveDirectory = join (currentDirectory, ARCHIVES_PATH, `${orgLibName}`, `${version}`);
1318
+ if (!fs.existsSync(archiveDirectory)) {
1319
+ fs.mkdirSync(archiveDirectory, { recursive: true });
1320
+ }
1321
+
1322
+ const archiveFile = join(archiveDirectory, archiveFileName);
1323
+ if (fs.existsSync(orgLibName)) {
1324
+
1325
+ }
1326
+
1327
+ const output = fs.createWriteStream(archiveFile);
1328
+ const archive = archiver('zip', {
1329
+ zlib: { level: 9 } // Sets the compression level.
1330
+ });
1331
+
1332
+ const zipChunks = [];
1333
+ archive.on('data', chunk => zipChunks.push(chunk));
1334
+
1335
+ // Add src directory
1336
+ archive.directory(srcDirectoryPath, 'src');
1337
+
1338
+ // Add config.json file
1339
+ if (fs.existsSync(tasksConfig)) {
1340
+ archive.file(tasksConfig, { name: path.basename(tasksConfig) });
1341
+ }
1342
+
1343
+ // Add package.json file
1344
+ if (fs.existsSync(packageJson)) {
1345
+ archive.file(packageJson, { name: path.basename(packageJson) });
1346
+ }
1347
+
1348
+ // pipe archive data to the file
1349
+ archive.pipe(output);
1350
+
1351
+ output.on('end', function() {
1352
+ console.log('Data has been drained');
1353
+ });
1354
+
1355
+ // good practice to catch warnings (ie stat failures and other non-blocking errors)
1356
+ archive.on('warning', function(err) {
1357
+ if (err.code === 'ENOENT') {
1358
+ // log warning
1359
+ } else {
1360
+ // throw error
1361
+ throw err;
1362
+ }
1363
+ });
1364
+
1365
+ // good practice to catch this error explicitly
1366
+ archive.on('error', function(err) {
1367
+ throw err;
1368
+ });
1369
+
1370
+ await archive.finalize();
1371
+
1372
+ // create componentsconfig.json
1373
+ await createComponentsConfigFile(archiveDirectory);
1374
+
1375
+ // const zipBuffer = Buffer.concat(zipChunks);
1376
+ // console.log(chalk.green(`component zipped with size: ${Math.ceil(zipBuffer.length / 1024)} KB`));
1377
+ // const zipContent = zipBuffer.toString('base64');
1378
+
1379
+
1380
+
1381
+ };
1382
+
1383
+ export const unZipFromArchive = async (fileName) => {
1384
+ await unarchive(fileName, 'temp');
1385
+ };
1386
+
1387
+
1388
+ export const restoreDependenciesFromPackage = async () => {
1389
+ const currentDirectory = process.cwd();
1390
+ const tempDirectory = join(currentDirectory, "temp");
1391
+ const tempPackagePath = join (tempDirectory, PACKAGE_JSON_FILENAME);
1392
+ const currentPackagePath = join(currentDirectory, PACKAGE_JSON_FILENAME);
1393
+
1394
+ let retrievedPackageData;
1395
+ let currentPackageData;
1396
+
1397
+ // reset
1398
+ haveDependencyDifference = false;
1399
+
1400
+ try {
1401
+ retrievedPackageData = fs.readFileSync(tempPackagePath);
1402
+ }
1403
+ catch (ex) {
1404
+ console.log($chalk.red(`\nUnable to retrieve package.json from archive.`))
1405
+ process.exit(1);
1406
+ }
1407
+
1408
+ try {
1409
+ currentPackageData = fs.readFileSync(currentPackagePath);
1410
+ }
1411
+ catch (ex) {
1412
+ console.log($chalk.red(`\nUnable to retrieve current package.json.`))
1413
+ process.exit(1);
1414
+ }
1415
+
1416
+
1417
+ const rData = JSON.parse(retrievedPackageData);
1418
+ let cData = JSON.parse(currentPackageData);
1419
+
1420
+ // now keep current package.json, but replace devDependencies and dependencies.
1421
+ const sDep = JSON.stringify(rData.dependencies);
1422
+ const sDevDep = JSON.stringify(rData.devDependencies);
1423
+
1424
+ const sCurDep = JSON.stringify(cData.dependencies);
1425
+ const sCurDevDep = JSON.stringify(cData.devDependencies);
1426
+
1427
+ // any changes, update haveDependencyDifference variable
1428
+ if (sDep != sCurDep) {
1429
+ haveDependencyDifference = true;
1430
+ }
1431
+ else if (sDevDep != sCurDevDep) {
1432
+ haveDependencyDifference = true;
1433
+ }
1434
+
1435
+
1436
+ cData.dependencies = JSON.parse(sDep);
1437
+ cData.devDependencies = JSON.parse(sDevDep);
1438
+
1439
+ // update current package json file
1440
+ fs.writeFileSync(currentPackagePath, JSON.stringify(cData, null, 4), { encoding: 'utf8' });
1441
+
1442
+
1443
+ }
1444
+
1445
+ export const getHaveDependencyDifference = () => {
1446
+ return haveDependencyDifference;
1447
+ }
1448
+
1449
+ export const restoreFromArchive = async (fileName, orgLibName, version) => {
1450
+ const currentDirectory = process.cwd();
1451
+ const tempDirectory = join(currentDirectory, "temp");
1452
+ const archiveDirectory = join (currentDirectory, ARCHIVES_PATH, `${orgLibName}`, `${version}`);
1453
+ const archFileName = join(archiveDirectory, fileName);
1454
+
1455
+ await unZipFromArchive(archFileName);
1456
+
1457
+ const tempConfigPath = join(tempDirectory, TASKS_CONFIG_JSON_FILENAME);
1458
+ const currentConfigPath = join(currentDirectory, TASKS_CONFIG_JSON_FILENAME);
1459
+ const tempPackagePath = join (tempDirectory, PACKAGE_JSON_FILENAME);
1460
+ const currentPackagePath = join(currentDirectory, PACKAGE_JSON_FILENAME);
1461
+ const tempDirectoryComponents = join(tempDirectory, "src", "components");
1462
+ const currentComponentsPath = join(currentDirectory, "src", "components");
1463
+
1464
+
1465
+ let contents = fs.readFileSync(tempConfigPath);
1466
+ fs.writeFileSync(currentConfigPath, contents);
1467
+
1468
+ // restore package json dependencies
1469
+ await restoreDependenciesFromPackage();
1470
+
1471
+ // remove current components
1472
+ fs.rmSync(currentComponentsPath, { recursive: true, force: true, maxRetries: 2 });
1473
+
1474
+ if (fs.existsSync(currentComponentsPath)) {
1475
+ // try another round of rm
1476
+ fs.rmSync(currentComponentsPath, { recursive: true, force: true, maxRetries: 2 });
1477
+
1478
+ if (fs.existsSync(currentComponentsPath)) {
1479
+ // still an issue
1480
+ console.log(chalk.red('\nUnable to complete clear out src/components directory.\n'));
1481
+ console.log(chalk.red('\nUnarchived components are in /temp directory.\n'));
1482
+ console.log(chalk.red('\nYou can migrate /temp/components to src/components manually.\n'));
1483
+ console.log(chalk.red('\nYou can replace /temp/tasks.config.json with tasks.config.json.\n'));
1484
+ process.exit();
1485
+ }
1486
+ }
1487
+
1488
+ // copy temp src/components
1489
+ fs.cpSync(tempDirectoryComponents, currentComponentsPath, { recursive: true, force: true });
1490
+
1491
+ // remove temp
1492
+ fs.rmSync(tempDirectory, { recursive: true, force: true, maxRetries: 2 });
1493
+
1494
+ };
1495
+
1496
+ export const clearSrcComponentsDirectory = async () => {
1497
+ const currentDirectory = process.cwd();
1498
+ const currentComponentsPath = join(currentDirectory, "src", "components");
1499
+
1500
+ // remove current components
1501
+ fs.rmSync(currentComponentsPath, { recursive: true, force: true, maxRetries: 2 });
1502
+
1503
+ // create blank src
1504
+ fs.mkdirSync(currentComponentsPath, { recursive: true });
1505
+ };
1506
+
1507
+ export const isHiddenFile = (fileName) => {
1508
+ if (fileName.indexOf(".") == 0) {
1509
+ return true;
1510
+ }
1511
+
1512
+ return false;
1513
+ };
1514
+
1515
+ export const getCurrentOrgLib = () =>{
1516
+ return defaultConfigs.currentOrgLib;
1517
+ };
1518
+
1519
+ export const hasArchives = async () => {
1520
+ addDebugLog("hasArchives", "call", "");
1521
+ const currentDirectory = process.cwd();
1522
+ const archiveDirectory = join (currentDirectory, ARCHIVES_PATH);
1523
+ const hasArc = fs.existsSync(archiveDirectory);
1524
+ addDebugLog("hasArchives", `${hasArc}`, "");
1525
+ return hasArc;
1526
+ };
1527
+
1528
+ export const deleteArchives = async () => {
1529
+ const currentDirectory = process.cwd();
1530
+ const archiveDirectory = join (currentDirectory, ARCHIVES_PATH);
1531
+ const hasArc = fs.existsSync(archiveDirectory);
1532
+
1533
+ if (hasArc) {
1534
+ try {
1535
+ fs.rmSync(archiveDirectory, { recursive: true });
1536
+ } catch (err) {
1537
+ }
1538
+ }
1539
+ }
1540
+
1541
+ export const checkLibraryAndArchives = async () => {
1542
+
1543
+ const isLibraryBased = getLibraryBased();
1544
+ const hasArch = await hasArchives();
1545
+
1546
+ if (isLibraryBased && !hasArch) {
1547
+ console.log(chalk.yellow('\nPlease create a library before proceeding.\n'));
1548
+ process.exit();
1549
+ }
1550
+ };
1551
+
1552
+ export const hasLibraryAndVersion = async (libraryName, version, devBuild) => {
1553
+ addDebugLog("hasLibraryAndVersion", `libraryName: ${libraryName}, version: ${version}, devBuild: ${devBuild}`, "");
1554
+ // const orgLib = getConfigDefaults();
1555
+ // const currentOrgLib = `${orgLib.organization}_${libraryName}`;
1556
+ // const libFiles = await getLibraryArchivesFiles(currentOrgLib);
1557
+ // let currentVersion = version;
1558
+ // if (devBuild) {
1559
+ // currentVersion = currentVersion.concat("-dev");
1560
+ // }
1561
+ // const fileName = `${currentOrgLib}_${currentVersion}.zip`;
1562
+ // if (libFiles && libFiles.length > 0) {
1563
+ // if (libFiles.includes(fileName)) {
1564
+ // return true;
1565
+ // }
1566
+ // }
1567
+
1568
+ // return false;
1569
+
1570
+ const orgLib = getConfigDefaults();
1571
+ const currentOrgLib = `${orgLib.organization}_${libraryName}`;
1572
+ const libVersions = await getLibraryArchivesVersions(currentOrgLib);
1573
+ let currentVersion = version;
1574
+ if (devBuild) {
1575
+ currentVersion = currentVersion.concat("-dev");
1576
+ }
1577
+
1578
+ if (libVersions.includes(currentVersion)) {
1579
+ return true;
1580
+ }
1581
+
1582
+ return false;
1583
+
1584
+
1585
+ };
1586
+
1587
+ export const getLibraryArchiveDirectories = async (currentOrgLib = "") => {
1588
+ addDebugLog("getLibraryArchiveDirectories", "", "");
1589
+ const currentDirectory = process.cwd();
1590
+ const archiveDirectory = join (currentDirectory, ARCHIVES_PATH);
1591
+
1592
+ if (!fs.existsSync(archiveDirectory)) {
1593
+ return [];
1594
+ }
1595
+
1596
+ return fs
1597
+ .readdirSync(archiveDirectory, { withFileTypes: true })
1598
+ .filter(dirent => dirent.isDirectory() && (dirent.name != currentOrgLib) )
1599
+ .map(dirent => dirent.name);
1600
+ };
1601
+
1602
+ // export const getLibraryArchivesFiles = async (orgLibName, version) => {
1603
+ // addDebugLog("getLibraryArchives", `orgLibName: ${orgLibName}`, "");
1604
+ // const currentDirectory = process.cwd();
1605
+ // const archiveDirectory = join (currentDirectory, ARCHIVES_PATH, `${orgLibName}`, `${version}`);
1606
+
1607
+ // if (!fs.existsSync(archiveDirectory)) {
1608
+ // return [];
1609
+ // }
1610
+
1611
+ // return fs
1612
+ // .readdirSync(archiveDirectory, { withFileTypes: true })
1613
+ // .filter(dirent => !dirent.isDirectory() && !isHiddenFile(dirent.name) )
1614
+ // .map(dirent => dirent.name);
1615
+
1616
+ // };
1617
+
1618
+ export const getLibraryArchivesVersions = async (orgLibName, currentVersion) => {
1619
+ addDebugLog("getLibraryArchivesVersions", `orgLibName: ${orgLibName}`, "");
1620
+ // const archFileList = await getLibraryArchivesFiles(orgLibName, currentVersion);
1621
+ // let archiveVersionList = [];
1622
+
1623
+ // if (archFileList && archFileList.length > 0) {
1624
+ // for (let index in archFileList) {
1625
+ // let fileName = archFileList[index];
1626
+ // let fVersion = fileName.replace(orgLibName.concat("_"), "");
1627
+ // fVersion = fVersion.replace(".zip", "");
1628
+ // if (currentVersion != fVersion) {
1629
+ // archiveVersionList.push(fVersion);
1630
+ // }
1631
+
1632
+ // }
1633
+ // }
1634
+ // else {
1635
+ // console.error(
1636
+ // `${chalk.red.bold(`No archives found for ${orgLibName}.`)}`
1637
+ // );
1638
+ // process.exit(1);
1639
+ // }
1640
+
1641
+ // if (archiveVersionList.length == 0) {
1642
+ // console.error(
1643
+ // `${chalk.red.bold(`No archives found for ${orgLibName} beyond current version.`)}`
1644
+ // );
1645
+ // process.exit(1);
1646
+ // }
1647
+
1648
+ // return archiveVersionList;
1649
+
1650
+ const currentDirectory = process.cwd();
1651
+ const archiveDirectory = join (currentDirectory, ARCHIVES_PATH, `${orgLibName}`);
1652
+
1653
+ // const currentVersion = getConfigDefaults().buildVersion;
1654
+
1655
+ if (!fs.existsSync(archiveDirectory)) {
1656
+ return [];
1657
+ }
1658
+
1659
+ return fs
1660
+ .readdirSync(archiveDirectory, { withFileTypes: true })
1661
+ .filter(dirent => dirent.isDirectory() && dirent.name != currentVersion )
1662
+ .map(dirent => dirent.name);
1663
+
1664
+
1665
+
1666
+ };
1667
+
1668
+ export const deleteLocalArchiveLibrary = async(libraryName) => {
1669
+ const currentDirectory = process.cwd();
1670
+ const archiveDirectory = join (currentDirectory, ARCHIVES_PATH, libraryName);
1671
+
1672
+ try {
1673
+ fs.rmSync(archiveDirectory, { recursive: true });
1674
+ console.log(`${chalk.green.bold(libraryName)} is deleted from Local.`);
1675
+ } catch (err) {
1676
+ console.log(`${chalk.green.red(`No such file ${libraryName}/${version}`)}`);
1677
+ throw new Error(`No such file ${libraryName}`);
1678
+ }
1679
+
1680
+
1681
+ }
1682
+
1683
+ export const deleteLocalArchiveLibraryVersion = async(libraryName, version) => {
1684
+ const currentDirectory = process.cwd();
1685
+ const archiveDirectory = join (currentDirectory, ARCHIVES_PATH, libraryName, version);
1686
+
1687
+ try {
1688
+ fs.rmSync(archiveDirectory, { recursive: true });
1689
+ console.log(`${chalk.green.bold(`${libraryName}/${version}`)} is deleted from Local.`);
1690
+
1691
+
1692
+
1693
+ } catch (err) {
1694
+ console.log(`${chalk.green.red(`No such file ${libraryName}/${version}`)}`);
1695
+ throw new Error(`No such file ${libraryName}/${version}`);
1696
+ }
1697
+
1698
+ }
1699
+
1700
+
1701
+ // init
1702
+ await forceDefaultsUpdate();