emblem-vault-sdk 2.6.0 → 2.7.0

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/utils/server.js CHANGED
@@ -5,6 +5,7 @@ const path = require('path');
5
5
 
6
6
  const PORT = 8080;
7
7
  const INCOMING_DIR = path.join(__dirname, 'incoming');
8
+ const TRAITS_DIR = path.join(__dirname, 'traits');
8
9
  const VAULT_IMAGES_BASE = '/Users/shannoncode/repo/Emblem.Current/vaultImages/collection';
9
10
  const SITE_METADATA_PATH = path.join(__dirname, 'emblem-site-metadata.json');
10
11
  const SDK_METADATA_PATH = path.join(__dirname, 'sdk-metadata.json');
@@ -50,6 +51,26 @@ async function handleListCollections(req, res) {
50
51
  }
51
52
  }
52
53
 
54
+ // Handler to list available trait files
55
+ async function handleListTraits(req, res) {
56
+ try {
57
+ const files = fs.readdirSync(TRAITS_DIR);
58
+ const traits = files
59
+ .filter(file => file.endsWith('.json'))
60
+ .map(file => ({
61
+ filename: file,
62
+ name: getCollectionName(file),
63
+ folder: getCollectionFolder(file)
64
+ }));
65
+
66
+ res.writeHead(200, { 'Content-Type': 'application/json' });
67
+ res.end(JSON.stringify(traits, null, 2));
68
+ } catch (error) {
69
+ res.writeHead(500, { 'Content-Type': 'application/json' });
70
+ res.end(JSON.stringify({ error: error.message }));
71
+ }
72
+ }
73
+
53
74
  function downloadImage(url, filepath) {
54
75
  return new Promise((resolve, reject) => {
55
76
  https.get(url, (response) => {
@@ -860,6 +881,105 @@ async function handleUpdateFromRaw(req, res) {
860
881
  });
861
882
  }
862
883
 
884
+ async function handleApplyTraitMappings(req, res) {
885
+ let body = '';
886
+
887
+ req.on('data', chunk => {
888
+ body += chunk.toString();
889
+ });
890
+
891
+ req.on('end', async () => {
892
+ try {
893
+ const { items } = JSON.parse(body);
894
+
895
+ // Read current metadata files
896
+ const sdkDataRaw = fs.readFileSync(SDK_METADATA_PATH, 'utf8');
897
+ const sdkData = JSON.parse(sdkDataRaw);
898
+
899
+ const siteDataRaw = fs.readFileSync(SITE_METADATA_PATH, 'utf8');
900
+ const siteData = JSON.parse(siteDataRaw);
901
+
902
+ const results = {
903
+ sdkUpdated: 0,
904
+ siteUpdated: 0,
905
+ failed: []
906
+ };
907
+
908
+ // Update each item with the mapped properties - ONLY if they already exist
909
+ Object.keys(items).forEach(key => {
910
+ try {
911
+ const mappedItem = items[key];
912
+
913
+ // Update SDK metadata ONLY if item already exists
914
+ if (sdkData[key]) {
915
+ // Merge existing with new mapped data
916
+ sdkData[key] = {
917
+ ...sdkData[key],
918
+ ...mappedItem,
919
+ // Preserve and merge raw data
920
+ raw: {
921
+ ...sdkData[key].raw,
922
+ ...mappedItem.raw
923
+ }
924
+ };
925
+ results.sdkUpdated++;
926
+ console.log(`Updated existing item ${key} in SDK metadata`);
927
+ }
928
+
929
+ // Update Site metadata ONLY if item already exists
930
+ if (siteData[key]) {
931
+ // Merge existing with new mapped data
932
+ siteData[key] = {
933
+ ...siteData[key],
934
+ ...mappedItem,
935
+ // Preserve and merge raw data
936
+ raw: {
937
+ ...siteData[key].raw,
938
+ ...mappedItem.raw
939
+ }
940
+ };
941
+ results.siteUpdated++;
942
+ console.log(`Updated existing item ${key} in Site metadata`);
943
+ }
944
+
945
+ } catch (error) {
946
+ results.failed.push({ key, error: error.message });
947
+ console.error(`Failed to update ${key}: ${error.message}`);
948
+ }
949
+ });
950
+
951
+ // Write back to files if there are updates
952
+ if (results.sdkUpdated > 0) {
953
+ // Create SDK backup
954
+ const sdkBackupPath = SDK_METADATA_PATH + '.backup.' + Date.now();
955
+ fs.copyFileSync(SDK_METADATA_PATH, sdkBackupPath);
956
+ console.log(`Created SDK backup at: ${sdkBackupPath}`);
957
+
958
+ // Write updated SDK data
959
+ fs.writeFileSync(SDK_METADATA_PATH, JSON.stringify(sdkData, null, 2), 'utf8');
960
+ console.log(`Updated SDK metadata with trait mappings - ${results.sdkUpdated} items`);
961
+ }
962
+
963
+ if (results.siteUpdated > 0) {
964
+ // Create Site backup
965
+ const siteBackupPath = SITE_METADATA_PATH + '.backup.' + Date.now();
966
+ fs.copyFileSync(SITE_METADATA_PATH, siteBackupPath);
967
+ console.log(`Created Site backup at: ${siteBackupPath}`);
968
+
969
+ // Write updated Site data
970
+ fs.writeFileSync(SITE_METADATA_PATH, JSON.stringify(siteData, null, 2), 'utf8');
971
+ console.log(`Updated Site metadata with trait mappings - ${results.siteUpdated} items`);
972
+ }
973
+
974
+ res.writeHead(200, { 'Content-Type': 'application/json' });
975
+ res.end(JSON.stringify(results, null, 2));
976
+ } catch (error) {
977
+ res.writeHead(500, { 'Content-Type': 'application/json' });
978
+ res.end(JSON.stringify({ error: error.message }));
979
+ }
980
+ });
981
+ }
982
+
863
983
  async function handleAddMissingItemsToBoth(req, res) {
864
984
  let body = '';
865
985
 
@@ -995,6 +1115,12 @@ const server = http.createServer((req, res) => {
995
1115
  return;
996
1116
  }
997
1117
 
1118
+ // Handle GET request to list available trait files
1119
+ if (req.method === 'GET' && req.url === '/list-traits') {
1120
+ handleListTraits(req, res);
1121
+ return;
1122
+ }
1123
+
998
1124
  // Handle POST request to copy images
999
1125
  if (req.method === 'POST' && req.url === '/copy-images') {
1000
1126
  handleCopyImages(req, res);
@@ -1061,6 +1187,12 @@ const server = http.createServer((req, res) => {
1061
1187
  return;
1062
1188
  }
1063
1189
 
1190
+ // Handle POST request to apply trait mappings
1191
+ if (req.method === 'POST' && req.url === '/apply-trait-mappings') {
1192
+ handleApplyTraitMappings(req, res);
1193
+ return;
1194
+ }
1195
+
1064
1196
  // Handle GET requests for files
1065
1197
  let filePath = '.' + req.url;
1066
1198