@magentrix-corp/magentrix-cli 1.1.4 → 1.1.5

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 (2) hide show
  1. package/actions/pull.js +90 -4
  2. package/package.json +1 -1
package/actions/pull.js CHANGED
@@ -5,7 +5,7 @@ import fs from "fs";
5
5
  import { withSpinner } from "../utils/spinner.js";
6
6
  import { EXPORT_ROOT, TYPE_DIR_MAP } from "../vars/global.js";
7
7
  import { mapRecordToFile, writeRecords } from "../utils/cli/writeRecords.js";
8
- import { updateBase } from "../utils/updateFileBase.js";
8
+ import { updateBase, removeFromBase } from "../utils/updateFileBase.js";
9
9
  import { compareAllFilesAndLogStatus, promptConflictResolution, showCurrentConflicts } from "../utils/cli/helpers/compare.js";
10
10
  import path from "path";
11
11
  import { compareLocalAndRemote } from "../utils/compare.js";
@@ -94,7 +94,14 @@ export const pull = async () => {
94
94
  }
95
95
  ];
96
96
 
97
- // Step 3: Download records in parallel with spinner
97
+ // Step 3: Load existing base.json to detect deletions
98
+ const hits = await config.searchObject({}, { filename: "base.json", global: false });
99
+ const cachedResults = hits?.[0]?.value || {};
100
+ const cachedAssets = Object.values(cachedResults).filter(c =>
101
+ c.type === 'File' || c.type === 'Folder'
102
+ );
103
+
104
+ // Step 4: Download records in parallel with spinner
98
105
  const [activeClassResult, activePageResult, assets] = await withSpinner("Downloading files...", async () => {
99
106
  const meqlResults = await Promise.all(
100
107
  queries.map(q => meqlQuery(instanceUrl, token.value, q.query))
@@ -108,7 +115,52 @@ export const pull = async () => {
108
115
  ]
109
116
  });
110
117
 
111
- // Update assets base
118
+ // Collect all server asset paths
119
+ const serverAssetPaths = new Set();
120
+ const collectServerPaths = (records) => {
121
+ for (const record of records) {
122
+ const fullPath = path.join(EXPORT_ROOT, record?.Path);
123
+ serverAssetPaths.add(path.normalize(fullPath));
124
+
125
+ if (record?.Type === 'Folder' && record?.Children?.length > 0) {
126
+ collectServerPaths(record.Children);
127
+ }
128
+ }
129
+ };
130
+ collectServerPaths(assets.tree);
131
+
132
+ // Step 5: Detect and delete assets that were on server before but are now gone
133
+ const assetsToDelete = [];
134
+ for (const cached of cachedAssets) {
135
+ const cachedPath = path.normalize(cached.filePath || cached.lastKnownPath);
136
+
137
+ // If this asset was in base.json but not returned from server, it was deleted
138
+ if (!serverAssetPaths.has(cachedPath)) {
139
+ assetsToDelete.push(cachedPath);
140
+ }
141
+ }
142
+
143
+ // Delete local files/folders that were deleted on server
144
+ for (const assetPath of assetsToDelete) {
145
+ try {
146
+ if (fs.existsSync(assetPath)) {
147
+ const stats = fs.statSync(assetPath);
148
+ if (stats.isDirectory()) {
149
+ fs.rmSync(assetPath, { recursive: true, force: true });
150
+ console.log(chalk.gray(` 🗑️ Removed deleted folder: ${path.relative(process.cwd(), assetPath)}`));
151
+ } else {
152
+ fs.unlinkSync(assetPath);
153
+ console.log(chalk.gray(` 🗑️ Removed deleted file: ${path.relative(process.cwd(), assetPath)}`));
154
+ }
155
+ }
156
+ // Remove from base.json
157
+ removeFromBase(assetPath);
158
+ } catch (err) {
159
+ console.warn(chalk.yellow(` ⚠️ Could not delete ${assetPath}: ${err.message}`));
160
+ }
161
+ }
162
+
163
+ // Step 6: Update assets base
112
164
  const processAssets = (records) => {
113
165
  for (const record of records) {
114
166
  if (record?.Type === 'Folder') {
@@ -139,7 +191,7 @@ export const pull = async () => {
139
191
 
140
192
  processAssets(assets.tree);
141
193
 
142
- // Check for conflicts and have user select conflict resolution
194
+ // Step 7: Handle code entity (ActiveClass, ActivePage) deletions
143
195
  const activeClassRecords = (activeClassResult.Records || []).map(record => {
144
196
  record.Content = record.Body;
145
197
  delete record.Body;
@@ -149,6 +201,40 @@ export const pull = async () => {
149
201
  const activePageRecords = (activePageResult.Records || []);
150
202
  const allRecords = [...activeClassRecords, ...activePageRecords].map(mapRecordToFile);
151
203
 
204
+ // Get all server record IDs
205
+ const serverRecordIds = new Set([
206
+ ...activeClassRecords.map(r => r.Id),
207
+ ...activePageRecords.map(r => r.Id)
208
+ ]);
209
+
210
+ // Find code entities in base.json that are no longer on server
211
+ const cachedCodeEntities = Object.values(cachedResults).filter(c =>
212
+ c.type !== 'File' && c.type !== 'Folder' && c.recordId
213
+ );
214
+
215
+ const codeEntitiesToDelete = [];
216
+ for (const cached of cachedCodeEntities) {
217
+ // If this code entity was in base.json but not returned from server, it was deleted
218
+ if (!serverRecordIds.has(cached.recordId)) {
219
+ codeEntitiesToDelete.push(cached);
220
+ }
221
+ }
222
+
223
+ // Delete local code entity files that were deleted on server
224
+ for (const entity of codeEntitiesToDelete) {
225
+ const entityPath = entity.filePath || entity.lastKnownPath;
226
+ try {
227
+ if (entityPath && fs.existsSync(entityPath)) {
228
+ fs.unlinkSync(entityPath);
229
+ console.log(chalk.gray(` 🗑️ Removed deleted ${entity.type}: ${path.relative(process.cwd(), entityPath)}`));
230
+ }
231
+ // Remove from base.json
232
+ removeFromBase(entity.recordId);
233
+ } catch (err) {
234
+ console.warn(chalk.yellow(` ⚠️ Could not delete ${entityPath}: ${err.message}`));
235
+ }
236
+ }
237
+
152
238
  const issues = [];
153
239
  for (const record of allRecords) {
154
240
  if (record?.error) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magentrix-corp/magentrix-cli",
3
- "version": "1.1.4",
3
+ "version": "1.1.5",
4
4
  "description": "CLI tool for synchronizing local files with Magentrix cloud platform",
5
5
  "main": "index.js",
6
6
  "type": "module",