Skip to content

Commit e7343bb

Browse files
committed
Fix the clone cleanup
1 parent efc54b8 commit e7343bb

File tree

2 files changed

+44
-12
lines changed

2 files changed

+44
-12
lines changed

packages/contentstack-clone/src/core/util/clone-handler.ts

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as path from 'path';
33
import inquirer from 'inquirer';
44
import chalk from 'chalk';
55
import * as fs from 'fs';
6+
import { rimraf } from 'rimraf';
67
import { CustomAbortController } from './abort-controller';
78
import exportCmd from '@contentstack/cli-cm-export';
89
import importCmd from '@contentstack/cli-cm-import';
@@ -567,8 +568,8 @@ export class CloneHandler {
567568
delete exportConfig.import;
568569
delete exportConfig.export;
569570

570-
// Resolve path to package root (works in both src and lib contexts)
571-
const packageRoot = __dirname.includes('/src/') ? __dirname.split('/src/')[0] : __dirname.split('/lib/lib/')[0] || __dirname.split('/lib/')[0];
571+
// Resolve path to package root - go up 3 levels from __dirname (core/util -> package root)
572+
const packageRoot = path.resolve(__dirname, '../../..');
572573
const exportDir = path.join(packageRoot, 'contents');
573574
log.debug(`Export directory: ${exportDir}`, this.config.cloneContext);
574575
const cmd: string[] = ['-k', exportConfig.source_stack, '-d', exportDir];
@@ -592,8 +593,8 @@ export class CloneHandler {
592593
log.debug('Force stop marketplace apps prompt enabled', this.config.cloneContext);
593594
}
594595

595-
// Resolve path to dummyConfig.json - always in src/core/util
596-
const configFilePath = path.join(packageRoot, 'src', 'core', 'util', 'dummyConfig.json');
596+
// dummyConfig.json is in the same directory as this file
597+
const configFilePath = path.join(__dirname, 'dummyConfig.json');
597598
cmd.push('-c');
598599
cmd.push(configFilePath);
599600
log.debug(`Writing export config to: ${configFilePath}`, this.config.cloneContext);
@@ -625,9 +626,8 @@ export class CloneHandler {
625626
delete importConfig.import;
626627
delete importConfig.export;
627628

628-
// Resolve path to dummyConfig.json - always in src/core/util
629-
const importPackageRoot = __dirname.includes('/src/') ? __dirname.split('/src/')[0] : __dirname.split('/lib/lib/')[0] || __dirname.split('/lib/')[0];
630-
const configFilePath = path.join(importPackageRoot, 'src', 'core', 'util', 'dummyConfig.json');
629+
// dummyConfig.json is in the same directory as this file
630+
const configFilePath = path.join(__dirname,'dummyConfig.json');
631631
const cmd: string[] = ['-c', configFilePath];
632632

633633
if (importConfig.destination_alias) {
@@ -794,8 +794,8 @@ export class CloneHandler {
794794
];
795795
let successMsg: string;
796796
let selectedValue: any = {};
797-
// Resolve path to package root (works in both src and lib contexts)
798-
const cloneTypePackageRoot = __dirname.includes('/src/') ? __dirname.split('/src/')[0] : __dirname.split('/lib/lib/')[0] || __dirname.split('/lib/')[0];
797+
// Resolve path to package root - go up 3 levels from __dirname (core/util -> package root)
798+
const cloneTypePackageRoot = path.resolve(__dirname, '../../..');
799799
this.config.data = path.join(cloneTypePackageRoot, 'contents', this.config.sourceStackBranch || '');
800800
log.debug(`Clone data directory: ${this.config.data}`, this.config.cloneContext);
801801

@@ -816,8 +816,38 @@ export class CloneHandler {
816816
}
817817

818818
this.cmdImport()
819-
.then(() => {
819+
.then(async () => {
820820
log.debug('Clone type selection and import completed successfully', this.config.cloneContext);
821+
822+
// Clean up contents directory after import completes
823+
if (this.config.pathDir) {
824+
const resolvedPath = path.resolve(this.config.pathDir);
825+
log.debug('Cleaning up contents directory after import', {
826+
...this.config.cloneContext,
827+
pathDir: this.config.pathDir,
828+
resolvedPath
829+
});
830+
try {
831+
await rimraf(resolvedPath);
832+
log.debug('Contents directory cleaned up successfully', {
833+
...this.config.cloneContext,
834+
pathDir: this.config.pathDir,
835+
resolvedPath
836+
});
837+
} catch (cleanupError: any) {
838+
log.debug('Cleanup error (non-fatal)', {
839+
...this.config.cloneContext,
840+
pathDir: this.config.pathDir,
841+
resolvedPath,
842+
error: cleanupError?.message,
843+
code: cleanupError?.code
844+
});
845+
// Don't fail the clone if cleanup fails
846+
}
847+
} else {
848+
log.debug('No pathDir configured, skipping cleanup', this.config.cloneContext);
849+
}
850+
821851
resolve(successMsg);
822852
})
823853
.catch(reject);

packages/contentstack-clone/tsconfig.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818
"es2020.promise"
1919
],
2020
"strictPropertyInitialization": false,
21-
"forceConsistentCasingInFileNames": true
21+
"forceConsistentCasingInFileNames": true,
22+
"resolveJsonModule": true
2223
},
2324
"include": [
2425
"src/**/*",
25-
"types/*"
26+
"types/*",
27+
"src/**/**/*.json"
2628
],
2729
"exclude": [
2830
"node_modules",

0 commit comments

Comments
 (0)