@magentrix-corp/magentrix-cli 1.2.0 → 1.2.1

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.
@@ -185,8 +185,8 @@ const handleDeleteStaticAssetAction = async (instanceUrl, apiKey, action) => {
185
185
  const errorMessage = error?.message || String(error);
186
186
  const errorLower = errorMessage.toLowerCase();
187
187
  const isNotFound = errorLower.includes('404') ||
188
- errorLower.includes('not found') ||
189
- errorLower.includes('item not found');
188
+ errorLower.includes('not found') ||
189
+ errorLower.includes('item not found');
190
190
 
191
191
  if (isNotFound) {
192
192
  // Clean up base.json since file doesn't exist on server
@@ -214,8 +214,8 @@ const handleCreateFolderAction = async (instanceUrl, apiKey, action) => {
214
214
  const errorMessage = error?.message || String(error);
215
215
  const errorLower = errorMessage.toLowerCase();
216
216
  const alreadyExists = errorLower.includes('already exists') ||
217
- errorLower.includes('folder exists') ||
218
- errorLower.includes('duplicate');
217
+ errorLower.includes('folder exists') ||
218
+ errorLower.includes('duplicate');
219
219
 
220
220
  if (alreadyExists) {
221
221
  // Folder already exists, update cache and treat as success
@@ -239,8 +239,8 @@ const handleDeleteFolderAction = async (instanceUrl, apiKey, action) => {
239
239
  const errorMessage = error?.message || String(error);
240
240
  const errorLower = errorMessage.toLowerCase();
241
241
  const isNotFound = errorLower.includes('404') ||
242
- errorLower.includes('not found') ||
243
- errorLower.includes('item not found');
242
+ errorLower.includes('not found') ||
243
+ errorLower.includes('item not found');
244
244
 
245
245
  if (isNotFound) {
246
246
  // Clean up base.json since folder doesn't exist on server
@@ -273,11 +273,80 @@ const handleDeleteFolderAction = async (instanceUrl, apiKey, action) => {
273
273
  }
274
274
  };
275
275
 
276
+ /**
277
+ * Synchronizes class name in content with filename, or vice versa.
278
+ */
279
+ const syncClassAndFileNames = (action, recordId) => {
280
+ // Only for ActiveClass
281
+ if (action.entity !== 'ActiveClass') return;
282
+
283
+ const filePath = action.filePath;
284
+ // If file was deleted or doesn't exist, skip
285
+ if (!fs.existsSync(filePath)) return;
286
+
287
+ const content = fs.readFileSync(filePath, 'utf-8');
288
+ const filename = path.basename(filePath, path.extname(filePath));
289
+
290
+ // Regex to find class/interface/enum name
291
+ // Matches: public class Name, public interface Name, public enum Name
292
+ // We assume standard formatting
293
+ const classRegex = /public\s+(?:class|interface|enum)\s+(\w+)/;
294
+ const match = content.match(classRegex);
295
+
296
+ if (match) {
297
+ const classNameInContent = match[1];
298
+
299
+ if (classNameInContent !== filename) {
300
+ // Mismatch detected
301
+
302
+ // Case 1: File was renamed (action.renamed is true) -> Update content
303
+ if (action.renamed) {
304
+ const newContent = content.replace(classRegex, (fullMatch, name) => {
305
+ return fullMatch.replace(name, filename);
306
+ });
307
+ fs.writeFileSync(filePath, newContent);
308
+ console.log(chalk.cyan(` ↻ Updated class name in file to: ${filename}`));
309
+
310
+ // Update cache with new content hash
311
+ updateBase(filePath, { Id: recordId, Type: 'ActiveClass' });
312
+ }
313
+ // Case 2: Content was updated (action.renamed is false) -> Rename file
314
+ else {
315
+ // Rename file to match class name
316
+ const dir = path.dirname(filePath);
317
+ const ext = path.extname(filePath);
318
+ const newFilename = `${classNameInContent}${ext}`;
319
+ const newFilePath = path.join(dir, newFilename);
320
+
321
+ if (fs.existsSync(newFilePath)) {
322
+ console.warn(chalk.yellow(` ⚠️ Cannot rename ${filename} to ${classNameInContent}: File already exists.`));
323
+ return;
324
+ }
325
+
326
+ try {
327
+ fs.renameSync(filePath, newFilePath);
328
+ console.log(chalk.cyan(` ↻ Renamed file to match class: ${newFilename}`));
329
+
330
+ // Update cache: update the entry for this recordId to point to new path
331
+ updateBase(newFilePath, { Id: recordId, Type: 'ActiveClass' }, newFilePath);
332
+ } catch (err) {
333
+ console.warn(chalk.yellow(` ⚠️ Failed to rename file: ${err.message}`));
334
+ }
335
+ }
336
+ }
337
+ }
338
+ };
339
+
276
340
  /**
277
341
  * Updates cache after successful operations.
278
342
  */
279
343
  const updateCacheAfterSuccess = async (action, operationResult) => {
280
344
  try {
345
+ // Sync class names/filenames if needed (Bug 2 Fix)
346
+ if (action.action === 'update' && operationResult?.recordId) {
347
+ syncClassAndFileNames(action, operationResult.recordId);
348
+ }
349
+
281
350
  switch (action.action) {
282
351
  case "create": {
283
352
  const createSnapshot = action.fields && Object.values(action.fields)[0]